1

我目前正在将 excel 数据导入 Java 中的向量中,但是现在我想对该数据进行分组,以便将具有相同 ID 的相似数据进行分组(在我的情况下为问题编号)。

这是我的导入代码:

import java.io.File;
import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JFileChooser;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;


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:
}

任何帮助或建议将不胜感激。

4

0 回答 0