簡體   English   中英

導出到Excel-在MVC4中安裝Office自定義

[英]Export to Excel - Installing Office Customization in MVC4

我正在MVC4應用程序中導出到excel文件。
電子表格

public class ExporttoExcel : ActionResult
{
public GridView ExcelGridView { get; set; }
public string fileName { get; set; }
public int totalQuantity;
public decimal totalPrice1;
public string x1;

public ExporttoExcel(GridView gv, string pFileName, int totalQty, decimal totalPrice, string x)
{
  x1= x;
  ExcelGridView = gv;
  fileName = pFileName;
  totalQuantity = totalQty;
  totalPrice1 = totalPrice;
}
    public override void ExecuteResult(ControllerContext context)
    {
    HttpContext curContext = HttpContext.Current;
    curContext.Response.Clear();
    curContext.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
    curContext.Response.Charset = "";
    curContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    curContext.Response.ContentType = "application/vnd.ms-excel";      
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    if (x1== "111")
    {
      htw.WriteLine("<html><head>");
      //code logic
      ExcelGridView.HeaderStyle.BackColor = Color.Yellow;
      ExcelGridView.RenderControl(htw);
      htw.WriteLine("</body></html>");
    }
    else
    {
      htw.WriteLine("Data");
      ExcelGridView.RenderControl(htw);
    }
    byte[] byteArray = Encoding.ASCII.GetBytes(sw.ToString());
    MemoryStream s = new MemoryStream(byteArray);
    StreamReader sr = new StreamReader(s, Encoding.ASCII);
    curContext.Response.Write(sr.ReadToEnd());
    curContext.Response.End();
    }
}

發生以下錯誤,並且沒有打開excel文件。

Error : 
From: file:///d:/Program Files/Microsoft Visual Studio 10.0/Common7/IDE/PrivateAssemblies/Microsoft.VisualStudio.QualityTools.LoadTestExcelAddIn.vsto did not succeed.


我的代碼中有任何錯誤。 我應該怎么做才能解決此錯誤。
編輯:
僅在IE中會發生此錯誤。 Firefox和Chrome打開excel文件。

我將三個文件添加到本地計算機中指定錯誤的位置。

Microsoft.VisualStudio.QualityTools.LoadTestExcelAddIn.dll
Microsoft.VisualStudio.QualityTools.LoadTestExcelAddIn.dll.manifest
Microsoft.VisualStudio.QualityTools.LoadTestExcelAddIn

它工作正常,IE會打開Excel文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM