Last post Jul 01, 2011 04:23 AM by jason tb
Jun 27, 2011 01:48 PM|Gindy39|LINK
Yet another thread of this topic, but a lot are unresolve or the subject is the same but the error is diferent, so here is my situation.
I'm using VB.net 2008, Excel 2011. On my web app I have this gridview that poblates itself from a SQL table. I can select the items of the grid and paste them over a Excel worksheet and they appear fine, but sometimes the data on the grid can get enormous
and well the copy + paste method is not the best. To simplify I added a button that "will" export the gridview into a excel worksheet, there the user can play with it , save it ect...
This is the current code I have for this action:
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
Protected Sub btnexcel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnexcel.Click
Response.Charset = ""
' If you want the option to open the Excel file without saving then
' comment out the line below
Response.ContentType = "application/vnd.xls"
Dim stringWrite As New System.IO.StringWriter()
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
Also on Page load I got code inside If Postback event disabling paging and sorting (having them enable causes lots of errors)
Once I click on the Export button, Excel pops up and gives the following error:
"The file you are trying to open, '[filename]', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file
now?" (Yes | No | Help)
Clicking YES will open the file with the following inside
CELL A2 <div>
CELL C2 </div>
thats it. Suppostly this code works on MS2003 but I don't have 2003 to test it on, and my users all have 2010 ,soo it don't matter if it works on 2003 or not.
Having the option to open the file and seen just the Div tags, makes me wonder if the code is working and this is really a excel error. Or maybe is a code error and excel is only able to see the div tags.
thank you for your time
Jun 28, 2011 03:16 AM|aswin_istq|LINK
Dim tw As New StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
Dim frm As HtmlForm = New HtmlForm()
Page.Response.ContentType = "application/vnd.ms-excel"
Page.Response.Charset = ""
Page.EnableViewState = False
frm.Attributes("runat") = "server"
Jun 28, 2011 07:52 AM|Gindy39|LINK
Changing my button code for the one you provided Aswin, still gives me the error. But now once I click OK anyway to proceed I get something different on excel. They still are just some html tags. Someone recomended to export to word then manually export
to excel, since there hasn't been an issue exporting to word. I'm going to try that now see how it wrks
Jun 28, 2011 08:28 AM|Gindy39|LINK
definetly something is wrong with either code or something.
I tried a similar code to the one I posted before, but exporting to word. It only export the 2 <div> tags. Using a 3rd code I found on another post, I debbug the app to check something and found out that the data is apparantly not binding and I have a databing
instruction ><. I'll keep trying
Jun 28, 2011 08:36 AM|Gindy39|LINK
With you code I decided to add
me.grdview1.databind () and then the code and it gives me the error.... but once i kill OK it gives me the whole grid ^^... NOW IM A HAPPY PERSON
now i need to figure out how to do this, since the grid can have diferent datasources since its the output of a search
Jun 29, 2011 12:53 AM|aswin_istq|LINK
you should bind the gridview b4. it will solve your problem
Jul 01, 2011 04:23 AM|jason tb|LINK
Have a look at the link,
Export gridview to EXCEL, WORD, PDF WITHOUT OLE AUTOMATION.