0

Situation: I've a data table which's is shown in jsp page with utf-8. There is no problem with it. When I export it to Excel,Non English values looks freaky.Exporting code:

public class DisplayExcelView extends ExcelView {
public String getMimeType() {   

    return "application/vnd.ms-excel; charset=utf-8"; //uses the Win-cp1256
}

}

It saves a xls file. When I open it with doubleclick

The file you are trying to open, blabla.xls is in different format by the file extension.blabla Do u want to open the file?

If I click yes; Non-english values looks freak.

But If I first open Excel 2007 and load DATA/From text (by importing .xls file),Excel shows a window where there is the selected charset is UTF-8. After selecting this, Non-English characters look perfect.

Isn't there a way to open the by double clicking on the .xls file and get correct non-english characters ?

Snicolas
  • 37,840
  • 15
  • 114
  • 173
xx yy
  • 11
  • 1
  • 6
  • Figure out what the default encoding is for Excel and write your file in that format. – toto2 Aug 09 '11 at 13:35
  • @toto when I import xls file, I can choose utf-8 from Excel and it works well, the point is with doubleclick of xls file doesnt give me a chance to select utf-8. where am I gonna figure it from Excel? – xx yy Aug 09 '11 at 13:54
  • Yes, by default Excel must use something else than uft-8. If you write your file from Java in that encoding than it will open correctly in Excel when you double click. It might also be possible to change the default Excel encoding to uft-8. This [link](http://stackoverflow.com/questions/508558/what-charset-does-microsoft-excel-use-when-saving-files) might be slightly relevant; I'm actually not sure that Java can write a file with the encodings they are discussing since they might be Windows specific. – toto2 Aug 09 '11 at 14:05
  • @toto return "application/vnd.ms-excel; charset=utf-8"; is this correct place to write charset for that problem? I've already tried all that stuffed that are utf-8,cp1254 etc. – xx yy Aug 09 '11 at 14:09
  • It's not just in `getMimeType`, but also wherever you write your file you must make sure that it writes in the right encoding (by default it's utf-8). You'll have to search in the classes of java.io, I forgot in which output streams you can specify the encoding (OutputStreamWriter is one such class). – toto2 Aug 09 '11 at 14:19
  • Well I'd think that since Windows internally uses UCS2, trying that one would be a good first guess if UTF-8 doesn't work. – Voo Aug 09 '11 at 14:39
  • @toto hey, I found something that in ASP.NET this code 'Response.ContentEncoding = System.Text.Encoding.Default; ' solved same problem. What is the java version of it? – xx yy Aug 10 '11 at 07:33
  • That's what I was saying above with the OutputStreamWriter, but I doubt that you want the default encoding, you should first figure out what your Excel has as default. – toto2 Aug 10 '11 at 11:51
  • @toto Excel as default is not what i want. I want utf-8 and also set utf-8,but excel says "xls is in different format blabla." after this Excel doesnt care my utf-8. But if I first open excel and then data/import xls; Excel support my utf-8 – xx yy Aug 10 '11 at 12:06
  • From searching on the web, I don't think you can do want you want. You have 2 choices: open Excel first and select utf-8 by hand when opening your file (which is what you do now), or you can write your file in another encoding than uft-8 from your Java code (you should use the encoding that Excel uses by default, which I don't know). I couldn't see any better solution from my web search, but there might be one... – toto2 Aug 10 '11 at 13:11
  • @toto spended about 4 days,couldnt find solution. selecting utf-8 by hand is working but users dont use this. my plan was selecting utf-8 with java not by hand but it is not possible I guess – xx yy Aug 10 '11 at 13:20
  • I hope you understand you can write your file from Java with non-utf-8 encoding with `OutputStreamWriter(OutputStream out, String charsetName)`. – toto2 Aug 10 '11 at 13:29
  • @toto no,cant get it, can u write full code? – xx yy Aug 10 '11 at 14:05

2 Answers2

0

It is my understanding from Microsoft tech support forums where I posed the question, that EXCEL does not support UTF-8 encoding. Not sure why. This is current as of today, August 16, 2105 and includes the recently released Excel 2016 editions on both Mac and Windows platforms.

berlin
  • 31
  • 1
  • 7
0

You can output to a text file using whichever encoding you want using:

BufferedWriter out = 
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"iso-8859-1"));

I picked iso-8859-1, but that might not be the default encoding of Excel. It's the only default charset in Java that is not of UTF type.

If you need some other charset for Windows (not iso-8859-1 or any of the utf), you'll have to look at how to import those charsets with java.nio.charset.spi.CharsetProvider (you might be better off using Google since the documentation is not very helpful).

toto2
  • 5,306
  • 21
  • 24