Apache POI - getCellType(): Unterschied zwischen den Versionen

Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springen
K
K
Zeile 1: Zeile 1:
[[Kategorie:Java]]
 
 
[[Kategorie:Java-Codeschnipsel]]
 
[[Kategorie:Java-Codeschnipsel]]
 
Apache POI ist ein [[Framework]] zum Verarbeiten von Microsoft Dateiformaten wie Word (*.doc, *.docx), Excel (*.xsl, *.xslx) und Powerpoint (*.ppt).
 
Apache POI ist ein [[Framework]] zum Verarbeiten von Microsoft Dateiformaten wie Word (*.doc, *.docx), Excel (*.xsl, *.xslx) und Powerpoint (*.ppt).

Version vom 28. März 2018, 07:56 Uhr

Apache POI ist ein Framework zum Verarbeiten von Microsoft Dateiformaten wie Word (*.doc, *.docx), Excel (*.xsl, *.xslx) und Powerpoint (*.ppt).

Excel

Zum Feststellen welchen Typs der Inhalt einer Zelle in einem Excel-Sheet ist, wurde bis Version 3.14 folgende oder eine ähnliche switch-Anweisung in den Code geschrieben:

Cell cell = ...
CellValue cellValue = evaluator.evaluate(cell);

switch (cellValue.getCellType()) {
   case Cell.CELL_TYPE_NUMERIC:
   System.out.println(cellValue.getNumberValue());   
   break;
 
   case Cell.CELL_TYPE_STRING:
   System.out.println(cellValue.getStringValue());
   break;
   
   case Cell.CELL_TYPE_BLANK:
   System.out.println();
   break;

   case Cell.CELL_TYPE_ERROR:
   System.out.println("error");
   break;

   case Cell.CELL_TYPE_FORMULA:
   System.out.println();
   break;

   default:
   System.out.println();
   break;
}

In Version 3.15 hat sich einiges geändert, so dass nun im Interface Cell keine öffentlichen statischen finalen Variablen (Konstanten) mehr zu finden sind. Stattdessen wurden die Zelltypen gegen eine Aufzählung (enum) eingetauscht.

Der Code zum Feststellen des Inhaltstyps einer Zelle sollte nun so aussehen:

switch (cell.getCellTypeEnum()) {
   case STRING:
   System.out.println(cell.getRichStringCellValue().getString());
   break;
   
   case NUMERIC:
   if (DateUtil.isCellDateFormatted(cell)) {
      System.out.println(cell.getDateCellValue());
   } else {
      System.out.println(cell.getNumericCellValue());
   }
   break;

   case BOOLEAN:
   System.out.println(cell.getBooleanCellValue());
   break;

   case FORMULA:
   System.out.println(cell.getCellFormula());
   break;

   case BLANK:
   System.out.println();
   break;

   default:
   System.out.println();
   break;
}