How to set data (number) format locale for specific cell using Apache POI (SXSSF)?

偶尔善良 提交于 2021-02-04 21:34:24

问题


The problem is very concrete: using Apache POI, I want to create cell (done), assign number format to it (done), and set format's locale (stuck here).

The code looks like this:

SXSSFWorkbook workbook = new SXSSFWorkbook(100);
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(1);
Cell cell = row.createCell(0);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(8); //currency with thousands separator and two decimal places after period
cell.setCellValue(123456.78);
//??? how to set custom locale for the cell's number format?

The problem that I'm trying to solve with custom locale is customizing thousands separator char (French's non-breaking space is OK for me).

XLSX workbooks allow such customization (update: I mean setting format locale per cell), this is achievable with both MS Office and OpenOffice. I want to do the same in code.

(Apache POI 3.12)


回答1:


In Offixe OpenXML (*.xlsx) for currency number format only the currency symbol can be localized but not the decimal separator. The decimal separator comes from Windows system locale settings of the Windows system the Excel is running on. And the thousands delimiter also defaults to the Windows system locale settings of the Windows system the Excel is running on.

In Excel this looks like:

As you see only the currency symbol can be localized.

At least the thousands delimiter can be set using a format string. So a format string could be

"#\\ ###\\ ##0.00\\ [$€-40C];[RED]\\-#\\ ###\\ ##0.00\\ [$€-40C]".

This is currency number format having localized french Euro currency symbol and space as the thousands delimiter. Because we are faking the thousands delimiter, we have to give as much digits as needed in the format string.

The decimal separator is the default, which means it comes from Windows system locale settings of the Windows system the Excel is running on. So the dot . within the format string does not means to always use a dot as decimal delimiter but to use the decimal delimiter which comes from the Windows system locale settings of the Windows system the Excel is running on. And if we would had used comma , as the thousands delimiter in the format string, this also would had used the thousands delimiter which comes from the Windows system locale settings of the Windows system the Excel is running on. And then we would had not need giving so much digits in the format string because then the thousands delimiter settings would repeat every thousands digits. So

"#,##0.00\\ [$€-40C];[RED]\\-#,##0.00\\ [$€-40C]"

would be enough.

Example:

import java.io.FileOutputStream;

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

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.streaming.SXSSFSheet;

public class CreateExcelNumberFormat {

 public static void main(String[] args) throws Exception {
  SXSSFWorkbook workbook = new SXSSFWorkbook(100);

  DataFormat dataformat = workbook.createDataFormat();
  CellStyle cellStyleCurrency = workbook.createCellStyle();
  cellStyleCurrency.setDataFormat(dataformat.getFormat("#\\ ###\\ ##0.00\\ [$€-40C];[RED]\\-#\\ ###\\ ##0.00\\ [$€-40C]")); 

  Sheet sheet = workbook.createSheet();
  Row row = sheet.createRow(1);
  Cell cell = row.createCell(0);

  cell.setCellValue(123456.78);
  cell.setCellStyle(cellStyleCurrency);

  ((SXSSFSheet)sheet).trackColumnForAutoSizing(0);
  sheet.autoSizeColumn(0);

  workbook.write(new FileOutputStream("CreateExcelNumberFormat.xlsx"));
  workbook.close();
  workbook.dispose();
 }
}

But this is not the same as the localized currency format which is usable in Libreoffice OpenDocument Spreadsheet format. This looks like:

As you see here both, the currency symbol and the language of the whole format, can be localized.

But it is that the Office OpenXML (*.xlsx) cannot store localized currency number formats. OpenDocument Spreadsheet (*.ods), which is the native format of OpenOffice/Libreoffice, can save localized currency number formats, but if Excel will open such a file, the localization will be lost.

The settings of the "Language" combo-box of OpenOffice/Libreoffice cannot be stored in *.xlsx, also not from OpenOffice/Libreoffice. Set something else than the default there in OpenOffice/Libreoffice, save the file as *.xlsx, close OpenOffice/Libreoffice, open the stored *.xlsx file in OpenOffice/Libreoffice again. You will see the "Language" is reseted to the default.



来源:https://stackoverflow.com/questions/46180941/how-to-set-data-number-format-locale-for-specific-cell-using-apache-poi-sxssf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!