添加NPOI包
在这里插入图片描述
将数据导出Excel实现
public async Task<bool> ExportPatientStatisticsDetails(ExportPatientDetailDto patientDetails)
{
try
{
var patientList = patientDetails.PatientTableList;
HSSFWorkbook workbook = new HSSFWorkbook();
//创建工作表
var sheet = workbook.CreateSheet("患者统计表");
//创建标题行
var row = sheet.CreateRow(0);
//创建单元格
var cellName = row.CreateCell(0);
cellName.SetCellValue("部位");
var cellNumber = row.CreateCell(1);
cellNumber.SetCellValue("患者数");
var cellPortion = row.CreateCell(2);
cellPortion.SetCellValue("比例");
//遍历集合,生成行
int index = 1;
for (int i = 0; i < patientList.Count(); i++)
{
var x = index + i;
var rowi = sheet.CreateRow(x);
var cell1 = rowi.CreateCell(0);
cell1.SetCellValue(patientList[i].NameOfDistributionType);
var cell2 = rowi.CreateCell(1);
cell2.SetCellValue(patientList[i].NumberOfPatients);
var cell3 = rowi.CreateCell(2);
cell3.SetCellValue(patientList[i].Proportion);
}
if (File.Exists(@"D:\患者统计表.xls"))
{
File.Delete(@"D:\患者统计表.xls");
}
FileStream file = new FileStream(@"D:\患者统计表.xls", FileMode.CreateNew, FileAccess.Write);
workbook.Write(file);
file.Dispose();
return true;
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
}