我从db中提取俄语字符并生成一个excel表。但不幸的是,在提取的excel表中,我必须手动将字符集更改为“UNICODE(UTF-8)”,否则我无法看到俄语字符。有人知道如何通过java更新这个字符集(我不是要求转换值)?
如果我需要提供任何内容,请告诉我。
答案 0 :(得分:0)
Apache POI
是一个选项吗?
Apache POI为您管理编码。返回的所有文本都是Java Unicode格式(我认为是UTF-16,而不是UTF-8)
以下是您在POI中阅读Excel工作表的方法:
try {
FileInputStream file = new FileInputStream(new File("C:\\test.xls"));
//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
System.out.println("");
}
file.close();
FileOutputStream out =
new FileOutputStream(new File("C:\\test.xls"));
workbook.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
来源:http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/