java 如何在 3.7 版本以上的 Apache POI 中的 HSSF 类的工作簿、工作表和单元格中设置字符编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12849913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:28:11  来源:igfitidea点击:

How to set character encode in workbook, sheet and cells of HSSF Classes in Apache POI beyond 3.7 version?

javacharacter-encodingapache-poi

提问by jRam90

It seems that the character-encoding methods for the HSSF Excel Classes have been taken out from the 3.8 release. I really need to specify the enconding on at least the cells. The API does not even register the encoding methods any more, not even as deprecated, they have left the static fields of encoding, though.

HSSF Excel Classes 的字符编码方法似乎已经从 3.8 版本中删除了。我真的需要至少在单元格上指定编码。API 甚至不再注册编码方法,甚至没有被弃用,但它们已经离开了编码的静态字段。

?How to set encoding in new POI versions?

?如何在新的 POI 版本中设置编码?

回答by jRam90

Ok, there is a method for doing this from your code for what ever your needs maybe:

好的,有一种方法可以根据您的需求从您的代码中执行此操作:

//Create the workbook, and the font
HSSFWorkbook wb;
HSSFFont wbFont;
wbFont=wb.createFont();
wbFont.setCharSet(HSSFFont.ANSI_CHARSET); //Your Character encoding goes in the parameter
//Establish cell styles
HSSFCellStyle cellStyle =wb.createCellStyle();
cellStyle.setFont(wbFont);
//We create our cells with our data and with our specified format
HSSFCell cell =null;
cell = row.createCell(1);
cell.setCellStyle(cellStyle);
cell.setCellValue("MY DATA");
//Do the rest for whatever you might need for your workbook and then you create it

Remember, these classes only work with .xls 2003

请记住,这些类仅适用于 .xls 2003