作者:乔山办公网日期:
返回目录:excel表格制作
你可以在服务器响应的时候加一个
response.setContentType("application/msexcel;charset=UTF-8");
没有太多e799bee5baa6e58685e5aeb9362原因,POI就是如此;再说导出的EXCEL文件名的中文乱码问题, 导出时代码如下:
.....
this.getResponse().reset();
this.getResponse().setContentType("application/msexcel");
this.getResponse().setHeader("Content-Disposition", "inline;filename=中文名.xls");
try {
em.getExcelMutliIO(this.getResponse().getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这个时候导出去时,文件名会为乱码,解决的办法如下,在你的代码增加下列函数:
public static String toUtf8String(String s){
StringBuffer sb = new StringBuffer();
for (int i=0;i<s.length();i++){
char c = s.charAt(i);
if (c >= 0 && c <= 255){sb.append(c);}
else{
byte[] b;
try { b = Character.toString(c).getBytes("utf-8");}
catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
然后在导出时,对文件名引用该函数,代码如下:
this.getResponse().setHeader("Content-Disposition", "inline;filename=" +toUtf8String("中文文件名.xls"));
HSSFCell csCell = row.createCell((short) 1);
csCell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell编码百解度决中文高问位字节截答断
csCell.setCellValue("中文测试版");// 设置中权文