use*_*370 7 java excel utf-8 cjk apache-poi
我一直在使用POI成功解析XLS和XLSX文件.但是,我无法从Excel电子表格中正确提取特殊字符,如中文或日文等UTF-8编码字符.我已经想出如何从UTF-8编码的csv或制表符分隔文件中提取数据,但是Excel文件没有运气.有人可以帮忙吗?
(编辑: 评论中的代码段)
HSSFSheet sheet = workbook.getSheet(worksheet);
HSSFEvaluationWorkbook ewb = HSSFEvaluationWorkbook.create(workbook);
while (rowCtr <= lastRow && !rowBreakOut)
{
Row row = sheet.getRow(rowCtr);//rows.next();
for (int col=firstCell; col<lastCell && !breakOut; col++) {
Cell cell;
cell = row.getCell(col,Row.RETURN_BLANK_AS_NULL);
if (ctype == Cell.CELL_TYPE_STRING) {
sValue = cell.getStringCellValue();
log.warn("String value = "+sValue);
String encoded = URLEncoder.encode(sValue, "UTF-8");
log.warn("URL-encoded with UTF-8: " + encoded);
....
Run Code Online (Sandbox Code Playgroud)
Roo*_*han 12
从Excel文件中提取波斯文本时遇到了同样的问题.我正在使用Eclipse,只需转到Project - > Properties并将"text files encoding"更改为UTF-8解决了这个问题.
小智 5
在POI中,您可以这样使用:
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(1);
// Create a new font and alter it.
Font font = wb.createFont();
font.setCharSet(FontCharset.ARABIC.getValue());
font.setFontHeightInPoints((short)24);
font.setFontName("B Nazanin");
font.setItalic(true);
font.setStrikeout(true);
// Fonts are set into a style so create a new one to use.
CellStyle style = wb.createCellStyle();
style.setFont(font);
// Create a cell and put a value in it.
Cell cell = row.createCell(1);
cell.setCellValue("????");
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
Run Code Online (Sandbox Code Playgroud)
并可以在FontCharset中使用另一个字符集
| 归档时间: |
|
| 查看次数: |
35669 次 |
| 最近记录: |