By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 2,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

Export to excel.

P: n/a
Hi all,
I'm exporting a datagrid to an excel file.
I'm using the following source code to export.
DataTable dt = new DataTable();
DataAccess.StoredProcedure sproc = new
DataAccess.StoredProcedure("sproc_getdata");

sproc.Run(dt);

DataGrid1.DataSource = dt;

DataGrid1.DataBind();

DataGrid1.EnableViewState = false;

DataGrid1.Visible = true;

DataGrid1.GridLines = GridLines.Both;

Response.Clear();

Response.Buffer = true;

Response.AddHeader( "Content-Disposition", "filename=File1.xls");
StringWriter stringWriter = new StringWriter();

HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

DataGrid1.RenderControl(htmlWriter);

Response.Write(stringWriter.ToString());

Response.ContentType="application/vnd.ms-excel";

Response.End();

Its working fine.

but there is an isuue datagrid dt contains extended language
strings (like chinese, japanese).These strings are not getting
exported to excel .
DataGrid is getting data from SQLServer2005 database. I know that
sqlserver is using utf-16 encoding for unicode character. So i've tried
to change Response.ContentEncoding to utf-16. But it is also not
working.

Can anyone help me.

Sep 29 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
What datatypes are you database columns?
If they are not nvarchars (or similar) you haven't got a hope of
getting the unicode data out.
el***********@gmail.com wrote:
Hi all,
I'm exporting a datagrid to an excel file.
I'm using the following source code to export.
DataTable dt = new DataTable();
DataAccess.StoredProcedure sproc = new
DataAccess.StoredProcedure("sproc_getdata");

sproc.Run(dt);

DataGrid1.DataSource = dt;

DataGrid1.DataBind();

DataGrid1.EnableViewState = false;

DataGrid1.Visible = true;

DataGrid1.GridLines = GridLines.Both;

Response.Clear();

Response.Buffer = true;

Response.AddHeader( "Content-Disposition", "filename=File1.xls");
StringWriter stringWriter = new StringWriter();

HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

DataGrid1.RenderControl(htmlWriter);

Response.Write(stringWriter.ToString());

Response.ContentType="application/vnd.ms-excel";

Response.End();

Its working fine.

but there is an isuue datagrid dt contains extended language
strings (like chinese, japanese).These strings are not getting
exported to excel .
DataGrid is getting data from SQLServer2005 database. I know that
sqlserver is using utf-16 encoding for unicode character. So i've tried
to change Response.ContentEncoding to utf-16. But it is also not
working.

Can anyone help me.
Sep 29 '06 #2

P: n/a

Code Monkey wrote:
What datatypes are you database columns?
If they are not nvarchars (or similar) you haven't got a hope of
getting the unicode data out.

they are nvarchar only..
The datagrid is showing all the characters..
without the line Response.ContentType="application/vnd.ms-excel"; i'm
getting correct output

Sep 29 '06 #3

P: n/a
Just an idea....

I've got similar code in one of my web-apps, but I'm using
<code>
GridView1.DataSource = myDataTable;
GridView1.DataBind();
Response.Clear();

Response.AddHeader("content-disposition",
"attachment;filename=myFileName.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

</code>

Works everytime for me!

el***********@gmail.com wrote:
Code Monkey wrote:
What datatypes are you database columns?
If they are not nvarchars (or similar) you haven't got a hope of
getting the unicode data out.
they are nvarchar only..
The datagrid is showing all the characters..
without the line Response.ContentType="application/vnd.ms-excel"; i'm
getting correct output
Sep 29 '06 #4

P: n/a
In case you haven't spotted it...look at the ContentType line!
Code Monkey wrote:
Just an idea....

I've got similar code in one of my web-apps, but I'm using
<code>
GridView1.DataSource = myDataTable;
GridView1.DataBind();
Response.Clear();

Response.AddHeader("content-disposition",
"attachment;filename=myFileName.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

</code>

Works everytime for me!

el***********@gmail.com wrote:
Code Monkey wrote:
What datatypes are you database columns?
If they are not nvarchars (or similar) you haven't got a hope of
getting the unicode data out.
>
>
they are nvarchar only..
The datagrid is showing all the characters..
without the line Response.ContentType="application/vnd.ms-excel"; i'm
getting correct output
Sep 29 '06 #5

P: n/a
I'm using visualstudio2003..
GriedView is not available there..

Oct 3 '06 #6

P: n/a
replace GridView with DataGrid!

el***********@gmail.com wrote:
I'm using visualstudio2003..
GriedView is not available there..
Oct 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.