问题描述:

        

C# list写出到Excel
可视化效果
C# list写出到Excel
Excel导出效果

 

解决方法:

/*
引用
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
数据
List<string> xList = new List<string>() { };
List<int> bCount = new List<int>() { };
List<int> lCount = new List<int>() { };
List<int> dCount = new List<int>() { };
List<int> dayCount = new List<int>() { };
*/
            #region 导出excel
            workbook = new XSSFWorkbook();
            ISheet sheet = null;
            fileName = savePath +strCN+strMDMC + dtpTime1 + "至" + dtpTime2 + ".xlsx";
            fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            int i = 0;
            try
            {
                sheet = workbook.CreateSheet("Sheet0");
                //{"交易日期","早餐","午餐","晚餐","总数"};
                IRow rowDate = sheet.CreateRow(0);
                IRow rowBCount = sheet.CreateRow(1);
                IRow rowLCount = sheet.CreateRow(2);
                IRow rowDCount = sheet.CreateRow(3);
                IRow rowAllCount = sheet.CreateRow(4);
                rowDate.CreateCell(0).SetCellValue("交易日期");
                rowBCount.CreateCell(0).SetCellValue("早餐");
                rowLCount.CreateCell(0).SetCellValue("午餐");
                rowDCount.CreateCell(0).SetCellValue("晚餐");
                rowAllCount.CreateCell(0).SetCellValue("总数");
                for (i = 1; i < xCount + 1;i++ )
                {
                    rowDate.CreateCell(i).SetCellValue(xList[i-1]);
                    rowBCount.CreateCell(i).SetCellValue(bCount[i-1]);
                    rowLCount.CreateCell(i).SetCellValue(lCount[i-1]);
                    rowDCount.CreateCell(i).SetCellValue(dCount[i-1]);
                    rowAllCount.CreateCell(i).SetCellValue(dayCount[i-1]);
                }
                
                
                workbook.Write(fs); //写入到excel
                MessageBox.Show("导出成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("导出失败!详情联系开发者!");
            }

依赖:

 

相关文章:

  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2022-01-05
  • 2022-12-23
猜你喜欢
  • 2021-10-13
  • 2021-10-28
  • 2022-02-02
  • 2022-12-23
  • 2022-12-23
  • 2021-06-10
相关资源
相似解决方案