0

我正在读取一个 excel 文件并将数据逐列添加到集合中。一旦遇到空值,它就会在下面的行中爆炸:

exampleDataList.Add(new PersonalData {firstname = col1Value.ToString(),lastname = col2Value.ToString(), address=col3Value.ToString(), salary=col4Value.ToString() });

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
    const int startRow = 1;

    if (file != null && Path.GetExtension(file.FileName) == ".xlsx")
    {
        IList<PersonalData> exampleDataList = new List<PersonalData>();
        using(var excel = new ExcelPackage(file.InputStream))
        {
            //open and read the xlsx file.                   
                //Get the work book in the file
            ExcelWorkbook workBook = excel.Workbook;
                if (workBook != null)
                {
                    if (workBook.Worksheets.Count > 0)
                    {
                        //Get the first worksheet
                        ExcelWorksheet currentWorkSheet = workBook.Worksheets.First();


                        for (int rowNumber = startRow + 1; rowNumber <= currentWorkSheet.Dimension.End.Row; rowNumber++)
                        // read each row from the start of the data (start row + 1 header row) to the end of the spreadsheet.
                        {
                            object col1Value = currentWorkSheet.Cells[rowNumber, 1].Value;
                            object col2Value = currentWorkSheet.Cells[rowNumber, 2].Value;
                            object col3Value = currentWorkSheet.Cells[rowNumber, 3].Value;
                            object col4Value = currentWorkSheet.Cells[rowNumber, 4].Value;                             

                            if ((col1Value != null && col2Value != null))
                            {
                                exampleDataList.Add(new PersonalData {firstname = col1Value.ToString(),lastname = col2Value.ToString(), address=col3Value.ToString(), salary=col4Value.ToString() });
                            }                                  

                        }

                        int myresultcount = WriteToDb(exampleDataList);
                    }

                }                  

        }
    }

    return RedirectToAction("Index");        
}

这是我的课

public class PersonalData
{
    public int Id { get; set; }
    public string  firstname { get; set; }
    public string lastname { get; set; }
    public string address { get; set; }
    public string salary { get; set; }
}

收到错误消息:对象引用未设置为对象的实例

我该如何纠正?我究竟做错了什么。我的目标是在一天结束时将其写入数据库。

4

3 回答 3

1

我相信您忘记检查 value3 和 value4 是否为 null,或者如果这些值允许为 null,则在执行之前检查它们是否为 nullToString()

if (col1Value != null && col2Value != null && 
    col3Value != null && col4Value != null)
{
    exampleDataList.Add(new PersonalData 
     {
          firstname = col1Value.ToString(),
          lastname = col2Value.ToString(), 
          address = col3Value.ToString(), 
          salary = col4Value.ToString() 
     });
}  

或者

if (col1Value != null && col2Value)
{
    exampleDataList.Add(new PersonalData 
    {
       firstname = col1Value.ToString(),
       lastname = col2Value.ToString(), 
       address = col3Value != null ? col3Value.ToString() : String.Empty, 
       salary = col4Value.ToString() != null ? col4Value.ToString() : String.Empty
    });
}  
于 2013-07-02T18:07:33.967 回答
1

检查第 3 列和第 4 列中的空值:

if ((col1Value != null && col2Value != null && col3Value != null && col4Value != null))
{
    exampleDataList.Add(new PersonalData {firstname = col1Value.ToString(),lastname = col2Value.ToString(), address=col3Value.ToString(), salary=col4Value.ToString() });
} 

或者,如果您想要第 3 列和第 4 列的空字符串而不是 NULL:

if ((col1Value != null && col2Value != null))
{
    exampleDataList.Add(new PersonalData {
            firstname = col1Value.ToString(),
            lastname  = col2Value.ToString(), 
            address   = col3Value == null ? "" : col3Value.ToString(), 
            salary    = col4Value == null ? "" : col4Value.ToString() });
}

为了简化 NULL 检查,您可以利用将+空字符串用于空值并自动调用.ToString()对象的行为:

if ((col1Value != null && col2Value != null))
{
    exampleDataList.Add(new PersonalData
        {
            firstname = col1Value.ToString(),
            lastname  = col2Value.ToString(), 
            address   = "" + col3Value, 
            salary    = "" + col4Value
        });
}
于 2013-07-02T18:08:11.943 回答
0

您缺少对 colXValue 对象的一些验证,您应该在尝试对其调用 ToString() 方法之前进行验证。

这取决于您要如何处理空值。

如果您想忽略在任何列中具有 null 的任何记录,则只需扩展您的:

 if ((col1Value != null && col2Value != null))

看起来像:

 if (    col1Value != null 
      && col2Value != null
      && col3Value != null
      && col4Value != null)

您还可能希望将空值转换为空字符串,在这种情况下,您可以执行以下操作:

 object col1Value = currentWorkSheet.Cells[rowNumber, 1].Value;
 object col2Value = currentWorkSheet.Cells[rowNumber, 2].Value;
 object col3Value = currentWorkSheet.Cells[rowNumber, 3].Value;
 object col4Value = currentWorkSheet.Cells[rowNumber, 4].Value;
 if (col1Value == null) col1Value = string.Emtpy;
 if (col2Value == null) col2Value = string.Emtpy;
 if (col3Value == null) col3Value = string.Emtpy;
 if (col4Value == null) col4Value = string.Emtpy;
 exampleDataList.Add(new PersonalData {firstname = col1Value.ToString(),lastname = col2Value.ToString(), address=col3Value.ToString(), salary=col4Value.ToString() });
于 2013-07-02T18:14:15.800 回答