Aspose.Cells库可以很容易地在.NET Core中创建、读取和操作Excel文件(.xls和.xlsx格式)。本文主要介绍.Net Core中通过Aspose组件,创建和读取Excel文件中数据的方法。利用Aspose组件操作Excel文件,性能比较好,也比较快,格式兼容性也挺好。另外服务器或PC上不用安装msoffice Excel软件就可以使用。下面分享一下.NET Core中调用Aspose组件的代码。

1、Aspose组件下载

Aspose下载地址https://products.aspose.com/cells/net

破解版下载地址https://download.csdn.net/download/wylcy/10389581?utm_source=bbsseo

官方文档地址https://docs.aspose.com/display/cellsnet/Home

官方Demo代码https://github.com/aspose-cells/Aspose.Cells-for-.NET

2、安装Nuget包

.Net Core使用Aspose.Cells组件,还要安装两个的Nuget包:

System.Drawing.Common

System.Text.Encoding.CodePages

Nuget使用教程VS(Visual Studio)中Nuget的使用

3、创建Excel文件代码

      public static void SaveExcelData(string path)
        {
            //Aspose.Cells.License li = new Aspose.Cells.License();
            //li.SetLicense("Aspose.Cells.lic");
            Aspose.Cells.Workbook wk = new Aspose.Cells.Workbook();
            Worksheet ws = wk.Worksheets[0];
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    ws.Cells.Rows[i][j].Value = (i + 1) + "*" + (j + 1) + "=" + (i + 1) * (j + 1);
                }
            }
            wk.Save(path);
        }

4、读取Excel文件代码 

 public static void ReadExcelData(string path)
        {
            DataTable dt = new DataTable();
            //验证license
            //Aspose.Cells.License li = new Aspose.Cells.License();
            //li.SetLicense("Aspose.Cells.lic");
            Aspose.Cells.Workbook wk = new Aspose.Cells.Workbook(path);
            Worksheet ws = wk.Worksheets["人力资源报表"];
            dt = ws.Cells.ExportDataTable(0, 0, ws.Cells.MaxDataRow + 1, ws.Cells.MaxDataColumn + 1);
            for (int k = 1; k < dt.Rows.Count; k++)
            {
                var code = dt.Rows[k][2].ToString();
                var subDept = dt.Rows[k][0].ToString();
                var dept = dt.Rows[k][1].ToString();
                var userName = dt.Rows[k][3].ToString();
                var gender = dt.Rows[k][4].ToString();
                Console.WriteLine($"code={code},subDept={subDept},dept={dept},userName={userName},gender={gender}");
            }
        }

5、本文项目代码下载

下载地址https://www.cjavapy.com/download/5c4c4e49dc72d93b685b6f63/

相关文档.NET Core Aspose Word(.doc,docx)文件加水印   

                  .NET Core Aspose Word(.doc,docx)转成pdf文件

推荐文档