Sara
Sara

Reputation: 1

MVC and Export to Excel

Description:

I tried the following code in MVC and received "OutputStream is not available when a custom TextWriter is used.". I followed the solution given in the URL http://blog.maartenballiauw.be/post/2008/05/ASPNET-MVC-custom-ActionResult.aspx. But my issue is not resolved.

Please help me to resolve this issue..

Code:

string json = e.ExtraParams["GridData"].ToString();
StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
XmlNode xml = eSubmit.Xml;       

this.Response.Clear();
this.Response.ContentType = "application/vnd.ms-excel";
this.Response.AddHeader("Content-Disposition", "attachment;     
filename=submittedData.xls");
XslCompiledTransform xtExcel = new XslCompiledTransform();       
xtExcel.Load(Server.MapPath("Excel/XSLStyleSheet/Example2.xsl"));
xtExcel.Transform(xml, null, this.Response.OutputStream);
this.Response.End();

Error:

Server Error in '/' Application.

OutputStream is not available when a custom TextWriter is used. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: OutputStream is not available when a custom TextWriter is used.

Source Error:

Line 85: XslCompiledTransform xtExcel = new XslCompiledTransform();
Line 86: xtExcel.Load(Server.MapPath("Excel/XSLStyleSheet/Example2.xsl"));
Line 87: xtExcel.Transform(xml, null, this.Response.OutputStream); <============== ERROR LINE
Line 88: this.Response.End();
Line 89: }

Upvotes: 0

Views: 1158

Answers (2)

kenny
kenny

Reputation: 22414

@Khalid's suggestion is good. If you really want to write to Excel XML I've had good results doing stuff with the ClosedXML library.

Upvotes: 1

Khalid Abuhakmeh
Khalid Abuhakmeh

Reputation: 10839

This might sound like a simple solution, but why not just ouput your data into a CSV format. Excel has no problems opening CSV spreadsheets.

Upvotes: 0

Related Questions