poi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Geroge <jamesgeor...@gmail.com>
Subject Re: Finally able to read .xls and .xlsx files, hope it will be useful to somebody
Date Wed, 14 Apr 2010 16:57:15 GMT

Hi Paul,
It was wonderful...and I edited my code as per your suggestion and works
well for all Excel versions and placed the new code below.


Paul Spencer-3 wrote:
> 
> James,
> I use the SS Usermodel instead of Excel version specific APIs. So opening
> a workbook is done with 
> 
>  import org.apache.poi.ss.usermodel.Workbook;
>  import org.apache.poi.ss.usermodel.WorkbookFactory;
>  ...
>  workbook = WorkbookFactory.create(fileInputStream);
> 
> 
> See
> http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/package-summary.html
> and
> http://poi.apache.org/spreadsheet/converting.html
> 
> Paul Spencer
> 
> On Apr 14, 2010, at 11:25 AM, James Geroge wrote:
> 
>> 
>> 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/p28245330/Test.java Test.java 
>> //....................................
> import java.io.FileInputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.util.Iterator;
> 
> import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
> 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.ss.usermodel.WorkbookFactory;
> 
> public class Test {
>   public static void main(String[] args) throws IOException,
> InvalidFormatException {
>       String fname = "C:\\SDI-XL.xls"; // or  "C:\\Test.xls"
> C:\\SDI-XL.xls
>       InputStream inp = new FileInputStream(fname);
>       Workbook wb = WorkbookFactory.create(inp); 
>       Sheet sheet = null; // sheet can be used as common for XSSF and HSSF
> WorkBook
>       sheet = wb.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);
>   }
> }
>> //....................................
>> 
>> 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: dev-unsubscribe@poi.apache.org
>> For additional commands, e-mail: dev-help@poi.apache.org
>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
> For additional commands, e-mail: dev-help@poi.apache.org
> 
> 
> 

-- 
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-tp28244172p28245330.html
Sent from the POI - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


Mime
View raw message