简体   繁体   中英

Export to Excel - Installing Office Customization in MVC4

I am exporting to excel file in MVC4 application.
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();
    }
}

The below error occurs and the excel file is not opened.

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


Any mistake in my code. What should I do to resolve this error. ?
EDIT :
This error occurs only in IE. Firefox and Chrome opens the excel file.

I added the three files in my local machine in the location where the error was specified.

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

It works fine and IE opens the Excel file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM