export gridview to excel format error

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
 
J

JohnE

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;
}
 
J

JohnE

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)
 
F

filip

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
 
J

JohnE

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;
}
 
Joined
Jun 26, 2010
Messages
2
Reaction score
0
Export to excel issues

Here is complete details about all problems we face in this process:
praveenbattula.blogspot.com/2010/09/gridview-and-export-
to-excel.html
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top