poi生成excel文件中文乱码问题

http://ljhzzyx.blog.163.com/blog/static/3838031220091122649872/


利用POI生成excel文件,在本机Windows测试成功,发布到linux服务器上,生成的文件中文有部分是乱码。项目的编码是utf-8,数据库里的编码是gbk,一般Windows中文系统的编码是gb2312,linux系统是utf-8,用到linux服务器查到的编码是en_US.utf-8。

      经查看,出现乱码的字符是直接在程序中写的字符串字面量(就是"..."里面写中文)。可以断定,原因是自己的java程序在不同的操作系统下处理字符串字面量,使用系统默认编码不同造成的。这些字面量使用 new String("...".getBytes(), "utf-8")这样来重新构造,在linux系统下生成的excel文件就不是乱码了。

      这样处理这些字面量后,发现有一个字符串仍是乱码,查看后面的代码,发现此字符串进行了多次 "+" 操作。如下, zoneName 和 corpName 是客户端界面传过来的字符串:
String reportTypeDay = "日统计报表";
String titleStr = zoneName + corpName;
...
titleStr += reportTypeDay;
字符串 titleStr 不需要进行转码,不会出现乱码。
String reportTypeDayLeft = new String("日统计报表(".getBytes(), fileEncoding);
...
String rightBracket = ")";
...
String sheetName = corpName + reportTypeDayLeft + tableDate + rightBracket;
corpName 和 tableDate是客户端界面传过来的字符串,此处reportTypeDayLeft如果不手动转码,在生成的excel文件中就会成为乱码,具体原因尚不清楚。

      接下来,有个问题就是,这样固定编码生成文件,在Windows下就会有乱码了。因此,需要根据程序运行时系统的编码来生成文件,获得系统编码的方法如下:
public static String getSystemFileCharset(){
    Properties pro = System.getProperties();
    return pro.getProperty("file.encoding");
 }

      POI在2.5版本的时候,有cell的setEncoding的方法,但处理的不好,有时候转换不成功。在POI3.2版本之后,就取消了这样的方法,POI的开发者不想管这麻烦事了,让你们这些用我POI的人自己处理字符去吧。在非POI相关的java程序中,可以在生成文件的时候,通过OutputStream构造指定字符集的OutputStreamWriter再去写文件,这样就不用自己去转换每个字符串的编码(常用的FileWriter是根据系统编码生成文件)。POI中是使用HSSFWorkbook的write方法生成文件,未找到指定字符集生成文件的方法,实现这样的功能可能要自己扩展。











java中文乱码的解决

  在基于Java的编程中,经常会碰到汉字的处里及显示的问题,比如一大堆乱码或问号。
这是因为JAVA中默认的编码方式是UNICODE,而中国人通常使用的文件和DB都是基于GB2312或者BIG5等编码,故会出现此问题。以前我也经常为这个问题而苦恼,后来经查了些资料,终于解决了,我知道一定有很多朋友也会碰到这个问题,所以特就总结了一下,来拿出来让大家一起分享了。
1、在网页中输出中文。
JAVA在网络传输中使用的编码是"ISO-8859-1",故在输出时需要进行转化,如:
String str="中文";
str=new String(str.getBytes("GB2312"),"8859_1");
但假如在编译程序时,使用的编码是“GB2312”,且在中文平台上运行此程序,不会出现此问题,一定要注重。
2、从参数中读取中文
这正好与在网页中输出相反如:
str=new String(str.getBytes("8859_1"),"GB2312");
3、操作DB中的中文问题
一个较简单的方法是:在“控制面扳”中,把“区域”设置为“英语(美国)”。假如还会出现乱码,还可进行如下设置:
取中文时:str=new String(str.getBytes("GB2312"));
向DB中输入中文:str=new String(str.getBytes("ISO-8859-1"));

4、在jsp中的中文解决:

在“控制面扳”中,把“区域”设置为“英语(美国)”.
在JSP页面中加入:
假如还不行正常显示,则还要进行下面的转换:
如:name=new String(name.getBytes("ISO-8859-1"),"GBK");
就不会出现中文问题了。



http://blog.chinaunix.net/uid-11187-id-3040030.html

posted @   silentmuh  阅读(2730)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
Live2D
欢迎阅读『poi生成excel文件中文乱码问题』
  1. 1 Walk Thru Fire Vicetone
  2. 2 爱你 王心凌
  3. 3 Inspire Capo Productions - Serenity
  4. 4 Welcome Home Radical Face
  5. 5 粉红色的回忆 李玲玉
Walk Thru Fire - Vicetone
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Van Der Voort, Joren Johannes

作曲 : Victor Pool/Justin Gammella/Ruben Christopher den Boer/Meron Mengist/Joren van der Voort

Talk to me

Spill the secrets you've been keeping

Life cuts deep

Let me help pick up the pieces

You're not alone, I'm by your side

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

No matter what, I'll make it right

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

I'm not an angel, I'm not a saint

I've been a closed book full of mistakes

But when you're broken, when you're in pain

Oooh, ooh

I'll walk through fire with you

I'll walk through fire

I'll walk through fire with you

I'll walk through fire

You know I

Don't pretend to be a savior

But let me in, yeah

I promise nobody can break us

You're not alone, I'm by your side

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

No matter what, I'll make it right

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

I'm not an angel, I'm not a saint

I've been a closed book full of mistakes

But when you're broken, when you're in pain

Oooh, ooh

I'll walk through fire with you

I'll walk through fire with you

I'll walk through fire

I'll walk through fire with you

I'll walk through fire with you

You're not alone, I'm by your side

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

I'm not an angel, I'm not a saint

I've been a closed book full of mistakes

But when you're broken, when you're in pain

Oooh, ooh

I'll walk through fire with you

I'll walk through fire with you

I'll walk through fire

I'll walk through fire with you

I'll walk through fire with you

点击右上角即可分享
微信分享提示