java - 使用 Apache POI 导出数据时印度(INR)和美国(美元)的货币格式不起作用
问题描述
我正在使用 Apache POI 导出数据,并且数据同时显示印度和美国货币。但是在应用单独的 DataFormats 之后,我也得到了应用于所有数据的相同格式(印度 INR)。
public static void main(String[] args) {
try {
System.out.println("*******Export Excel*********");
File myFile = new File("MyFirstExcel.xls");
FileOutputStream out = new FileOutputStream(myFile);
XSSFWorkbook xssfworkbook = new XSSFWorkbook();
XSSFSheet sheet = xssfworkbook.createSheet("new sheet");
XSSFCellStyle INRformatStyle = xssfworkbook.createCellStyle();
XSSFCellStyle USformatStyle = xssfworkbook.createCellStyle();
XSSFDataFormat df = xssfworkbook.createDataFormat();
USformatStyle.setDataFormat(df.getFormat("[$$-409]#,##0.00;"));
INRformatStyle.setDataFormat(df.getFormat("₹#,##0.00;"));
sheet.setColumnWidth(0, 7000);
XSSFRow row = sheet.createRow((short) 0);
XSSFCell cell = row.createCell((short) 0);
cell.setCellValue(100000.0);
cell.setCellStyle(USformatStyle);
XSSFRow row1 = sheet.createRow((short) 1);
XSSFCell cell1 = row1.createCell((short) 0);
cell1.setCellValue(100000.0);
cell1.setCellStyle(INRformatStyle);
xssfworkbook.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
预计 $100,000.00 ₹1,00,000.00
实际 $1,00,000.00 ₹1,00,000.00
解决方案
如何进行数字分组Excel
取决于Windows
系统区域设置:更改 Windows 区域设置。实际上没有通过数字格式设置它的本机选项。因此,只要您的系统告诉Excel
:“我们想要使用格式印地语(印度)”,因此将数字分组设置为,12,34,56,789
那么对于每个设置了千位分隔符的数字都是如此。
避免这种情况的唯一可能是将使用的系统区域设置设置为不格式化Hindi(India)
,因此将数字分组设置为不12,34,56,789
。
或使用一种假数字格式作为[$$-409]#\,###\,##0.00
.
...
USformatStyle.setDataFormat(df.getFormat("[$$-409]#\\,###\\,##0.00"));
...
这不是使用逗号作为千位分隔符,而是,
作为数字流中的文本。所以系统的数字分组被绕过了。
完整示例:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
class CreateExcelDigitGroupings {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
DataFormat dataformat = workbook.createDataFormat();
CellStyle defaultCurrency = workbook.createCellStyle();
defaultCurrency.setDataFormat((short)8); //see https://poi.apache.org/apidocs/dev/org/apache/poi/ss/usermodel/BuiltinFormats.html
CellStyle defaultUSDCurrency = workbook.createCellStyle();
defaultUSDCurrency.setDataFormat(dataformat.getFormat("[$$-409]#,##0.00"));
CellStyle fakeHindiCurrency = workbook.createCellStyle();
fakeHindiCurrency.setDataFormat(dataformat.getFormat("₹#\\,##\\,##\\,##0.00"));
CellStyle fakeUSDCurrency = workbook.createCellStyle();
fakeUSDCurrency.setDataFormat(dataformat.getFormat("$#\\,###\\,##0.00"));
Sheet sheet = workbook.createSheet("Sheet1");
double value = 12345678.9;
Cell cell;
int r = 0;
sheet.createRow(r).createCell(0).setCellValue("defaultCurrency");
cell = sheet.getRow(r++).createCell(1);
cell.setCellValue(value);
cell.setCellStyle(defaultCurrency);
sheet.createRow(r).createCell(0).setCellValue("defaultUSDCurrency");
cell = sheet.getRow(r++).createCell(1);
cell.setCellValue(value);
cell.setCellStyle(defaultUSDCurrency);
sheet.createRow(r).createCell(0).setCellValue("fakeHindiCurrency");
cell = sheet.getRow(r++).createCell(1);
cell.setCellValue(value);
cell.setCellStyle(fakeHindiCurrency);
sheet.createRow(r).createCell(0).setCellValue("fakeUSDCurrency");
cell = sheet.getRow(r++).createCell(1);
cell.setCellValue(value);
cell.setCellStyle(fakeUSDCurrency);
sheet.autoSizeColumn(0);
sheet.autoSizeColumn(1);
FileOutputStream out = new FileOutputStream("CreateExcelDigitGroupings.xlsx");
workbook.write(out);
workbook.close();
out.close();
}
}
结果在 Excel 中Hindi(India)
设置了格式Windows regional settings
,因此默认数字分组设置为12,34,56,789
:
如您所见,所有默认数字格式都使用系统的数字分组。但我的假美元格式有效。
结果是 OpenOffice 3.2.1 Ubuntu 18.04.1 LTS。
推荐阅读
- c# - Stackoverflow 异常自动映射器
- python - 把文件变成列表
- python - 无输出 - Jupyter Notebook
- javascript - 当我尝试在我的 NodeJs 应用程序中访问会话数据时,无法读取未定义的属性电子邮件
- spring-boot - 没有从 Spring Boot 应用程序调用多个 RestController
- azure-application-insights - 如何捕获当前时间范围
- ruby-on-rails - ruby (rails) Net::HTTPResponse 未定义的方法关闭了吗?
- unit-testing - Flutter Mockito - 模拟抛出异常
- javascript - Multipass ShaderToy 有自己的输入
- vba - 中望CAD VBA设置线条颜色