]> source.dussan.org Git - poi.git/commitdiff
Tweak DateUtil, an add cell content fetching to the quick guide documentation
authorNick Burch <nick@apache.org>
Sat, 5 Apr 2008 18:01:05 +0000 (18:01 +0000)
committerNick Burch <nick@apache.org>
Sat, 5 Apr 2008 18:01:05 +0000 (18:01 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@645150 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/spreadsheet/quick-guide.xml
src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java
src/java/org/apache/poi/ss/usermodel/DateUtil.java

index e4f3a7837e4722d21d492bab80ce865e7f349814..cdad1392be95ca022e4ae0571791db6278586444 100644 (file)
@@ -43,6 +43,7 @@
                     <li><link href="#CreateDateCells">How to create date cells</link></li>
                     <li><link href="#CellTypes">Working with different types of cells</link></li>
                     <li><link href="#Iterator">Iterate over rows and cells</link></li>
+                    <li><link href="#CellContents">Getting the cell contents</link></li>
                     <li><link href="#TextExtraction">Text Extraction</link></li>
                     <li><link href="#Alignment">Aligning cells</link></li>
                     <li><link href="#Borders">Working with borders</link></li>
        }
                                </source>
                 </section>
+
+                <anchor id="CellContents"/>
+                <section><title>Getting the cell contents</title>
+                                       <p>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.</p>
+                    <p>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.</p>
+                               <source>
+// 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();
+               }
+       }
+}
+                               </source>
+                </section>
+
                 <anchor id="TextExtraction"/>
                 <section><title>Text Extraction</title>
                                        <p>For most text extraction requirements, the standard
index bc86f217fc1b5f0f33ca2b1ed35cc4eb4440d7be..adedc2d9188fe061373eabaea1a6ff1c7534a7a0 100644 (file)
 ==================================================================== */
 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));
+       }
 }
index ad3ba25d837456682486b2cae21b8938b2534018..9335de35dad2578ac330e0b9ad4e8c28b11c35d7 100644 (file)
@@ -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);