我目前正在将 excel 数据导入 Java 中的向量中,但是现在我想对该数据进行分组,以便将具有相同 ID 的相似数据进行分组(在我的情况下为问题编号)。
这是我的相关导入代码:
public class FileChooser extends javax.swing.JFrame {
private String path ="";
public FileChooser() {
initComponents();
}
private static Vector importExcelSheet(String fileName)
{
Vector cellVectorHolder = new Vector();
try
{
Workbook workBook = WorkbookFactory.create(new FileInputStream(fileName));
Sheet sheet = workBook.getSheetAt(0);
Iterator rowIter = sheet.rowIterator();
while(rowIter.hasNext())
{
XSSFRow row = (XSSFRow) rowIter.next();
Iterator cellIter = row.cellIterator();
Vector cellStoreVector=new Vector();
while(cellIter.hasNext())
{
XSSFCell cell = (XSSFCell) cellIter.next();
cellStoreVector.addElement(cell);
}
cellVectorHolder.addElement(cellStoreVector);
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return cellVectorHolder;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int option = chooser.showOpenDialog(this); // parentComponent must a component like JFrame, JDialog...
if (option == JFileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();
path = selectedFile.getAbsolutePath();
jTextField1.setText(path);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Vector dataHolder = importExcelSheet(path);
Enumeration e = dataHolder.elements(); // get all vector elements
while (e.hasMoreElements()) { // step through all vector elements
Object obj = e.nextElement();
System.out.println(obj);
}// TODO add your handling code here:
}
这将选择 excel 电子表格并将所有值提取到向量中并将它们打印出来,我想知道的是,我可以将向量中的相同数据分组并输出该数据而不是整个数据。
所以假设我有一个手机合同电子表格,我想选择三星 Galaxy s3 的所有合同而不是所有合同,我该怎么做?