export gridview to excel format error

  • Thread starter Thread starter JohnE
  • Start date Start date
J

JohnE

Hello. I am exporting a gridview to excel 2007. I am using the xlsx
extension in the export code. But, when it asks for open or save, I will get
the following error when trying to open the spreadsheet from either method.
The error reads,

"Excel cannot open the file 'ChangeRequestList[2].xlsx' because the file
format or file extension is not valid. Verify that the file has not been
corrupted and that the file extension matches the format of the file."

The gridview does have paging on. I took it off to make one long gridview
on one page and the error still occurs eitherway.

Here is the code that I am using for the export. It is the same as what I
have been finding when searching for exporting information.

Does anyone know what the reason is behind the error and how to fix it?

Thanks... John
 
Sorry, I forgot to paste the code in.

protected void btnExportToExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=ChangeRequestList.xlsx");
Response.Charset = "";

// If you want the option to open the Excel file without saving then
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xlsx";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new
HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for the specified
ASP.NET server control at run time.
return;
}
 
The ContentType was seen is more then one sample when googling. I went to
the site and got the xlsx and pasted it in as the ContentType but it still
gives the same error. Is there a web setting I need to do? If so, where is
that done (IE8)?
Thanks... John (relative newbie)
 
Hello.  I am exporting a gridview to excel 2007.  I am using the xlsx
extension in the export code.  But, when it asks for open or save, I will get
the following error when trying to open the spreadsheet from either method.  
The error reads,

"Excel cannot open the file 'ChangeRequestList[2].xlsx' because the file
format or file extension is not valid.  Verify that the file has not been
corrupted and that the file extension matches the format of the file."

The gridview does have paging on.  I took it off to make one long gridview
on one page and the error still occurs eitherway.  

Here is the code that I am using for the export.  It is the same as what I
have been finding when searching for exporting information.

Does anyone know what the reason is behind the error and how to fix it?

Thanks... John  

You can try with GemBox spreadsheet component. http://www.gemboxsoftware.com/GBSpreadsheet.htm
This component is very fast and very easy to work with. Here's an
example how you can export datagrid with it:
http://www.gemboxsoftware.com/LA/Import-Export-DataGrid-XLS-XLSX-CSV-HTML-.NET.htm
 
Mark Rae said:
[please don't top-post]
The ContentType was seen is more then one sample when googling. I went to
the site and got the xlsx and pasted it in as the ContentType but it still
gives the same error. Is there a web setting I need to do? If so, where
is
that done (IE8)?

http://blogs.msdn.com/vsofficedeveloper/pages/Excel-2007-Extension-Warning.aspx

Okay. I read all the info and made all the changes. But I am still getting
the error that was shown in the initial entry. I can not see what is causing
the error message to appear especially after following the instructions so
far and it is getting frustrating. Below is the code. You see any errors
in it? Any further thoughts?
Thanks...John


protected void btnExportToExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=ChangeRequestList.xlsx");
Response.Charset = "";

// If you want the option to open the Excel file without saving then
// comment out the line below
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType =
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new
HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for the specified
ASP.NET server control at run time.
return;
}
 
Back
Top