MVC粘贴HTML到Excel

我有一个html内容,我想导出为ex​​cel文件。 我导出到Excel的function如下所示:

public ActionResult ExportToExcel(string htmlToExport) { using (ExcelPackage ep = new ExcelPackage()) { string fileTemplate = "test.xlsx"; using (FileStream stream = new FileStream(HttpContext.Server.MapPath("~/") + ConfigurationManager.AppSettings["templatePath"] + fileTemplate, FileMode.Open)) { ep.Load(stream); } ExcelWorksheet ws = ep.Workbook.Worksheets.First(); Response.AppendHeader("Content-Disposition", fileTemplate); return File(ep.GetAsByteArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); } } 

有没有办法将html粘贴到excel文件? 如果我手动这样做,一切都OK。 我想在服务器端做到这一点,并返回粘贴HTML内的Excel文件。 这可能吗?

当然这是可能的。 我的代码示例如下;

  public ActionResult BankAccountReport(BankAccountsInfoFilter bankAccountsInfoFilter) { var bankAccountInfos = service.GetBankAccounts(bankAccountsInfoFilter); DetailReportExport(bankAccountInfos); return new EmptyResult(); } [NonAction] public void DetailReportExport(List<BankAccountViewModel> bankAccountInfos) { string html = RenderPartialViewToString("_BankAccountDetailReport", bankAccountInfos); Response.ClearContent(); Response.AddHeader("content-disposition", "attachment; filename=BankAccountInfoDetailReport.xls"); Response.ContentType = "application/excel"; Response.AddHeader("content-language", "tr-TR"); Response.ContentEncoding = System.Text.Encoding.UTF8; Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble()); Response.Write(html); }