2016-10-17 77 views
0

asp.net MVCアプリケーションでExcelファイルのダウンロード機能を実装しようとしましたが、NPOIを使用しています。NPOIを使用したActiveXコントロールを使用した.xlsmドキュメントのダウンロードに関する問題

ファイルには3つのシートがあり、シート2には2つのアクティブなボタンがあります。

enter image description here

私は、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ボタンをサポートしていませんか? この問題を解決するにはどうすればよいですか?

+1

ジャスト側は、あなたがブロックを使用しているときのFileStreamを閉じる必要はありません注意してください。 – mybirthname

答えて

0

.xlsmファイルのダウンロード中に正しいMIMEタイプを使用する必要があります。私はNPOIで作成した後、.xltmファイルをダウンロードしています。適切なMIMEタイプのための

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リンク

関連する問題