473,395 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Export to excel.

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
6 8541
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

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
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
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
I'm using visualstudio2003..
GriedView is not available there..

Oct 3 '06 #6
replace GridView with DataGrid!

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
I have an ASP page that calls ASP routines that I created that execute a database query and return the results to a recordset. I then iterate through the recordset and display the data in a table....
5
by: Maria L. | last post by:
Hi, I need to export the content of a DataGrid (in Windows application in C#), into an Excel spreadsheet. Anyone knows how to do this? Any code snippets would help! thanks a lot, Maria
2
by: Siu | last post by:
Hi, I use the following code to export and import a file Excel from resp. into a Web page with the following code: //EXPORT Response.Clear(); Response.Buffer = true; Response.ContentType =...
6
by: Elena | last post by:
I'm trying to export data to an Excel worksheet. I can export the data in the cell values perfectly. I need the code to change a header and footer for the worksheet, not for the columns. Is...
13
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"...
5
by: Simon | last post by:
Dear reader, With the export command you can export a query to Excel. By activate this command a form pop's up with the following text:
1
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this...
2
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.