2013-04-05 72 views
1

我正在導出到MVC4應用程序中的excel文件。
Excel中導出到Excel - 在MVC4中安裝Office自定義

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文件。

+0

Excel旨在打開Excel文件。你在這裏做的是一個HTML文件。 – 2013-04-05 05:58:46

+0

爲什麼你不直接發送內容到響應?你需要中間文件嗎? 「if」的哪個分支正在執行? – 2013-04-05 06:07:31

+0

@Darin是的。我傳遞一個gridview.It是主要的東西,必須出口到excel .. html只用於裝飾excel。 – kk1076 2013-04-05 06:39:55

回答

0

我在我的本地機器中指定錯誤的位置添加了三個文件。

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

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