0

I am using Apache POI and I am trying to pull information from a excel workbook (Understanding this is a .xlsx file, I am using XSSF from the POI.), and display it onto a JTable. However, I am seeing that this is not creating new columns. I have noticed that when I am storing it into the new vector, making a vectors, it is not making a new vector, I think, for each row that it iterates through.

How would I store this information? I think I would have to store the vector, datatemp, then clear it, everytime a new row is found and add it to the data vector. But how would I do this? Am I on the right track?

Images: (Not sure if I am allowed to post this due to not having 10 rep, but if not please remove)

https://i.stack.imgur.com/AXg0v.png

https://i.stack.imgur.com/RuId5.png

    package table_testing_broken_up;

import java.awt.BorderLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class pull_information extends JFrame
{
    private static Vector dataTemp = new Vector();
    private static Vector columns = new Vector();
    private static Vector data = new Vector();
    public static DefaultTableModel model = null;
    public static JTable table = new JTable();

    public static void main(String[] args)
    {
        makeFrame();
    }

    static void makeFrame()
    {
        setData();
        JFrame frame = new JFrame("Table Test");
        frame.setSize(500, 150);
        data.addElement(dataTemp);
        model = new DefaultTableModel(data, columns);
        table.setModel(model);
        table.setAutoResizeMode(table.AUTO_RESIZE_ALL_COLUMNS);
        frame.add(table.getTableHeader(), BorderLayout.PAGE_START);
        frame.add(table);
        frame.setVisible(true);
    }

    static void setData()
    {
        try
        {
            FileInputStream file = new FileInputStream( new File("C:\\Testing\\student_employment_form.xlsx"));
            XSSFWorkbook workbook = new XSSFWorkbook(file);
            XSSFSheet sheet = workbook.getSheetAt(0);

            Iterator<Row> rowIterator = sheet.iterator();
            while(rowIterator.hasNext())
            {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                if(row.getRowNum() == 0)
                {
                    while(cellIterator.hasNext())
                    {
                        Cell cell = cellIterator.next();
                        columns.add(cell.getStringCellValue());
                        if(row.getRowNum() == 1)
                        {
                            break;
                        }
                    }
                    continue;
                }
                while(cellIterator.hasNext())
                {
                    Cell cell = cellIterator.next();
                    if(rowIterator.hasNext())
                    {
                        if(cell.getCellType() == Cell.CELL_TYPE_STRING)
                        {
                            dataTemp.add(cell.getStringCellValue());
                        }
                        else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
                        {
                            dataTemp.add(cell.getNumericCellValue());
                        }
                        //data.addElement(dataTemp);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            file.close();
        }
        catch (FileNotFoundException e)
        {
            System.out.println("File Not found");
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }

    }
}

Other research:

I know I could use: JTableReadTableModelTask, however, I would have to use JComponentPack v3.5 which seems not to be open source, and cost money.

Note: This is for work, just a small project that I am working on to make my work go faster, and I am an undergrad pursuing a B.S in Comp Sci & Math. Also this is just a testing file, I will implement this into my main program and push it to git.

Any advice would be much apperiated! Thanks!

camickr
  • 321,443
  • 19
  • 166
  • 288
Redspart
  • 3
  • 1
  • 3

1 Answers1

1

I think I would have to store the vector, datatemp, then clear it, everytime a new row is found and add it to the data vector

No, you can't clear it because the Vector is added to the TableModel so if you just clear it each row will have a reference to the same Vector.

Am I on the right track?

Yes, but you need to create a new Vector for every row of data so each row can reference a different Vector containing the column data. Something like:

dataTemp = new Vector();  // added

while(cellIterator.hasNext())
{
    Cell cell = cellIterator.next();
    if(rowIterator.hasNext())
    {
        if(cell.getCellType() == Cell.CELL_TYPE_STRING)
        {
            dataTemp.add(cell.getStringCellValue());
        }
        else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
        {
            dataTemp.add(cell.getNumericCellValue());
        }
        //data.addElement(dataTemp);
    }
    else
    {
        break;
    }
}

data.add( dataTemp ); // added

Now your data Vector is a Vector of Vectors, which represent a Vector for every row and each row Vector has a Vector or each column of the row.

Edit:

Iterator<Row> rowIterator = sheet.iterator();

while(rowIterator.hasNext())
{
    Row row = rowIterator.next();
    Iterator<Cell> cellIterator = row.cellIterator();

    if(row.getRowNum() == 0)
    {
        while(cellIterator.hasNext())
        {
            Cell cell = cellIterator.next();
            columns.add(cell.getStringCellValue());
        }
    }
    else
    {
        dataTemp = new Vector();

        while(cellIterator.hasNext())
        {
            Cell cell = cellIterator.next();

            if(cell.getCellType() == Cell.CELL_TYPE_STRING)
            {
                dataTemp.add(cell.getStringCellValue());
            }
            else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
            {
                dataTemp.add(cell.getNumericCellValue());
            }
        }

        data.add( dataTemp );
    }

}

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you very much. This works now. I think I have been programming for a bit to long. Time to take a walk! I would give you rep, but I can not :( – Redspart Jun 13 '14 at 16:15
  • @Redspart, I have never used Apache POI, but your code looks a little complicated to me. I would have thought you would have a RowIterator, and then within each row a CellIterator. Your code seems to imply that you have a RowIterator, within a CellIterator (if I'm reading the code correctly). Anwyway, I posted some code that I think might simplify your code, if my original assumption is correct. – camickr Jun 13 '14 at 16:29
  • Both `Sheet` and `Row` are `Iterable`, so _for-each_ loops are available, as seen [here](http://stackoverflow.com/a/3562214/230513). – trashgod Jun 14 '14 at 00:25