Click to See Complete Forum and Search --> : Export to Excel missing data


ericbonsu
June 11th, 2008, 09:30 AM
I use the function below to export a datagrid to excel but the some of the data exported to excel is missing can anyone help?
I havew already looked into timeout issues but it can't be that becuase the missing data is random.

Sub DataGridToExcel(ByVal dgExport As DataGrid, ByVal response As HttpResponse)
dgExport.ShowHeader = True
Response.Clear()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim stringWrite As New System.IO.StringWriter()
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
dgExport.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()
End Sub

Shuja Ali
June 11th, 2008, 09:45 AM
Please use CODE tags when you are posting sourcecode in your posts.

Is there any specific pattern of missing data, like any particular column that is missing or any particular row that is missing in the Excel sheet. The code looks ok to me.

ericbonsu
June 11th, 2008, 09:51 AM
Altogether there should be 2155 records pulled but it looks like records 512 to about 817 are missing.

Shuja Ali
June 11th, 2008, 10:04 AM
I would suggest debugging the code and see if the Dataset contains all the records or not.

ericbonsu
June 11th, 2008, 10:07 AM
On the webpage the datagrid displays all the data, it only goes missing when exported to excel. Is there another function I could use to export to excel?

Shuja Ali
June 11th, 2008, 10:09 AM
On the webpage the datagrid displays all the data, it only goes missing when exported to excel. Is there another function I could use to export to excel?
As I said, I don't see anything wrong with the code, I have been using similar code for months now. You will have to debug it and see why your data is missing.

ericbonsu
June 11th, 2008, 11:41 AM
I did some investigating and it looks like it stops exporting after this character "<" (without the quotes of course). Any ideas?

Shuja Ali
June 11th, 2008, 12:28 PM
Basically the string is rendered as HTML and < sign is used to represent HTML tags. This is the reason why your data is not coming up properly. You will have to replace this with &lt;.

ericbonsu
June 11th, 2008, 01:08 PM
That worked and all data exports now but now it shows < as &lt in excel.
Is there a way to strip html tags in the function instead of having to do a replace in the SQL statement.

Shuja Ali
June 11th, 2008, 02:03 PM
You should replace < with &lt; and not just &lt.

ericbonsu
June 11th, 2008, 02:30 PM
It works thanks.
I'll just have to do a replace for all string fields.