先整理好Excel模板,如:
接下来在程序获取上面整理好的Excel模板并替换关键字就可以了public ActionResult SummaryStatistics()
public ActionResult SummaryStatistics()
{
Workbook workbook;
using (var stream = System.IO.File.OpenRead(Server.MapPath("/Content/Templates/Statistics/数据汇总表.xlsx")))
{
workbook = new Workbook(stream);
}
//上半部分统计
workbook.Replace(string.Format("${0}$", "TrainOrgCount"), summaryLastMonth.TrainOrgCount);
workbook.Replace(string.Format("${0}$", "TrainOrgThanLastMonth"), summaryLastMonth.TrainOrgThanLastMonth);
workbook.Replace(string.Format("${0}$", "TrainCount"), summaryLastMonth.TrainCount);
workbook.Replace(string.Format("${0}$", "TrainThanLastMonth"), summaryLastMonth.TrainThanLastMonth);
workbook.Replace(string.Format("${0}$", "ExamCount"), summaryLastMonth.ExamCount);
workbook.Replace(string.Format("${0}$", "PassRate"), summaryLastMonth.PassRate);
workbook.Replace(string.Format("${0}$", "CertCount"), summaryLastMonth.CertCount);
workbook.Replace(string.Format("${0}$", "CertThanLastMonth"), summaryLastMonth.CertThanLastMonth);
workbook.Replace(string.Format("${0}$", "ContinueEduCount"), summaryLastMonth.ContinueEduCount);
workbook.Replace(string.Format("${0}$", "ChangeCertCount"), summaryLastMonth.ChangeCertCount);
//省略N个处理......,处理方式与上面一样
if (!System.IO.Directory.Exists(Server.MapPath("/UploadFileXLS")))
{
System.IO.Directory.CreateDirectory(Server.MapPath("/UploadFileXLS"));
}
var filePath = "/UploadXLS/" + time.AddMonths(-1).Year + "年" + time.AddMonths(-1).Month + "月份数据汇总表.xlsx";
var address = Server.MapPath(filePath);
if (System.IO.File.Exists(address))
{
System.IO.File.Delete(address);
}
workbook.Save(address);
return View();
}
知识兔这样就处理好了