Please set the ExcelPackage.LicenseContext property. See https://epplussoftware.com/developers/licenseexception

¥18.00

使用EPPlus读取Excel2007文件到DataTable,报错:Please set the ExcelPackage.LicenseContext property. See https://epplussoftware.com/developers/licenseexception


public static DataTable Read2007(Stream inputStream)
        {
            DataTable table = new DataTable();

            ExcelPackage pck = new ExcelPackage(inputStream);
            ExcelWorksheet ws = pck.Workbook.Worksheets[1];
            int rowNum = ws.Dimension.End.Row;
            int colNum = ws.Dimension.End.Column;

            for (int i = 1; i <= rowNum; i++)
            {
                DataRow dataRow = table.NewRow();
                for (int j = 1; j <= colNum; j++)
                {
                    if (i == 1)
                    {
                        if (ws.Cells[i, j].Value != null)
                        {
                            DataColumn column = new DataColumn(ws.Cells[i, j].Value.ToString());
                            table.Columns.Add(column);
                        }
                    }
                    else
                    {
                        dataRow[j - 1] = ws.Cells[i, j].Value != null ? ws.Cells[i, j].Value.ToString() : null;

                    }
                }
                if (i != 1)
               
            

最佳解决方案

原因分析

EPPlus 5.0 以后的版本需要指定 商业证书 或者非商业证书。你需要在代码里指定证书或者降低EPPlus版本

在代码里面指定非商业证书:ExcelPackage.LicenseContext = LicenseContext.NonCommercial;


   public static DataTable Read2007(Stream inputStream)
        {
            DataTable table = new DataTable();
           //指定EPPlus使用非商业证书
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            ExcelPackage pck = new ExcelPackage(inputStream);
            ExcelWorksheet ws = pck.Workbook.Worksheets[0];
            int rowNum = ws.Dimension.End.Row;
            int colNum = ws.Dimension.End.Column;

            for (int i = 1; i <= rowNum; i++)
            {
                DataRow dataRow = table.NewRow();
                for (int j = 1; j <= colNum; j++)
                {
                    if (i == 1)
                    {
                        if (ws.Cells[i, j].Value!=null)
                        {
                            DataColumn column = new DataColumn(ws.Cells[i, j].Value.ToString());
                            table.Columns.Add(column);
                        }
                       
                    }
                    else
                    {
                        dataRow[j - 1] = ws.Cells[i, j].Value != null ? ws.Cells[i, j].Value.ToString() : null;

                    }
              &nbs
                        

hierror T4 被采纳率73%
2020-08-29 11:00
打赏 0 0
页面统计
4429 访问
0 帮助
0.00 打赏

hierror 2019 © hierror.com 京ICP备13026190号-1

通知消息
  • 暂无任何消息