使用封闭的xml将数据表转换为excel(xls或xlsx)。 当您提供使用下载功能时,这将帮助您。 并且您正在使用sql server并在excel中向用户提供数据。
答案 0 :(得分:0)
从nuget下载closedxml。
using(XLWorkbook wb = new XLWorkbook()) {
wb.Worksheets.Add(dt);
wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
wb.Style.Font.Bold = true;
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename= EmployeeReport.xlsx");
using(MemoryStream MyMemoryStream = new MemoryStream()) {
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
答案 1 :(得分:0)
以下是另一种方法,使用GemBox.Spreadsheet:
ExcelFile ef = new ExcelFile();
ef.Styles.Normal.HorizontalAlignment = HorizontalAlignmentStyle.Center;
ef.Styles.Normal.Font.Weight = ExcelFont.BoldWeight;
ExcelWorksheet ws = ef.Worksheets.Add("Sheet1");
ws.InsertDataTable(dt);
ef.Save(Response, "EmployeeReport.xlsx");