Hi Friends,
I was trying to read .xls and .xlsx into one java class file and able to do
it :jumping:, hope it will be useful to somebody.
Jar Files Used
http://old.nabble.com/file/p28244172/JarsUsed.jpg
Java Source Code as attachment:
http://old.nabble.com/file/p28244172/Test.java Test.java
//....................................
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
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.xssf.usermodel.XSSFWorkbook;
public class Test {
public static void main(String[] args) throws IOException {
String fname = "C:\\Test.xlsx"; // or "C:\\Test.xls" C:\\SDI-XL.xls
InputStream inp = new FileInputStream(fname);
String fileExtn = GetFileExtension(fname);
Workbook wb_xssf; //Declare XSSF WorkBook
Workbook wb_hssf; //Declare HSSF WorkBook
Sheet sheet = null; // sheet can be used as common for XSSF and HSSF
WorkBook
if (fileExtn.equalsIgnoreCase("xlsx"))
{
wb_xssf = new XSSFWorkbook(inp);
log("xlsx="+wb_xssf.getSheetName(0));
sheet = wb_xssf.getSheetAt(0);
}
if (fileExtn.equalsIgnoreCase("xls"))
{
POIFSFileSystem fs = new POIFSFileSystem(inp);
wb_hssf = new HSSFWorkbook(fs);
log("xls="+wb_hssf.getSheetName(0));
sheet = wb_hssf.getSheetAt(0);
}
Iterator rows = sheet.rowIterator(); // Now we have rows ready from
the sheet
while (rows.hasNext())
{
Row row = (Row) rows.next();
log("row#="+row.getRowNum()+"");
log("**********************");
//log(row.getPhysicalNumberOfCells()+"");
Iterator cells = row.cellIterator();
while (cells.hasNext())
{
Cell cell = (Cell) cells.next();
switch ( cell.getCellType() )
{
case Cell.CELL_TYPE_STRING:
log(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)) {
log(cell.getDateCellValue()+"");
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
log(cell.getBooleanCellValue()+"");
break;
case Cell.CELL_TYPE_FORMULA:
log(cell.getCellFormula());
break;
default:
}
}
}
inp.close();
}
private static void log(String message)
{
System.out.println(message);
}
private static String GetFileExtension(String fname2)
{
String fileName = fname2;
String fname="";
String ext="";
int mid= fileName.lastIndexOf(".");
fname=fileName.substring(0,mid);
ext=fileName.substring(mid+1,fileName.length());
return ext;
}
}
//....................................
If anybody have suggestions, please respond.
Regards,
James George.
--
View this message in context:
http://old.nabble.com/Finally-able-to-read-.xls-and-.xlsx-files%2C-hope-it-will-be-useful-to-somebody-tp28244172p28244172.html
Sent from the POI - Dev mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]