You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2008/04/05 20:01:12 UTC
svn commit: r645150 - in /poi/branches/ooxml/src:
documentation/content/xdocs/spreadsheet/quick-guide.xml
examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java
java/org/apache/poi/ss/usermodel/DateUtil.java
Author: nick
Date: Sat Apr 5 11:01:05 2008
New Revision: 645150
URL: http://svn.apache.org/viewvc?rev=645150&view=rev
Log:
Tweak DateUtil, an add cell content fetching to the quick guide documentation
Modified:
poi/branches/ooxml/src/documentation/content/xdocs/spreadsheet/quick-guide.xml
poi/branches/ooxml/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java
poi/branches/ooxml/src/java/org/apache/poi/ss/usermodel/DateUtil.java
Modified: poi/branches/ooxml/src/documentation/content/xdocs/spreadsheet/quick-guide.xml
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/documentation/content/xdocs/spreadsheet/quick-guide.xml?rev=645150&r1=645149&r2=645150&view=diff
==============================================================================
--- poi/branches/ooxml/src/documentation/content/xdocs/spreadsheet/quick-guide.xml (original)
+++ poi/branches/ooxml/src/documentation/content/xdocs/spreadsheet/quick-guide.xml Sat Apr 5 11:01:05 2008
@@ -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
Modified: poi/branches/ooxml/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java?rev=645150&r1=645149&r2=645150&view=diff
==============================================================================
--- poi/branches/ooxml/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java (original)
+++ poi/branches/ooxml/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java Sat Apr 5 11:01:05 2008
@@ -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 @@
* 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 void newSheet() throws IOException {
+ public static void newSheet() throws IOException {
Workbook[] wbs = new Workbook[] {
new HSSFWorkbook(), new XSSFWorkbook()
};
@@ -68,7 +70,7 @@
}
}
- public void newCells() throws IOException {
+ public static void newCells() throws IOException {
Workbook[] wbs = new Workbook[] {
new HSSFWorkbook(), new XSSFWorkbook()
};
@@ -97,7 +99,7 @@
}
}
- 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 @@
fileOut.close();
}
- public void iterating() {
+ public static void iterating() {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
@@ -138,19 +140,39 @@
}
}
- 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));
}
}
Modified: poi/branches/ooxml/src/java/org/apache/poi/ss/usermodel/DateUtil.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/ss/usermodel/DateUtil.java?rev=645150&r1=645149&r2=645150&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/ss/usermodel/DateUtil.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/ss/usermodel/DateUtil.java Sat Apr 5 11:01:05 2008
@@ -280,6 +280,8 @@
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);
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org