Tan078 2017-08-07 09:10 采纳率: 100%
浏览 4159
已采纳

C#使用epplus.dll读取excel数据

图片说明
写了一个方法去读取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;
    }
}

}

  • 写回答

1条回答

  • threenewbee 2017-08-07 09:31
    关注

    worksheet.Cells那几行调试下,是不是有单元格不存在
    SetModel里面也调试下,传入的这几个参数中有一个是null或者不能转换为需要的格式

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致