<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
==================================================================== */
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;
* 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;
}
}
- public void newSheet() throws IOException {
+ public static void newSheet() throws IOException {
Workbook[] wbs = new Workbook[] {
new HSSFWorkbook(), new XSSFWorkbook()
};
}
}
- public void newCells() throws IOException {
+ public static void newCells() throws IOException {
Workbook[] wbs = new Workbook[] {
new HSSFWorkbook(), new XSSFWorkbook()
};
}
}
- public void newDateCells() throws IOException {
+ public static void newDateCells() throws IOException {
Workbook wb = new HSSFWorkbook();
//Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
fileOut.close();
}
- public void iterating() {
+ public static void iterating() {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
}
}
- 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));
+ }
}