package hsenidmobile.idews.datacollector;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.RichTextString;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
public class POIExcelReader {
public POIExcelReader() {
}
public void displayFromExcel(String xlsPath) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(xlsPath);
}
catch (FileNotFoundException e) {
System.out.println("File not found in the specified path.");
e.printStackTrace();
}
try {
Workbook wb = WorkbookFactory.create(inputStream);
Sheet sheet = wb.getSheetAt(29);
Iteratorrows = sheet.rowIterator();
System.out.println("get the new sheet ?>>>>>");
while (rows.hasNext()) {
Row row = rows.next();
System.out.println();
System.out.println("Row No.: " + (row.getRowNum() + 1));
Iteratorcells = row.cellIterator(); |
while (cells.hasNext()) {
Cell cell = cells.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_FORMULA: {
switch(cell.getCachedFormulaResultType()){
case Cell.CELL_TYPE_NUMERIC:{
System.out.print(" Cell No.: " + cell.getColumnIndex());
System.out.print(" Numeric value: " + cell.getNumericCellValue());
break;
}
}
break;
}
case Cell.CELL_TYPE_NUMERIC: {
System.out.print(" Cell No.: " + cell.getColumnIndex());
System.out.print(" Numeric value: " + cell.getNumericCellValue());
break;
}
case Cell.CELL_TYPE_STRING: {
RichTextString richTextString = cell.getRichStringCellValue();
if(richTextString.getString()!=null && !richTextString.getString().trim().equals("")){
System.out.print(" Cell No.: " + cell.getColumnIndex());
System.out.print(" String value: " + richTextString.getString());
}
break;
}
default: {
// System.out.print(" Type not supported.");
break;
}
}
}
}
}
catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
POIExcelReader poiExample = new POIExcelReader();
// String xlsPath = "/home/rasika/dev/idews/Data_Vekpro_2006.xls";
String xlsPath = "/home/rasika/dev/idews/Mg_01.xls";
poiExample.displayFromExcel(xlsPath);
}
}
Wednesday, September 30, 2009
Apache Poi example
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment