Reputation: 11
I have a grid view. Am trying to export in the excel. The code is as follows :
public override void VerifyRenderingInServerForm(Control gvReport1)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string attachment = "attachment; filename=Report.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
gvReport1.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();
}
Now the problem is that an excel file is opening but data is not getting displayed. I guess the data is not getting read, and the more wierd thing is that I have two web pages. In the first webpage the export to excel is working properly, but on the second page it does not work.
Upvotes: 1
Views: 756
Reputation: 434
I hope you find what you search on here :
.doc
application/msword
.dot
application/msword
.docx
application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx
application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docm
application/vnd.ms-word.document.macroEnabled.12
.dotm
application/vnd.ms-word.template.macroEnabled.12
.xls
application/vnd.ms-excel
.xlt
application/vnd.ms-excel
.xla
application/vnd.ms-excel
.xlsx
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltx
application/vnd.openxmlformats-officedocument.spreadsheetml.template
.xlsm
application/vnd.ms-excel.sheet.macroEnabled.12
.xltm
application/vnd.ms-excel.template.macroEnabled.12
.xlam
application/vnd.ms-excel.addin.macroEnabled.12
.xlsb
application/vnd.ms-excel.sheet.binary.macroEnabled.12
.ppt
application/vnd.ms-powerpoint
.pot
application/vnd.ms-powerpoint
.pps
application/vnd.ms-powerpoint
.ppa
application/vnd.ms-powerpoint
.pptx
application/vnd.openxmlformats-officedocument.presentationml.presentation
.potx
application/vnd.openxmlformats-officedocument.presentationml.template
.ppsx
application/vnd.openxmlformats-officedocument.presentationml.slideshow
.ppam
application/vnd.ms-powerpoint.addin.macroEnabled.12
.pptm
application/vnd.ms-powerpoint.presentation.macroEnabled.12
.potm
application/vnd.ms-powerpoint.presentation.macroEnabled.12
.ppsm
application/vnd.ms-powerpoint.slideshow.macroEnabled.12
Upvotes: 0
Reputation: 29213
There's probably a more elegant way...
There is.
Have a look at this: Export to Excel library
It's a completely free library which uses Microsoft's Open XML libraries to create a genuine Excel 2007 .xlsx file.
All you have to do is call one "CreateExcelDocument" function, and pass in the DataTable, DataSet or List<> which contains the data you're using to fill your GridView.
// Step 1: Create a DataSet, and put some sample data in it
DataSet ds = CreateSampleData();
// Step 2: Create the Excel .xlsx file
try
{
string excelFilename = "C:\\Sample.xlsx";
CreateExcelFile.CreateExcelDocument(ds, excelFilename);
}
catch (Exception ex)
{
MessageBox.Show("Couldn't create Excel file.\r\nException: " + ex.Message);
return;
}
Enjoy, and good luck !!
Upvotes: 1
Reputation: 5276
Response.Write(stw.ToString());
Is incorrect, this will just return the type name, you need to write the output of RenderControl to a string first, then write the string content using Response.Write().
EDIT: Here is an example of how you could get the content of a rendered control:
// Obtain the content about to be rendered.
var ms = new MemoryStream();
var w = new StreamWriter(ms);
var r = new StreamReader(ms);
using (var htmlw = new HtmlTextWriter(w))
{
gvReport1.RenderControl(htmlw);
htmlw.Flush();
}
ms.Position = 0;
var content = r.ReadToEnd();
r.Close();
There's probably a more elegant way...
Upvotes: 0