且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Apache POI自定义数据格式已修改

更新时间:2022-02-28 08:31:48

Apache poi 创建 *.xlsx 文件,就像 Excel 会存储的那样它.并且在存储中,数字格式从不本地化,而是始终为 en_US 格式.然后,只有 Excel GUI 会本地化数字格式.

Apache poi creates the *.xlsx file as Excel would store it. And in the storage the number formats never are localized but always are in en_US format. Only the Excel GUI then localizes the number format.

因此,对于存储,您的数字格式必须为

So for the storage your number format would must be

"_ [$€-nl-BE] * #,##0.00_ ;_ [$€-nl-BE] * -#,##0.00_ ;_ [$€-nl-BE] * \"-\"??_ ;_ @_ "

请注意,由于 en_US 区域设置,小数点分隔符为.,千位分隔符为 .

Note decimal separator is . and thousands separator is , because of en_US locale.

如果在本地化的 Excel GUI 中打开,则会更改为特定于语言环境的小数点分隔符和千位分隔符.

If opened in localized Excel GUI this changes then to the locale specific decimal separator and thousands separator.

但是Excel本身也不以数字格式存储诸如 nl-BE 之类的语言标签.而是使用 Windows语言代码标识符(LCID).这是 nl-BE 813 .因此,使用与 [$€-nl-BE] 相同的 [$€-813] 会更好.

But Excel itself also is not storing language tags like nl-BE in number formats. Instead it uses the Windows Language Code Identifier (LCID). This is 813 for nl-BE. So using [$€-813], which is the same as [$€-nl-BE], will be better.

以下也在我的德语 Excel 中为我工作:

Following works for me also in my German Excel:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;

class CreateExcelCustomNumberFormat {

 public static void main(String[] args) throws Exception {

  Workbook workbook = new XSSFWorkbook();
  DataFormat dataFormat = workbook.createDataFormat();
  CellStyle cellStyle = workbook.createCellStyle();
  cellStyle.setDataFormat(dataFormat.getFormat("_ [$€-813] * #,##0.00_ ;_ [$€-813] * -#,##0.00_ ;_ [$€-813] * \"-\"??_ ;_ @_ "));

  Cell cell = workbook.createSheet().createRow(0).createCell(0);
  cell.setCellValue(1000.43);
  cell.setCellStyle(cellStyle);

  FileOutputStream out = new FileOutputStream("Excel.xlsx");
  workbook.write(out);
  out.close();
  workbook.close();
 }
}

此代码已使用当前的 apache poi 4.1.2 Excel 2016 进行了测试.

This code is tested using current apache poi 4.1.2 and Excel 2016.