我正在尝试在我的asp.net MVC应用程序中使用NPOI实现excel文件下载功能。
该文件有三张,Sheet2有两个activex按钮。
我正在阅读源文件,向sheet2添加值,将其保存在临时位置,稍后使用以下代码下载文件。
using (var fs = new FileStream(xlsFilePath, FileMode.Open, FileAccess.Read))
{
var templateWorkbook = new XSSFWorkbook(fs);
//Sheet update operation done here
fs.Close();
var memoryStream = new MemoryStream();
templateWorkbook.Write(memoryStream);
System.IO.File.WriteAllBytes(groupDocumentPath, memoryStream.ToArray());
}
问题: 由于activex控件,下载的文件已损坏,并且在尝试打开文件时出现错误:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><logFileName>error728720_01.xml</logFileName><summary>Errors were detected in file 'C:\Users\dsekaran\Downloads\5B87FFF306BE8040D10B702 (5).xlsm'</summary><repairedParts><repairedPart>Repaired Part: /xl/worksheets/sheet2.xml part with XML error. Catastrophic failure Line 1, column 0.</repairedPart></repairedParts><repairedRecords><repairedRecord>Repaired Records: Drawing from /xl/drawings/drawing1.xml part (Drawing shape)</repairedRecord></repairedRecords></recoveryLog>
澄清: 这是否意味着NPOI不支持activex按钮? 如何克服这个问题呢?
答案 0 :(得分:0)
下载.xlsm文件时,您必须使用正确的MIME类型。我通过NPOI创建后下载.xltm文件。请参阅以下功能
public ActionResult DownloadXLTMFile()
{
try
{
//Using Resposne Stream to Make File Available for User to Download;
Response.Clear();
Response.ContentType = "application/vnd.ms-excel.template.macroEnabled.12";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "YourFileName.xltm"));
Response.BinaryWrite(System.IO.File.ReadAllBytes(HostingEnvironment.MapPath("~/App_Data/YourManupulatedFile.xltm")));
Response.End();
}
catch (Exception Ex)
{
}
finally
{ }
return View();
}
检查this链接以获取相应的MIME类型