Reading Values from MS Excel


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Reading Values from MS Excel

  1. #1
    Join Date
    Oct 2003
    Posts
    1

    Reading Values from MS Excel

    In java is it possible to read values from MS Excel.
    If so could any one help me in doing this out

  2. #2
    Join Date
    Oct 2003
    Posts
    4
    Yes it is possible.

    There are certain formats (such as CSV or tab delimited) which save excel sheets as text files.

    CSV means comma separated variables.

    You can then use a file reader to read these files into your program.

    Save your excel file using one of these, open it in notepad and have a look.

    Then read up on file handling in java.

    Look up FileReader, BufferedReader and IOExceptions for a start.

  3. #3
    Join Date
    Mar 2003
    Posts
    834
    You can go one better that and use POI:

    http://jakarta.apache.org/poi/

    I had a play with it a couple of nights ago. It's really cool:
    Code:
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import java.io.FileInputStream;
    import java.io.IOException;
    
    public class Test {
    	public static void main(String[] args) throws IOException {
    		String cellName = args[0];
    		short rowNumber = getRowNumber(cellName);
    		short cellNumber = getCellNumber(cellName);
    
    		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("Test.xls"));
    		HSSFWorkbook wb = new HSSFWorkbook(fs);
    		HSSFSheet sheet = wb.getSheetAt(0);
    		HSSFRow row = sheet.getRow(rowNumber);
    		HSSFCell cell = row.getCell(cellNumber);
    		String value = cell.getStringCellValue();
    		System.out.println(cellName + " == \"" + value + "\"");
    	}
    
    	private static short getRowNumber(String cellName) {
    		String strNumber = cellName.substring(1);
    		short number = Short.parseShort(strNumber);	
    		number--;
    		return number;
    	}
    
    	private static short getCellNumber(String cellName) {
    		char letter = cellName.charAt(0);
    		short number = (short)(letter - 'A');	
    		return number;
    	}
    }
    To use this code, create a spreadsheet "Test.xls"with some text in each of the cells. Then invoke this program:

    java Test A2

    ...and it will print out the contents of cell A2.

    The program is particularly robust, so treat it carefully.
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center