Nov 30, 2011

Create Dynamic Excel file using Asp.Net

public static void Write(HttpResponse response)
{

// make sure nothing is in response stream
response.Clear();
response.Charset = "";

// set MIME type to be Excel file.
response.ContentType = "application/ms-excel";

// add a header to response to force download (specifying filename)
response.AddHeader("Content-Disposition", "attachment; filename=\"MyFile.xls\"");

for (int i = 0; i < 10; i++)
{
// Send the data. Tab delimited, with newlines.
response.Write("Col1 \t Col2 \t Col3 \t Col4 \n");
}



// Close response stream.
response.End();

}

2 comments: