Results 1 to 5 of 5

Thread: Open File xls

  1. #1
    karno is offline Member
    Join Date
    Mar 2010
    Posts
    16
    Rep Power
    0

    Default Open File xls

    hi I am new in Java i had code this to open file .xls in java...
    but why i dont get all of data in the file .xls..
    any body help me.....???

    Java Code:
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.math.BigDecimal;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JFileChooser;
    import javax.swing.filechooser.FileNameExtensionFilter;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    
    /**
     *
     * @author ifnu
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException {
            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(new  File(System.getProperty("user.home")));
            chooser.setFileFilter(new FileNameExtensionFilter("Excell File (*.xls)", "xls","XLS"));
            chooser.showOpenDialog(null);
            if(chooser.getSelectedFile()!=null){
                File selectedFile = chooser.getSelectedFile();
                HSSFWorkbook workbook = new HSSFWorkbook(new        FileInputStream(selectedFile));
                HSSFSheet sheet = workbook.getSheetAt(0);
                HSSFRow row = null;
                
                for(int i =0;i<=sheet.getLastRowNum();i++){
                    row = sheet.getRow(i);
                     System.out.println(row.getCell((short)0).getRichStringCellValue().getString());
                    System.out.println(row.getCell((short)1).getRichStringCellValue().getString());
                    System.out.println(String.valueOf((int)row.getCell((short)2).getNumericCellValue()));
                    System.out.println(row.getCell((short)4).getDateCellValue());
                    System.out.println(new BigDecimal(row.getCell((short)5).getNumericCellValue()));
                   
                }
               
            }
        }
    
    }
    :confused:
    Last edited by Eranga; 03-17-2010 at 03:16 AM. Reason: added code tags

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    karno is offline Member
    Join Date
    Mar 2010
    Posts
    16
    Rep Power
    0

    Default

    how I get the number of sheet of .xls file in java ?
    i am using jdk 1.6 and POI 3.6 ...

  4. #4
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    Java Code:
    	InputStream inp = new FileInputStream("workbook.xls");
    HSSFWorkbook workbook = new HSSFWorkbook(new POIFSFileSystem(inp));
    	
    System.out.println("Number Of Sheets:" +  workbook.getNumberOfSheets() );
    		
    	int numberOfSheets = workbook.getNumberOfSheets();
    		
    	for ( int i = 0; i < numberOfSheets; i++) {
    		 HSSFSheet sheet = workbook.getSheetAt(i);
    		 System.out.println(sheet.getSheetName());
    	}

  5. #5
    karno is offline Member
    Join Date
    Mar 2010
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 03-05-2010, 04:21 AM
  2. Different Files Open in File -> Open Workspace
    By mgm2010 in forum JCreator
    Replies: 0
    Last Post: 04-11-2009, 02:14 PM
  3. how to open a file through URL
    By katie in forum New To Java
    Replies: 3
    Last Post: 07-13-2008, 02:53 AM
  4. To open an image file such as Jpeg file using JAva Program
    By itmani2020 in forum Advanced Java
    Replies: 10
    Last Post: 07-11-2008, 09:57 AM
  5. Is it possible to open a txt file?
    By jason27131 in forum New To Java
    Replies: 20
    Last Post: 08-07-2007, 01:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •