数据绑定返回空值

本文关键字:空值 返回 数据绑定 | 更新日期: 2023-09-27 18:11:20

所以我试图读取excel文件并将数据插入类属性。我认为有一个问题,当我实例化类,但我做它之前,我正在解析和添加值到类。

一旦解析完成-我调用绑定类,但它返回null。

调用

        TestData td = new TestData();
        XLReader.GetClassFromExcel<TestData>(1,1,1);
        var ddddd = td.Title; //this is null.

    public class TestData
{
    public string Title { get; set; }
    public string Site { get; set; }
    public string StartDate { get; set; }
    public string EndDate { get; set; }
    public string Email { get; set; }
    public string OrderRef { get; set; }
    public string ReqBy { get; set; }
    public string ApprovedBy { get; set; }
}

Excel的解析

public static class XLReader
{
    /// <summary>
    /// The deployment files folder name
    /// </summary>
    private static readonly string deploymentFilesFolderName =
        Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    public static List<T> GetClassFromExcel<T>(int fromRow, int fromColumn, int toColumn = 0)
    {
        var path = Path.Combine(deploymentFilesFolderName, @"Tools'TestData.xlsx");
        List<T> retList = new List<T>();
        using (var pck = new ExcelPackage())
        {
            using (var stream = File.OpenRead(path))
            {
                pck.Load(stream);
            }
            //Retrieve first Worksheet
            var ws = pck.Workbook.Worksheets.First();
            //If the to column is empty or 0, then make the tocolumn to the count of the properties
            //Of the class object inserted
            toColumn = toColumn == 0 ? typeof(T).GetProperties().Count() : toColumn;
            //Read the first Row for the column names and place into a list so that
            //it can be used as reference to properties
            List<string> columnNames = new List<string>();
            // wsRow = ws.Row(0);
            foreach (var cell in ws.Cells[1, 1, 1, ws.Cells.Count()])
            {
                columnNames.Add(cell.Value.ToString());
            }
            //Loop through the rows of the excel sheet
            for (var rowNum = fromRow; rowNum <= ws.Dimension.End.Row; rowNum++)
            {
                //create a instance of T
                T objT = Activator.CreateInstance<T>();
                //Retrieve the type of T
                Type myType = typeof(T);
                //Get all the properties associated with T
                PropertyInfo[] myProp = myType.GetProperties();
                var wsRow = ws.Cells[rowNum, fromColumn, rowNum, ws.Cells.Count()];
                foreach (var propertyInfo in myProp)
                {
                    if (columnNames.Contains(propertyInfo.Name))
                    {
                        int position = columnNames.IndexOf(propertyInfo.Name);
                        //To prevent an exception cast the value to the type of the property.
                        var blah = Convert.ChangeType(wsRow[rowNum, position + 1].Value, propertyInfo.PropertyType);
                        propertyInfo.SetValue(objT,blah);
                    }
                }
                retList.Add(objT);
            }
        }
        return retList;
    }
}

数据绑定返回空值

当我导入excel数据时,我总是使用以下代码创建一个字符串数组来存储值

package.Load(fileContent.InputStream);
ExcelWorksheet workSheet = package.Workbook.Worksheets[1];
int totalRows = workSheet.Dimension.End.Row;
int totalCols = workSheet.Dimension.End.Column;
List<myObject> listMyObject = new List<myObject>();
for (int currentRow = 1; currentRow <= totalRows; currentRow++)
{
    string[] resultRow = new string[totalCols];
    for (int currentCol = 1; currentCol <= totalCols; currentCol++)
    {
        //We read the entire line and store it in the array
        resultRow[currentCol - 1] = (workSheet.Cells[currentRow, currentCol].Value != null) ? workSheet.Cells[currentRow, currentCol].Value.ToString() : "";
    }
   //And now I can bind my array to my object
   listMyObject.Add(myObjectHelper.Convert(resultRow));
}
//Here i've got my list object

我的数组最多只包含一个空字符串。在这种情况下,我知道我的Excel文件中没有填写任何内容。

将字符串数组转换为对象后