var exportTemplatePath = "./Template/cxtemplate.xlsx";
var newName = $"xxx进度表汇总-{System.DateTime.Today.ToString("yyyy-MM-dd")}.xlsx";
var exprotPath = "wwwroot/Excel/" + newName;
if (File.Exists(exprotPath))
{
File.Delete(exprotPath);
}
File.Copy(exportTemplatePath, exprotPath);
FileStream file = new FileStream(exprotPath, FileMode.Open, FileAccess.Read);
var workbook = new XSSFWorkbook(file);
var sheet = workbook.GetSheet("Sheet1");
//List<Dictionary<string, string>> dict = GetAllData();
//string json = JsonConvert.SerializeObject(dict);
//System.Data.DataTable table = JsonToObj(json, "");
//此处传入 DataTable格式数据即可
int rowIndex = sheet.LastRowNum + 1;
foreach (DataRow row in table.Rows)
{
XSSFRow dataRow = (XSSFRow)sheet.CreateRow(rowIndex);
foreach (DataColumn column in table.Columns)
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
++rowIndex;
}
using (var stream = new FileStream(exprotPath, FileMode.Create, FileAccess.Write))
{
workbook.Write(stream);
}