StreamWriter and export to excel

S

Serkan

Hi guys
i tried this example at
http://www.codeproject.com/dotnet/ExportToExcel.asp

but
i add these statements below

HttpResponse response = HttpContext.Current.Response;
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" +
fileName + "\""); //filenam koddan geliyor
response.Write(excelDoc);

when run the page it appear file save window and i save the file but the
file contains only "System.IO.StreamWriter"

i write below code to est data is created or not created in excel file

excelDoc = new System.IO.StreamWriter("c:\deneme.xml", true,
System.Text.Encoding.Default);

and i see that data is true and written in excel

i think that is the problem how i can write
Response.Write(excelDoc.ToString()); I'm sure this
row(Response.Write(excelDoc.ToString()); ) is not correct.
so my question is how can i write StreamWriter to web page to save file as
excel...

Thank you for helping....
 
M

Michael Stancombe

Hi,

Try changing the stream writer so it is writing to the pages output
stream.

eg

//get response, set content type.
HttpResponse response = HttpContext.Current.Response;
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" +
fileName + "\"");
response.Clear();
excelDoc = new System.IO.StreamWriter(response.OutputStream);

//code to write all the excel stuff, as per the article.
//...
//eg:
excelDoc.Write("This will appear in the file.");
//...

//flush and finish response
excelDoc.Flush();
response.End();

hth -Mike
 
G

Garfield

thank you for helping...



Michael Stancombe said:
Hi,

Try changing the stream writer so it is writing to the pages output
stream.

eg

//get response, set content type.
HttpResponse response = HttpContext.Current.Response;
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" +
fileName + "\"");
response.Clear();
excelDoc = new System.IO.StreamWriter(response.OutputStream);

//code to write all the excel stuff, as per the article.
//...
//eg:
excelDoc.Write("This will appear in the file.");
//...

//flush and finish response
excelDoc.Flush();
response.End();

hth -Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top