使用ExcelPackage进行Excel报表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用ExcelPackage进行Excel报表相关的知识,希望对你有一定的参考价值。

Nuget包名为

epplus.core

 

命名空间OfficeOpenXml

 

  string localFileName = path + Path.DirectorySeparatorChar + fileName;
                FileInfo file = new FileInfo(localFileName);
                using (ExcelPackage package = new ExcelPackage(file))
                {
                    ExcelWorkbook wb = package.Workbook;

                    //配置文件属性  
                    wb.Properties.Title = "库存导入模板";
                    // 添加worksheet
                    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("商品库存");
                    //添加头
                    worksheet.Cells[1, 1].Value = "商品ID";
                    worksheet.Cells[1, 2].Value = "商品名称";
                    worksheet.Cells[1, 3].Value = "货号";
                    worksheet.Cells[1, 4].Value = "原价";
                    worksheet.Cells[1, 5].Value = "售价";
                    worksheet.Cells[1, 6].Value = "数量";
                    worksheet.Cells[1, 7].Value = "库存规格1";
                    worksheet.Cells[1, 8].Value = "库存规格2";

                    //获取当前商户库存数据模板
                    var items = _pser.GetImportInnentoryList(CurrentVendorID);
                    int rowindex = 1;
                    foreach (var item in items)
                    {
                        rowindex++;
                        //添加值
                        worksheet.Cells[rowindex, 1].Value = item.ProductID;
                        worksheet.Cells[rowindex, 2].Value = item.ProductName;
                        worksheet.Cells[rowindex, 3].Value = item.ArtNo;
                        worksheet.Cells[rowindex, 4].Value = item.TagPrice;
                        worksheet.Cells[rowindex, 5].Value = item.Price;
                        worksheet.Cells[rowindex, 6].Value = item.Quantity;
                        worksheet.Cells[rowindex, 7].Value = item.Specification1;
                        worksheet.Cells[rowindex, 8].Value = item.Specification2;

                    }
                    package.Save();
                }

跑是肯定跑不通的,因为我是COPY的

以上是关于使用ExcelPackage进行Excel报表的主要内容,如果未能解决你的问题,请参考以下文章

EPPlus--ExcelPackage 导出Excel

以编程方式创建 Excel 2007 表格

Excel类库

Excel 到 DataTable 使用 EPPlus - Excel 锁定编辑

C#怎么读取Excel的数据

帆软报表怎么连接几个不同的excel表格