1

I am exporting to excel from GridView which works fine on localhost but not on the production server. I am guessing there needs to be some excel component installed on the server. Does anyone know what I need to install to get this working?

GridViewAllApps.DataBind();

Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "attachment;filename=Applications.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridViewAllApps.RenderControl(hw);
string style = @"<style> .textmode {mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
IainB
  • 45
  • 1
  • 4

2 Answers2

0

LainB,

What errors are you getting on the production server? Your grid subclass renders the grid to an Excel file via an html writer which you string write to a file using a method you created called RenderControl(). That method code probably makes use of Office Interop dll's on your machine (You probably reference them in your using statements and / or project references).

Unfortunately, when running on the web server, your code is constrained by what IIS is allowed to do. Even if you installed the Office components on the server, your web site will not be able to access the files. Additionally, baking the interop files into the distribution might be forbidden due to legal restrictions.

See here Create Excel (.XLS and .XLSX) file from C#

Community
  • 1
  • 1
Marc Lyon
  • 336
  • 2
  • 7
  • I'm not getting any errors. What happens is the Excel context pops up asking whether you want to open or save. If you choose to open then Excel opens up but no worksheet is generated. If you choose to save then a file is saved which shows a valid file size but when you open it the file is empty. It works fine on local so that has lead me to believe it is a missing Office component on the live server. – IainB Jul 19 '16 at 06:29
0

Solution: I abandoned writing DataGrid to Excel because it requires Excel component installed on the server. Marc's link lead me to EPPlus which I installed (dll) and works like a dream.

IainB
  • 45
  • 1
  • 4