How to read an Excel file, get and set the information using POI

Posted by user1399713 on Super User See other posts from Super User or by user1399713
Published on 2013-07-03T02:05:12Z Indexed on 2013/07/03 5:08 UTC
Read the original article Hit count: 375

Filed under:
|
|

I'm using Java to read a form that is in an Excel spreadsheet that the user fills in with information about geometric shape. Ex:

      Shape :_________
      Color :_________
      Area:  _________
      Perimeter:________

So far the code I have can I can read what I want in the form and print out the values of Shape, Color, Area, Perimeter.

public class RangeSetter {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    FileInputStream file = new FileInputStream(new File("test2.xls")); 
    //C:\Users\Yo\Documents
    // Setup code
    String cname = "Shape";
   HSSFWorkbook wb = new HSSFWorkbook(file); // retrieve workbook

    // Retrieve the named range
    // Will be something like "$C$10,$D$12:$D$14";
    int namedCellIdx = wb.getNameIndex(cname);
    Name aNamedCell = wb.getNameAt(namedCellIdx);

    // Retrieve the cell at the named range and test its contents
    // Will get back one AreaReference for C10, and
    //  another for D12 to D14
    AreaReference[] arefs = AreaReference.generateContiguous(aNamedCell.getRefersToFormula());
    for (int i=0; i<arefs.length; i++) {
        // Only get the corners of the Area
        // (use arefs[i].getAllReferencedCells() to get all cells)
        CellReference[] crefs = arefs[i].getAllReferencedCells();
        for (int j=0; j<crefs.length; j++) {
            // Check it turns into real stuff
            Sheet s = wb.getSheet(crefs[j].getSheetName());
            Row r = s.getRow(crefs[j].getRow());
            Cell c = r.getCell(crefs[j].getCol());
            if (c!= null ){
                switch(c.getCellType()){
                case Cell.CELL_TYPE_STRING:
                System.out.println(c.getStringCellValue());

                }


            }
        }
    }

What I want to do is to create a method that gets the that information and another that sets it. So far I can only print to the console

© Super User or respective owner

Related posts about microsoft-excel

Related posts about java