Apache POI > 处理特殊格式?
Apache POI > handling Special formats?
我正在考虑生成一个 xlsx 文件,并且,对于某些单元格,应用 "Special"(这是 Excel 中使用的确切词,在单元格格式 > 类别中)具有特定区域设置的类别。例如,我的 Excel 本地安装附带 "Social Insurance Number" 语言环境 "English (Canada)"。
我已经检查了 POI API,用谷歌搜索了一下,我对如何做到这一点感到困惑。
我试过手动创建这样的单元格(直接使用 Excel)然后使用 POI 读取它们。
如果我将 getCellStyle().getDataFormat() 应用于我的单元格,我返回的值等于或大于 164。我猜这意味着它被认为是用户定义的东西,因为 POI org.apache.poi.ss.usermodel.BuiltinFormats#FIRST_USER_DEFINED_FORMAT_INDEX常数是 164。
我想做的事情完全可以实现吗?一般来说,我什至不知道 Excel 的特殊类型在哪里定义。这些似乎是内置的。
所有 Excel
数字格式基于特殊格式模式。参见 How to control and understand settings in the Format Cells dialog box in Excel。
要获得特殊格式所需的确切格式模式,可以将特殊格式应用于单元格,然后查看相应的自定义格式。
以下为特殊格式"Social Insurance Number (CH)"-瑞士:
这是德语 Excel
"Sonderformat" 是 "Special".
这对应于自定义格式000\.00\.000\.000
。您可以通过在 Excel
的对话框 Format Cells
- Number
中简单地更改为类别 Custom
来扣除它。然后从下面的文本字段中读取模式 Type:
:
这是德语 Excel
"Benutzerdefiniert" 是 "Custom".
所以一般的方法是:首先在Excel
的对话框Format Cells
-Number
中选择你的特殊格式。然后将类别更改为 Custom
并从下面的文本字段中读取相应的模式 Type:
.
如果您有数字格式模式,可以使用 apache poi
,如下所示:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
class CreateExcelNumberFormat {
public static void main(String[] args) throws Exception {
try (Workbook workbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {
DataFormat format = workbook.createDataFormat();
CellStyle specialSIN = workbook.createCellStyle();
specialSIN.setDataFormat(format.getFormat("000\.00\.000\.000"));
Sheet sheet = workbook.createSheet();
Cell cell = sheet.createRow(0).createCell(0);
cell.setCellStyle(specialSIN);
cell.setCellValue(12345678901d);
sheet.setColumnWidth(0, 14*256);
workbook.write(fileout);
}
}
}
您的语言环境英语(加拿大)的社会保险号码必须是:
...
specialSIN.setDataFormat(format.getFormat("000 000 000"));
...
cell.setCellValue(46454286);
...
我正在考虑生成一个 xlsx 文件,并且,对于某些单元格,应用 "Special"(这是 Excel 中使用的确切词,在单元格格式 > 类别中)具有特定区域设置的类别。例如,我的 Excel 本地安装附带 "Social Insurance Number" 语言环境 "English (Canada)"。
我已经检查了 POI API,用谷歌搜索了一下,我对如何做到这一点感到困惑。
我试过手动创建这样的单元格(直接使用 Excel)然后使用 POI 读取它们。
如果我将 getCellStyle().getDataFormat() 应用于我的单元格,我返回的值等于或大于 164。我猜这意味着它被认为是用户定义的东西,因为 POI org.apache.poi.ss.usermodel.BuiltinFormats#FIRST_USER_DEFINED_FORMAT_INDEX常数是 164。
我想做的事情完全可以实现吗?一般来说,我什至不知道 Excel 的特殊类型在哪里定义。这些似乎是内置的。
所有 Excel
数字格式基于特殊格式模式。参见 How to control and understand settings in the Format Cells dialog box in Excel。
要获得特殊格式所需的确切格式模式,可以将特殊格式应用于单元格,然后查看相应的自定义格式。
以下为特殊格式"Social Insurance Number (CH)"-瑞士:
Excel
"Sonderformat" 是 "Special".
这对应于自定义格式000\.00\.000\.000
。您可以通过在 Excel
的对话框 Format Cells
- Number
中简单地更改为类别 Custom
来扣除它。然后从下面的文本字段中读取模式 Type:
:
Excel
"Benutzerdefiniert" 是 "Custom".
所以一般的方法是:首先在Excel
的对话框Format Cells
-Number
中选择你的特殊格式。然后将类别更改为 Custom
并从下面的文本字段中读取相应的模式 Type:
.
如果您有数字格式模式,可以使用 apache poi
,如下所示:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
class CreateExcelNumberFormat {
public static void main(String[] args) throws Exception {
try (Workbook workbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {
DataFormat format = workbook.createDataFormat();
CellStyle specialSIN = workbook.createCellStyle();
specialSIN.setDataFormat(format.getFormat("000\.00\.000\.000"));
Sheet sheet = workbook.createSheet();
Cell cell = sheet.createRow(0).createCell(0);
cell.setCellStyle(specialSIN);
cell.setCellValue(12345678901d);
sheet.setColumnWidth(0, 14*256);
workbook.write(fileout);
}
}
}
您的语言环境英语(加拿大)的社会保险号码必须是:
...
specialSIN.setDataFormat(format.getFormat("000 000 000"));
...
cell.setCellValue(46454286);
...