From: Nick Burch Date: Sat, 5 Apr 2008 18:01:05 +0000 (+0000) Subject: Tweak DateUtil, an add cell content fetching to the quick guide documentation X-Git-Tag: REL_3_5_BETA2~129 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=96d3004ed71dc050333ad7320c35d8bc524227c0;p=poi.git Tweak DateUtil, an add cell content fetching to the quick guide documentation git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@645150 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/spreadsheet/quick-guide.xml b/src/documentation/content/xdocs/spreadsheet/quick-guide.xml index e4f3a7837e..cdad1392be 100644 --- a/src/documentation/content/xdocs/spreadsheet/quick-guide.xml +++ b/src/documentation/content/xdocs/spreadsheet/quick-guide.xml @@ -43,6 +43,7 @@
  • How to create date cells
  • Working with different types of cells
  • Iterate over rows and cells
  • +
  • Getting the cell contents
  • Text Extraction
  • Aligning cells
  • Working with borders
  • @@ -303,6 +304,53 @@ } + + +
    Getting the cell contents +

    To get the contents of a cell, you first need to + know what kind of cell it is (asking a string cell + for its numeric contents will get you a + NumberFormatException for example). So, you will + want to switch on the cell's type, and then call + the appropriate getter for that cell.

    +

    In the code below, we loop over every cell + in one sheet, print out the cell's reference + (eg A3), and then the cell's contents.

    + +// import org.apache.poi.ss.usermodel.*; + +Sheet sheet1 = wb.getSheetAt(0); +for (Row row : sheet1) { + for (Cell cell : row) { + CellReference cellRef = new CellReference(row.getRowNum(), cell.getCellNum()); + System.out.print(cellRef.formatAsString()); + System.out.print(" - "); + + switch(cell.getCellType()) { + case Cell.CELL_TYPE_STRING: + System.out.println(cell.getRichStringCellValue().getString()); + break; + case Cell.CELL_TYPE_NUMERIC: + if(DateUtil.isCellDateFormatted(cell)) { + System.out.println(cell.getDateCellValue()); + } else { + System.out.println(cell.getNumericCellValue()); + } + break; + case Cell.CELL_TYPE_BOOLEAN: + System.out.println(cell.getBooleanCellValue()); + break; + case Cell.CELL_TYPE_FORMULA: + System.out.println(cell.getCellFormula()); + break; + default: + System.out.println(); + } + } +} + +
    +
    Text Extraction

    For most text extraction requirements, the standard diff --git a/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java b/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java index bc86f217fc..adedc2d918 100644 --- a/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java +++ b/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java @@ -16,18 +16,20 @@ ==================================================================== */ package org.apache.poi.ss.usermodel.examples; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; -import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CreationHelper; +import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.usermodel.XSSFWorkbook; @@ -35,7 +37,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; * Various things from the quick guide documentation */ public class FromQuickGuide { - public void newWorkbook() throws IOException { + public static void newWorkbook() throws IOException { boolean doHSSF = true; boolean doXSSF = true; @@ -53,7 +55,7 @@ public class FromQuickGuide { } } - public void newSheet() throws IOException { + public static void newSheet() throws IOException { Workbook[] wbs = new Workbook[] { new HSSFWorkbook(), new XSSFWorkbook() }; @@ -68,7 +70,7 @@ public class FromQuickGuide { } } - public void newCells() throws IOException { + public static void newCells() throws IOException { Workbook[] wbs = new Workbook[] { new HSSFWorkbook(), new XSSFWorkbook() }; @@ -97,7 +99,7 @@ public class FromQuickGuide { } } - public void newDateCells() throws IOException { + public static void newDateCells() throws IOException { Workbook wb = new HSSFWorkbook(); //Workbook wb = new XSSFWorkbook(); CreationHelper createHelper = wb.getCreationHelper(); @@ -126,7 +128,7 @@ public class FromQuickGuide { fileOut.close(); } - public void iterating() { + public static void iterating() { Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet("new sheet"); @@ -138,19 +140,39 @@ public class FromQuickGuide { } } - public void getCellContents(Sheet sheet) { + public static void getCellContents(Sheet sheet) { for (Row row : sheet) { for (Cell cell : row) { CellReference cellRef = new CellReference(row.getRowNum(), cell.getCellNum()); System.out.print(cellRef.formatAsString()); + System.out.print(" - "); switch(cell.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.println(cell.getRichStringCellValue().getString()); break; case Cell.CELL_TYPE_NUMERIC: + if(DateUtil.isCellDateFormatted(cell)) { + System.out.println(cell.getDateCellValue()); + } else { + System.out.println(cell.getNumericCellValue()); + } + break; + case Cell.CELL_TYPE_BOOLEAN: + System.out.println(cell.getBooleanCellValue()); + break; + case Cell.CELL_TYPE_FORMULA: + System.out.println(cell.getCellFormula()); + break; + default: + System.out.println(); } } } } + + public static void main(String[] args) throws Exception { + Workbook wb = WorkbookFactory.create(new FileInputStream("src/testcases/org/apache/poi/hssf/data/WithMoreVariousData.xlsx")); + getCellContents(wb.getSheetAt(0)); + } } diff --git a/src/java/org/apache/poi/ss/usermodel/DateUtil.java b/src/java/org/apache/poi/ss/usermodel/DateUtil.java index ad3ba25d83..9335de35da 100644 --- a/src/java/org/apache/poi/ss/usermodel/DateUtil.java +++ b/src/java/org/apache/poi/ss/usermodel/DateUtil.java @@ -280,6 +280,8 @@ public class DateUtil double d = cell.getNumericCellValue(); if ( DateUtil.isValidExcelDate(d) ) { CellStyle style = cell.getCellStyle(); + if(style == null) return false; + int i = style.getDataFormat(); String f = style.getDataFormatString(); bDate = isADateFormat(i, f);