写了一个方法去读取excel2017表格数据,为什么m_ReadDatumList.Add()会报System.NullReferenceException这个异常?求大神解答
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CNData
{
public class Datum
{
public Datum()
{
}
public string S_No { set; get; }
public string Des { set; get; }
public double F_Norm { set; get; }
public double F_USL { set; get; }
public double F_LSL { set; get; }
}
}
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace CNData
{
public class Program
{
private static List m_ReadDatumList = new List(100);
private static void Main(string[] args)
{
FileInfo existingFile = new FileInfo("D:test.xlsx");
try
{
ExcelPackage package = new ExcelPackage(existingFile);
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];//选定第一页
int maxColumnNum = worksheet.Dimension.End.Column;//最大列
int minColumnNum = worksheet.Dimension.Start.Column;//最小列
int maxRowNum = worksheet.Dimension.End.Row;//最大行
int minRowNum = worksheet.Dimension.Start.Row;//最小行
string strno = "C";
string strdes = "D";
string strfnorm = "E";
string strfusl = "F";
string strflsl = "F";
for (int nRowIndex = 6; nRowIndex <= maxRowNum; nRowIndex++)
{
//object objMark = worksheet.Cells[strno + nRowIndex.ToString()].Value;
//if (objMark == null)
// continue;
int m = 2 * nRowIndex - 6;
int n = m + 1;
object objno = worksheet.Cells[strno + m.ToString()].Value;
object objdes = worksheet.Cells[strdes + m.ToString()].Value;
object objf_Norm = worksheet.Cells[strfnorm + m.ToString()].Value;
object objf_USL = worksheet.Cells[strfusl + m.ToString()].Value;
object objf_LSL = worksheet.Cells[strflsl + n.ToString()].Value; m_ReadDatumList.Add(SetModel(objno, objdes, objf_Norm, objf_USL, objf_LSL));
//int C1 = m_ReadDatumList.Count;
//Console.WriteLine(C1);
//Console.ReadKey();
}
}
catch (Exception ex)
{
throw ex;
}
}
private static Datum SetModel(object objno, object objdes, object objf_Norm, object objf_USL, object objf_LSL)
{
Datum mdlDatum = new Datum();
mdlDatum.S_No = objno.ToString().Trim();
mdlDatum.Des = objdes.ToString().Trim();
mdlDatum.F_Norm = double.Parse(objf_Norm.ToString());
mdlDatum.F_USL = double.Parse(objf_USL.ToString());
mdlDatum.F_LSL = double.Parse(objf_LSL.ToString());
return mdlDatum;
}
}
}