• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Coding Still

  • Home
  • About

Export data to excel for non-latin characters

April 14, 2011 By _tasos 7 Comments

A common feature in applications is to export data in excel. With .NET it is pretty simple with the use of the Microsoft Excel Object Library. Sadly, in asp.net this is not available in medium trust. The alternative in a web application is to render the data in a HTML table and send it to the user with a “application/vnd.ms-excel” Content Type. With asp.net you can use a DataGrid control to render the table. The code for this is all over the Internet.

For those that have tried to use this code with non-latin characters (e.g. Greek characters) you might have the non-latin characters not showing properly. And yes, it is a encoding issue!

You might have noticed in Visual Studio  that there are 2 UTF-8 options to save a file. The default encoding for Visual Studio is UTF-8 (with signature). In order to set this in your exported excel, you just have to add the line  CurrentResponse.ContentEncoding = Encoding.UTF8. But this is the UTF-8 version without the signature and this line might not do the trick.

In order to add the signature you just have to add a few bytes in the start of the response stream. The code is very simple, check it out.

Sub DataToExcel(ByVal dt As DataTable, ByVal CurrentResponse As System.Web.HttpResponse, ByVal ExcelFileName As String)
    CurrentResponse.Clear()
    CurrentResponse.Charset = ""
    CurrentResponse.ContentType = "application/vnd.ms-excel"
    CurrentResponse.AddHeader("Content-Disposition", "attachment;filename=" & ExcelFileName & ".xls")  
 
    'Here we set the correct encoding so that all characters show!
    CurrentResponse.ContentEncoding = System.Text.Encoding.UTF8
    CurrentResponse.Charset = "65001"
    Dim b As Byte() = New Byte() {&HEF, &HBB, &HBF}
    CurrentResponse.BinaryWrite(b)
 
    Dim stringWrite As New System.IO.StringWriter
    Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
    Dim dg As New System.Web.UI.WebControls.DataGrid
    dg.DataSource = dt
    dg.DataBind()
    dg.RenderControl(htmlWrite)
    CurrentResponse.Write(stringWrite.ToString)
    CurrentResponse.End()
End Sub

This function will not work if instead of a DataGrid we use the newer GridView control. If we use the GridView control you will get an error that the GridView needs to be included in a <form runat=”server”> tag. I haven’t succeeded in using a GridView, so we keep using the DataGrid control.

Here is also the C# version of the code, thanks to the comment of Ahmed

public void DataToExcel(DataTable dt, System.Web.HttpResponse CurrentResponse, string ExcelFileName)
{
	CurrentResponse.Clear();
	CurrentResponse.Charset = "";
	CurrentResponse.ContentType = "application/vnd.ms-excel";
	CurrentResponse.AddHeader("Content-Disposition", "attachment;filename=" + ExcelFileName + ".xls");
 
	//Here we set the correct encoding so that all characters show!
	CurrentResponse.ContentEncoding = System.Text.Encoding.UTF8;
	CurrentResponse.Charset = "65001";
	byte[] b = new byte[] {
		0xef,
		0xbb,
		0xbf
	};
	CurrentResponse.BinaryWrite(b);
 
	System.IO.StringWriter stringWrite = new System.IO.StringWriter();
	System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
	System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
	dg.DataSource = dt;
	dg.DataBind();
	dg.RenderControl(htmlWrite);
	CurrentResponse.Write(stringWrite.ToString);
	CurrentResponse.End();
}

Filed Under: ASP.NET Tagged With: Data backup

Reader Interactions

Comments

  1. shadowy says

    December 28, 2011 at 18:56

    Thanks a bunch, this solved my problem perfectly. I am now able to export the latin characters into excel from my ASP.NET app

    Reply
  2. themhz says

    April 4, 2012 at 10:46

    You saved my day

    Reply
  3. Ahmed Mozaly says

    September 22, 2014 at 14:32

    Thank you for your code snippet it saved my day 🙂
    This is the C# equivalent to the core lines if anyone interested

    Response.ContentEncoding = System.Text.Encoding.UTF8;
    Response.Charset = "65001";
    var b = new byte[] {
    0xef,
    0xbb,
    0xbf
    };
    Response.BinaryWrite(b);

    Reply
  4. Art says

    January 5, 2016 at 12:35

    Thank you ^_^

    Reply
  5. Nikhil says

    March 4, 2016 at 15:26

    Thanks its working for me

    Reply
  6. mahmut says

    June 13, 2017 at 09:20

    sen adamın dibisin 🙂

    Reply
  7. Alfred says

    August 22, 2017 at 16:23

    You’re the man! Thanks a lot.

    Reply

Leave a Reply to Art Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Categories

  • .NET Development
  • ASP.NET
  • Databases
  • Fun
  • IIS
  • JavaScript
  • Web Development

Tags

.NET Core Android ANTLR ASP.NET Ajax ASP.NET Core ASP.NET MVC ASP.NET Web Forms AWS Bouncy Castle Chartjs cli Client info detection Comic Continuous integration CSS Data backup Date handling Firebase Firefox addons Github HigLabo HTML5 Image manipulation jQuery JWT MySQL Nodejs Nuget OAuth Objectionjs OOP openssl Oracle ORM PHP Regular expressions SEO Social media SQL SQL Server UI/UX Url rewriting Videos Visual Studio Web design

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Secondary Sidebar

Archives

  • July 2020
  • March 2020
  • August 2019
  • December 2018
  • November 2018
  • February 2018
  • August 2016
  • June 2016
  • May 2016
  • February 2016
  • January 2016
  • August 2015
  • July 2015
  • October 2014
  • July 2014
  • November 2013
  • April 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • May 2012
  • February 2012
  • December 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010

Footer

Recent Posts

  • Anatomy of an Objection.js model
  • Check your RSA private and public keys
  • Round functions on the Nth digit
  • Send FCM Notifications in C#
  • Jwt Manager
  • Things around the web #5
  • Query JSON data as relational in MySQL
  • Create and sign JWT token with RS256 using the private key
  • Drop all database objects in Oracle
  • Create and deploy a Nuget package

Latest tweets

  • Geekiness Intensifies.. NASA used Three.js to render a real-time simulation of this week's NASA rover landing on M… https://t.co/orgkXnYj9O February 19, 2021 18:12
  • Things I Wished More Developers Knew About Databases https://t.co/h4gfq6NJgo #softwaredevelopment #databases May 3, 2020 12:52
  • How a Few Lines of Code Broke Lots of Packages https://t.co/p7ZSiLY5ca #javascript May 3, 2020 12:48
  • Can someone steal my IP address and use it as their own? https://t.co/HoQ7Z3BG69 January 24, 2020 13:27
  • Organizational complexity is the best predictor of bugs in a software module https://t.co/aUYn9hD4oa #softwaredevelopment January 13, 2020 08:24
  • http://twitter.com/codingstill

Misc Links

  • George Liatsos Blog
  • Plethora Themes
  • C# / VB Converter
  • Higlabo: .NET library for mail, DropBox, Twitter & more

Connect with me

  • GitHub
  • LinkedIn
  • RSS
  • Twitter
  • Stack Overflow

Copyright © 2021 · eleven40 Pro on Genesis Framework · WordPress · Log in