0

通过使用 Java 代码,如何直接从具有属性文件中的键的 Excel 表中获取值?我的问题是:

我有一个 xyz_en_US.properties 文件。这个属性文件包含英语语言的键和值。现在我也有一个具有键和值的 Excel 表(即单独转换为西班牙语的值,键保持为英文)。

告诉我如何在 Eclipse 中编写(java)实用程序源代码以将西班牙值检索到相应的英文键,我需要将该键和值存储在名为 resourcebundle.java 的单独文件中

这可能使用arraylist和hashmap ..吗?

4

1 回答 1

1

您可以使用Apache 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();
}

顺便说一句,只是居里想知道为什么 i18n 的 excel 表,我相信应该有一些原因。在我看来,你可以使用它自己的属性文件。像 xyz_es_ES.properties。看看这个教程

于 2013-06-25T06:12:51.823 回答