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
This commit is contained in:
Nick Burch 2008-04-05 18:01:05 +00:00
parent ec1b495a0a
commit 96d3004ed7
3 changed files with 79 additions and 7 deletions

View 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>
@ -303,6 +304,53 @@
}
</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

View File

@ -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));
}
}

View 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);