2

我正在从 db 中提取俄语字符并生成一个 excel 表。但不幸的是,在提取的 excel 表中,我必须手动将字符集更改为 "UNICODE(UTF-8)" ,否则我无法看到俄语字符。有谁知道如何通过 java 更新这个字符集(我不是要求转换值)?

让我知道我是否需要提供任何东西。

4

1 回答 1

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/

于 2013-10-23T17:43:58.220 回答