我正在使用ASP.Net中的EPPlus库创建Excel xlsm文件。当我创建文件并打开文件时,显示以下弹出消息: 我们在'ExcelDemo.xlsm'中发现了某些内容的问题。您是否希望我们尽力恢复原状?如果您信任此工作簿的来源,请单击“是”。 我正在使用以下代码
MemoryStream ms = new MemoryStream();
FileStream file_stream = new FileStream("C:\excelDemo.xlsm", FileMode.Open);
byte[] fileContent = null;
file_stream.CopyTo(ms);
file_stream.Close();
DataSet ds1 = new DataSet();
//will take dataset from from DB
FileInfo file = new FileInfo("ExcelDemo.xlsm");
using (OfficeOpenXml.ExcelPackage excelPackage = new OfficeOpenXml.ExcelPackage(ms))
{
foreach (DataTable dt in ds1 .Tables)
{
OfficeOpenXml.ExcelWorkbook excelWorkBook = excelPackage.Workbook;
OfficeOpenXml.ExcelWorksheet excelWorksheet = excelWorkBook.Worksheets[dt.TableName];
excelWorksheet.Cells["A20"].LoadFromDataTable(dt, false);
}
excelPackage.SaveAs(ms);
fileContent = excelPackage.GetAsByteArray();
}
ms.Close();
return new FileContentResult(fileContent, "application/vnd.ms-excel.template.macroEnabled.12") { FileDownloadName = "ExcelDemo.xlsm"};
我的代码有问题吗?或者这是Excel问题?