我正在使用Apache POI,我正在尝试从excel工作簿中提取信息(理解这是一个.xlsx文件,我正在使用POI中的XSSF。),并将其显示在JTable上。但是,我发现这不是创建新列。我注意到,当我将它存储到新的向量中时,制作一个向量,我认为它不是为它迭代的每一行创建一个新的向量。
我如何存储这些信息?我想我必须存储vector,datatemp,然后清除它,每次找到一个新行并将其添加到数据向量。但是我该怎么做呢?我是在正确的轨道上吗?
图片:(不确定我是否因为没有10个代表而被允许发布,但如果没有,请删除)
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();
}
}
}
其他研究:
我知道我可以使用:JTableReadTableModelTask,但是,我必须使用JComponentPack v3.5,它似乎不是开源的,而且需要花钱。
注意:这是为了工作,只是我正在努力使我的工作变得更快的一个小项目,而且我是一名在Comp Sci&amp; S中追求B.S的本科生。数学。这只是一个测试文件,我将把它实现到我的主程序中并将其推送到git。
任何建议都会很有吸引力! 谢谢!
答案 0 :(得分:1)
我想我必须存储vector,datatemp,然后清除它,每次找到新行并将其添加到数据向量
不,你无法清除它,因为Vector被添加到TableModel中,所以如果你只是清除它,每一行都会引用同一个Vector。
我是在正确的轨道上吗?
是的,但您需要为每行数据创建一个新的Vector,以便每行可以引用包含列数据的不同Vector。类似的东西:
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
现在你的data
向量是向量的向量,它表示每行的向量,每行向量都有一个向量或每行的行。
编辑:
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 );
}
}