Creating excel files from java with a specific charset.

G

Guy

Hello

This is not a pure java question and can also be related to HTML, but
I'll try.

Ive written a java servlet that queries a database table and shows the
result as a html table. The user can also ask to receive the result as
an Excel sheet.
Im creating the Excel sheet by printing the same html table, but with
the content-type of "application/vnd.ms-excel". The Excel file is
created fine.
The problem is that the tables may contain non-english data so I want
to use a UTF-8 encoding.

I put this in my code:
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Encoding","utf-8");
And even this:
out.println("<!DOCTYPE
application/vnd.ms-excel;charset=utf-8>");

But it doesnt seem to be enough.
The non-english characters appear as garbage and the produced code
looks like this:

<Content-type: application/vnd.ms-excel;charset=utf-8>
<!DOCTYPE application/vnd.ms-excel;charset=utf-8>
<META HTTP-EQUIV="expires" CONTENT="0">
<META HTTP-EQUIV="pragma" CONTENT="NO-CACHE">
<html xmlns:eek:="urn:schemas-microsoft-com:eek:ffice:eek:ffice"
xmlns:x="urn:schemas-microsoft-com:eek:ffice:excel"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html;
charset=Windows-1254">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 10">
<link rel=File-List href="a.files/filelist.xml">
<link rel=Edit-Time-Data href="a.files/editdata.mso">
<link rel=OLE-Object-Data href="a.files/oledata.mso">
<title>Show user history</title>

Note that although the page encoding is utf-8, Excel adds another line
with the "Windows-1254" charset and thats what causing the problem.
When I change it manually everything works fine.

Is there a way I can control this charset parameter dynamically ? Or
fix the whole thing in a different way ?

Thanks

Guy
 
G

Guest

Hello

This is not a pure java question and can also be related to HTML, but I'll
try.

Ive written a java servlet that queries a database table and shows the
result as a html table. The user can also ask to receive the result as an
Excel sheet.
Im creating the Excel sheet by printing the same html table, but with the
content-type of "application/vnd.ms-excel". The Excel file is created
fine.
The problem is that the tables may contain non-english data so I want to
use a UTF-8 encoding.

I put this in my code:
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Encoding","utf-8");
And even this:
out.println("<!DOCTYPE
application/vnd.ms-excel;charset=utf-8>");

But it doesnt seem to be enough.
The non-english characters appear as garbage and the produced code looks
like this:

<Content-type: application/vnd.ms-excel;charset=utf-8> <!DOCTYPE
application/vnd.ms-excel;charset=utf-8> <META HTTP-EQUIV="expires"
CONTENT="0"> <META HTTP-EQUIV="pragma" CONTENT="NO-CACHE"> <html
xmlns:eek:="urn:schemas-microsoft-com:eek:ffice:eek:ffice"
xmlns:x="urn:schemas-microsoft-com:eek:ffice:excel"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=Windows-1254">
<meta name=ProgId content=Excel.Sheet> <meta name=Generator
content="Microsoft Excel 10"> <link rel=File-List
href="a.files/filelist.xml"> <link rel=Edit-Time-Data
href="a.files/editdata.mso"> <link rel=OLE-Object-Data
href="a.files/oledata.mso"> <title>Show user history</title>

Note that although the page encoding is utf-8, Excel adds another line
with the "Windows-1254" charset and thats what causing the problem. When I
change it manually everything works fine.

Is there a way I can control this charset parameter dynamically ? Or fix
the whole thing in a different way ?

Thanks

Guy

What library are you using to create the excel spreadsheet? POI HSSF (
http://jakarta.apache.org/poi/hssf/ ) will do most everything (except
locking cells). You can specify an encoding when opening or creating
spreadsheets. You can even create a spreadsheet and send it to a browser
without creating a physical file on the server.

BTW, Content-encoding is only for text-based documents, not binaries.

HTH,
La'ie Techie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top