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)
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!