Issue downloading .xlsm document with activex cont

2019-09-03 16:35发布

I am trying to implement an excel file download functionality in my asp.net MVC application and using NPOI.

The file has three sheets and Sheet2 has two activex buttons.

enter image description here

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());
        }

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>

Clarification: Does this mean NPOI doesn't support activex buttons? What can be done to overcome this issue?

1条回答
手持菜刀,她持情操
2楼-- · 2019-09-03 17:00

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

查看更多
登录 后发表回答