使用epplus创建.xlsm文件 [英] Use epplus to create .xlsm file
问题描述
我正在尝试让一个网站导出.xlsm文件,但似乎找不到任何有用的东西.这是我用来测试构建.xlsx文件(有效)的一个简单示例.
I'm trying to have a website export a .xlsm file and I can't seem to find anything that helps. Here's a simple example I'm using to test building a .xlsx file (which works).
@using OfficeOpenXml;
<html>
<body>
<div id="page-wrapper">
@{
// Change file extension to xlsm to test
string fileExtension = "xlsm";
ExcelPackage p = new ExcelPackage();
p.Workbook.Worksheets.Add("Worksheet Name");
int LatestWorksheetNumber = p.Workbook.Worksheets.Count;
ExcelWorksheet ws = p.Workbook.Worksheets[LatestWorksheetNumber];
ws.Cells[1, 1].Value = "Test";
//Generate A File
Byte[] bin = p.GetAsByteArray();
string filename = "filename";
try
{
//Download the file as an attachment
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Cookies.Clear();
string ContentType = "";
if (fileExtension == "xlsx")
{
ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
else
{
ContentType = "application/vnd.ms-excel.sheet.macroEnabled.12";
}
Response.GetType();
Response.ContentType = ContentType;
Response.AddHeader("content-disposition", "attachment; filename=" + filename + "." + fileExtension);
Response.BinaryWrite(bin);
Response.End();
<p>File Created</p>
}
catch (Exception e)
{
<p>@e.Message</p>
}
}
</div>
</body>
</html>
将文件扩展名更改为.xlsm后,会生成文件,但是当我尝试在Excel中打开文件时,出现错误消息,提示扩展名不正确.我认为我唯一需要更改的就是内容类型标头,但这显然不是问题.我还想念什么???任何指导将不胜感激!
After changing the file extension to .xlsm the file is generated but when I try to open the file in Excel I get an error saying that the extension isn't correct. I thought the only thing I would have to change would be the content type header but that's obviously not the issue. What else am I missing??? Any guidance would be appreciated!
推荐答案
Xiaoy312提出了此问题!在Byte[] bin = p.GetAsByteArray();
之前添加p.Workbook.CreateVBAProject();
解决了我的问题!其他所有内容均保持不变,但Excel实际上将立即打开文件!这是我遇到相同问题的所有人的最终代码:
Xiaoy312 nailed the issue! Adding p.Workbook.CreateVBAProject();
before Byte[] bin = p.GetAsByteArray();
solved my issue! Everything else stayed the same but Excel will actually open the files now! Here's my final code for anyone who has the same issue:
@using OfficeOpenXml;
<html>
<body>
<div id="page-wrapper">
@{
// Change file extension to xlsm to test
string FileExtension = "xlsm";
ExcelPackage p = new ExcelPackage();
p.Workbook.Worksheets.Add("Worksheet Name");
int LatestWorksheetNumber = p.Workbook.Worksheets.Count;
ExcelWorksheet ws = p.Workbook.Worksheets[LatestWorksheetNumber];
ws.Cells[1, 1].Value = "Test";
p.Workbook.CreateVBAProject();
//Generate A File
Byte[] bin = p.GetAsByteArray();
string filename = "filename";
try
{
//Download the file as an attachment
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Cookies.Clear();
string ContentType = "";
if (FileExtension == "xlsx")
{
ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
else
{
ContentType = "application/vnd.ms-excel.sheet.macroEnabled.12";
}
Response.GetType();
Response.ContentType = ContentType;
Response.AddHeader("content-disposition", "attachment; filename=" + filename + "." + FileExtension);
Response.BinaryWrite(bin);
Response.End();
<p>File Created</p>
}
catch (Exception e)
{
<p>@e.Message</p>
}
}
</div>
</body>
</html>
这篇关于使用epplus创建.xlsm文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!