使用NPOI使用ActiveX控件下载.xlsm文档时出现问题 [英] Issue downloading .xlsm document with activex controls using NPOI
问题描述
我正在尝试在asp.net MVC应用程序中实现Excel文件下载功能,并使用 NPOI .
I am trying to implement an excel file download functionality in my asp.net MVC application and using NPOI.
该文件有三张纸,Sheet2有两个Activex按钮.
The file has three sheets and Sheet2 has two activex buttons.
我正在读取源文件,将值添加到sheet2,将其保存在临时位置,然后使用以下代码下载文件.
I am reading the source file, adding values to sheet2, save it in temporary location and download the file later using the following code.
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控件,因此下载的文件已损坏,并且在尝试打开文件时抛出错误:
Issue: Because of the activex control the downloaded file is corrupt and when trying to open the file it is throwing an error:
<?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按钮? 该如何解决这个问题?
Clarification: Does this mean NPOI doesn't support activex buttons? What can be done to overcome this issue?
推荐答案
在下载.xlsm文件时,您必须使用正确的MIME类型.通过NPOI创建.xltm文件后,我正在下载它.参见下面的功能
you'll have to use correct MIME type while downloading the .xlsm file. I am downloading .xltm file after creating it through NPOI. See the below function
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();
}
check this link for appropriate MIME type
这篇关于使用NPOI使用ActiveX控件下载.xlsm文档时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!