Apache POI:串联字符串单元格中的德语单元格格式

时间:2018-09-05 06:14:42

标签: java localization apache-poi locale

我有一个带有两个单元格的Excel-(xlsx-)文件:

  • A1是数值为“ 5.3”的数字
  • B1是公式=“ Number:”&A1

当我在德语LibreOffice或Excel中打开此Excel文件时,都向我显示带逗号分隔符的单元格内容:

  • A1:5,3
  • B1:数字:5,3

当我用Apache POI(版本3.17; new DataFormatter(Locale.GERMAN).formatCellValue(cell, formulaEvaluator))读取内容时,数字单元格A1的格式正确为“ 5,3”,但是连接单元格B1的格式意外地以点作为分隔符

这是我的示例代码:

import java.io.File;
import java.util.Locale;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class MiniPoiTest {

  public static void main(String[] args) throws Exception {
    // doesn't change anything
    // LocaleUtil.setUserLocale(Locale.GERMAN);

    // read workbook
    File xlsxFile = new File("string-concat.xlsx");
    Workbook workbook = WorkbookFactory.create(xlsxFile);
    FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
    formulaEvaluator.clearAllCachedResultValues();
    Sheet sheet = workbook.getSheetAt(0);

    // read two cells
    Row row = sheet.getRow(0);
    Cell numericCell = row.getCell(0);
    Cell concatenatedCell = row.getCell(1);

    // print numeric cell
    String numericCellValue = new DataFormatter(Locale.GERMAN).formatCellValue(numericCell,
            formulaEvaluator);
    System.out.println("Value in " + numericCell.getAddress().formatAsString()
            + " (numeric cell): '" + numericCellValue + "'");

    // print concatenated cell
    String concatenatedCellValue = new DataFormatter(Locale.GERMAN)
            .formatCellValue(concatenatedCell, formulaEvaluator);
    System.out.println("Value in " + concatenatedCell.getAddress().formatAsString()
            + " (concatenated cell): '" + concatenatedCellValue + "'");
    System.out.println("Formula in " + concatenatedCell.getAddress().formatAsString()
            + " (concatenated cell): '" + concatenatedCell.getCellFormula() + "'");
  }

}

执行示例代码时的结果:

Value in A1 (numeric cell): '5,3'
Value in B1 (concatenated cell): 'Number: 5.3'
Formula in B1 (concatenated cell): '"Number: "&A1'

是否有一些技巧来获取还用德语数字分隔符(“ Number:5,3”)格式化的B1的串联内容?

0 个答案:

没有答案