JAVA Apache POI自定义格式

我正在使用poi版本: 3.14

我访问一个Excel( .xlsx )文件

this.workbook = WorkbookFactory.create(new FileInputStream(f.getPath())); this.workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK); this.sheet = workbook.getSheetAt(0); 

以下是应用于单元格的.xlsx文件中的自定义格式。 我想在我的代码中显示单元格的值。

自定义格式

正如你可以看到在文件中可见的单元格值是“031642153283700100”。 另外,当我在这个特定的细胞clic,价值改变为“42153283700100”(数据没有自定义格式应用)。

编辑

原始单元格types是CELL_TYPE_NUMERIC。

.xlsx文件中的原始单元格 ==> 点击单元格


如何在java代码中显示格式化值“031642153283700100”?

我试过了 :

  • cell.toString()=>“42153283700100”
  • cell.getNumericCellValue()=>“42153283700100”

与以前的单元格types转换:

 cell.setCellType(Cell.CELL_TYPE_STRING); 
  • cell.getStringCellValue()=>“42153283700100”
  • cell.getRichStringCellValue()=>“42153283700100”

  • 具有formatCellValue(cell)方法的旧HSSFDataFormatter =>“421532837001000316”

对于数字格式,您应该使用CellGeneralFormatterCellNumberFormatter

例:

 import org.apache.poi.ss.usermodel.*; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.format.*; import java.io.IOException; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.FileInputStream; class ReadExcelWithFormats { public static void main(String[] args) { try { InputStream inp = new FileInputStream("workbook.xlsx"); Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { String formatstring = cell.getCellStyle().getDataFormatString(); System.out.println(formatstring); switch (cell.getCellType()) { //... case Cell.CELL_TYPE_NUMERIC: double cellvalue = cell.getNumericCellValue(); System.out.println(cellvalue); String formattedvalue = ""; if ("general".equals(formatstring.toLowerCase())) { formattedvalue = new CellGeneralFormatter().format(cellvalue); } else { formattedvalue = new CellNumberFormatter(formatstring).format(cellvalue); } System.out.println(formattedvalue); break; //... default: System.out.println(); } } } } catch (InvalidFormatException ifex) { } catch (FileNotFoundException fnfex) { } catch (IOException ioex) { } } } 

编辑

好的,我们来举个更一般的例子。 以上将无法与date(我已经知道),但也没有与所有的数字格式。 后者我认为CellNumberFormatter应该做,但它不。 不幸的是,它甚至会产生一些适当的数字格式的错误。

Excel ,数字格式最多可以包含4个以分号分隔的部分。 第一部分是数字大于0,第二部分是数字小于0,第三部分是数字等于0,第四部分是文本。

format > 0 ; format < 0 ; format = 0 ; text

由于CellNumberFormatter不能正确处理,我们应该在使用CellNumberFormatter之前CellNumberFormatter

 import org.apache.poi.ss.usermodel.*; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.format.*; import java.io.IOException; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.FileInputStream; import java.util.Date; class ReadExcelWithFormats { public static void main(String[] args) { try { InputStream inp = new FileInputStream("workbook.xlsx"); Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { String formatstring = cell.getCellStyle().getDataFormatString(); System.out.println(formatstring); switch (cell.getCellType()) { //... case Cell.CELL_TYPE_NUMERIC: String formattedvalue = ""; String[] formatstringparts = formatstring.split(";"); if (DateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); System.out.println(date); String dateformatstring = ""; if (cell.getCellStyle().getDataFormat() == 14) { //default short date without explicit formatting dateformatstring = "yyyy-MM-dd"; //default date format for this } else if (cell.getCellStyle().getDataFormat() == 22) { //default short datetime without explicit formatting dateformatstring = "yyyy-MM-dd hh:mm"; //default datetime format for this } else { //other data formats with explicit formatting dateformatstring = formatstringparts[0]; } formattedvalue = new CellDateFormatter(dateformatstring).format(date); } else { double cellvalue = cell.getNumericCellValue(); System.out.println(cellvalue); switch (formatstringparts.length) { case 4: case 3: if (cellvalue > 0) { if ("general".equals(formatstringparts[0].toLowerCase())) { formattedvalue = new CellGeneralFormatter().format(cellvalue); } else { formattedvalue = new CellNumberFormatter(formatstringparts[0]).format(cellvalue); } } if (cellvalue < 0) { if ("general".equals(formatstringparts[1].toLowerCase())) { formattedvalue = new CellGeneralFormatter().format(cellvalue); } else { formattedvalue = new CellNumberFormatter(formatstringparts[1]).format(cellvalue); } } if (cellvalue == 0) { if ("general".equals(formatstringparts[2].toLowerCase())) { formattedvalue = new CellGeneralFormatter().format(cellvalue); } else { formattedvalue = new CellNumberFormatter(formatstringparts[2]).format(cellvalue); } } break; case 2: if (cellvalue >= 0) { if ("general".equals(formatstringparts[0].toLowerCase())) { formattedvalue = new CellGeneralFormatter().format(cellvalue); } else { formattedvalue = new CellNumberFormatter(formatstringparts[0]).format(cellvalue); } } if (cellvalue < 0) { if ("general".equals(formatstringparts[1].toLowerCase())) { formattedvalue = new CellGeneralFormatter().format(cellvalue); } else { formattedvalue = new CellNumberFormatter(formatstringparts[1]).format(cellvalue); } } break; default: if ("general".equals(formatstringparts[0].toLowerCase())) { formattedvalue = new CellGeneralFormatter().format(cellvalue); } else { formattedvalue = new CellNumberFormatter(formatstringparts[0]).format(cellvalue); } } } System.out.println(formattedvalue); break; //... default: System.out.println(); } } } } catch (InvalidFormatException ifex) { } catch (FileNotFoundException fnfex) { } catch (IOException ioex) { } } } 

你可以试试这个

  row = sheet.getRow(RowNo); // RowNo on which Row cell should have appeared cell = row.getCell(CellNo); // CellNo on which Position cell should have appeared on Row cell.setCellValue("031642153283700100"); // You could also get CellFormula by using cell.getCellFormula();