【问题标题】:How to get values directly from Excel sheet which have the keys in property files如何直接从具有属性文件中的键的 Excel 工作表中获取值
【发布时间】:2013-06-25 14:34:06
【问题描述】:

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

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

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

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

【问题讨论】:

  • 是的,这是可能的。您是在开发 Web 应用程序、移动应用程序还是控制台应用程序?
  • 请告诉我该怎么做?
  • @user2511966 :这是一个建议。 1) 将 excel 转换为 csv 2) 导入数据库 3) 选择查询

标签: java excel


【解决方案1】:

您可以使用Apache POI 读取excel文件。 Here 是 sn-p 来帮助你。

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。看看this tutorial

【讨论】:

  • 先生,你是对的!我也通过引用这些主题进行了编码。但我觉得将 proeperty 文件转换为西班牙语是手动完成的。然后使用 getbundle() 检索该西班牙语包;但我不想这样做,因为它包含 bcoz 1000 个标签。(我的上级机构也不期待它)所以考虑将 en_US 键映射到 es_ES(西班牙语)值,然后将映射的新键值 parirs 存储在新文件中resourcebundle.java.你提供的代码就像单独检索excel表的所有键值一样工作。没有使用 en_US 属性和 Excelsheet 文件进行映射。
  • 请告诉我如何将它们放入 arraylist 或 hashmap 中?
  • 你能告诉我如何在 excelsheet 中获取与属性文件的键值匹配的每个值。
猜你喜欢
  • 2023-02-07
  • 1970-01-01
  • 2022-06-20
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
相关资源
最近更新 更多