1

如何将我在 excel 中的数据分组并将分组数据的每个部分输出到单独的 excel 工作表?

我下面的代码执行以下操作: 1) 从 Excel 表 (.xlsx) 获取所有数据,并显示它。2) 将相同的数据输出到另一个有名字的路径。

任何帮助/建议将不胜感激

package javaapplication8;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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 JavaApplication8 {
private static String path = "";


public static void main(String[] args) throws IOException {

    JFrame frame = new JFrame();
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int option = chooser.showOpenDialog(frame); // parentComponent must a component like JFrame, JDialog...
    if (option == JFileChooser.APPROVE_OPTION) {
    File selectedFile = chooser.getSelectedFile();
    path = selectedFile.getAbsolutePath();

    }

    try {

FileInputStream file = new FileInputStream(new File(path));

//Get the workbook instance for XLS file
XSSFWorkbook workbook = new XSSFWorkbook(file);

//Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);

//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
    Row row = rowIterator.next();

    //For each row, iterate through each columns
    Iterator<Cell> cellIterator = row.cellIterator();
    while(cellIterator.hasNext()) {

        Cell cell = cellIterator.next();

        switch(cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                System.out.print(cell.getBooleanCellValue() + "\t\t");
                break;
            case Cell.CELL_TYPE_NUMERIC:
                System.out.print(cell.getNumericCellValue() + "\t\t");
                break;
            case Cell.CELL_TYPE_STRING:
                System.out.print(cell.getStringCellValue() + "\t\t");
                break;
        }
    }
    System.out.println("");
}
file.close();
FileOutputStream out =
    new FileOutputStream(new File("C:\\test.xlsx"));
workbook.write(out);
out.close();

} 
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
}

如果复制上述代码应该适用于任何 .xlsx 文件

我试图分组的数据样本是:

EPPACC      Cname       AbarDate    CTermDat    MDiscDat
A4041222    Sihlaba     2011/09/16  2013/09/15  2012/11/20
A4041231    Gwavu       2011/09/26  2013/09/26  2012/11/22
A4041260    Lin         2011/11/21  2013/11/20  2012/11/29
A4041260    Lin         2011/09/16  2013/09/15  2012/11/29
A4041281    Sharma      2011/09/16  2013/09/15  2013/01/21
A4041336    Nkwankwana  2011/09/16  2013/09/15  2013/01/21
A4041336    Nkwankwana  2011/09/16  2013/09/15  2013/01/21
A4041420    Gqozo       2011/09/22  2013/09/21  2012/07/18
A4041420    Gqozo       2011/09/22  2013/09/21  2012/07/20
A4041494    Henneberry  2011/09/21  2013/09/20  2013/01/21
A4041522    Monepya     2011/09/16  2013/09/15  2013/01/21
A4041600    Vezi        2011/09/16  2013/09/15  2012/12/13
A4041640    Cupido      2011/09/27  2013/09/26  2012/09/25
A4041640    Cupido      2011/09/26  2013/09/25  2012/11/27
A4041644    Mfingwana   2011/09/27  2013/09/26  2013/01/21
A4041644    Mfingwana   2011/09/27  2013/09/27  2013/01/21
A4041665    Mafura      2011/09/29  2013/09/28  2012/12/13
A4041770    Mlangeni    2011/09/17  2013/09/16  2012/10/12
A4041965    Vukeya      2011/09/17  2013/09/17  2012/11/22
A4042005    Tayerera    2011/09/17  2013/09/16  2012/11/27
A4042005    Tayerera    2011/11/11  2013/11/10  2012/11/27
A4042005    Tayerera    2011/11/11  2013/11/10  2012/11/27
A4042005    Tayerera    2011/09/17  2013/09/16  2012/11/27
A4042029    Wallace     2011/09/17  2013/09/16  2013/01/21
A4042188    Khoza       2011/10/04  2013/10/04  2012/04/04
A4042212    Gocini      2011/09/30  2013/09/29  2012/10/29

我想按 EPPACC 分组

4

3 回答 3

0

这是一个有效的解决方案。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Test {

    static ArrayList<XSSFWorkbook> grouplist = new ArrayList();

    public static void main(String[] args) throws IOException {

        JFrame frame = new JFrame();
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int option = chooser.showOpenDialog(frame); // parentComponent must a component like JFrame, JDialog...
        File file = option == JFileChooser.APPROVE_OPTION ? chooser.getSelectedFile() : null;

        try {
            if (file != null) {
                FileInputStream fis = new FileInputStream(file);

//Get the workbook instance for XLS file
                XSSFWorkbook workbook = new XSSFWorkbook(fis);

//Get first sheet from the workbook
                XSSFSheet sheet = workbook.getSheetAt(0);

//Iterate through each rows from first sheet
                Iterator<Row> rowIterator = sheet.iterator();
                Row header = rowIterator.next();//skiping columns header
                while (rowIterator.hasNext()) {
                    Row row = rowIterator.next();
                    Cell cell = row.getCell(0);
                    String EPPACC = cell.getStringCellValue();
                    int i = validateGroup(header, cell.getStringCellValue());
                    XSSFWorkbook group = grouplist.get(i);
                    XSSFSheet sh = group.getSheet(EPPACC);
                    int rc = sh.getLastRowNum() + 1;
                    //creating a row
                    XSSFRow rowx = sh.createRow(rc);
                    for (Cell c : row) {
               //Creating cells
                        int cellType = c.getCellType();
                        XSSFCell cc = rowx.createCell(c.getColumnIndex(), cellType);
                        switch (cellType) {
                            case Cell.CELL_TYPE_BOOLEAN:
                                cc.setCellValue(c.getBooleanCellValue());
                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                                cc.setCellValue(c.getNumericCellValue());
                                break;
                            case Cell.CELL_TYPE_STRING:
                                cc.setCellValue(c.getStringCellValue());
                                break;
                        }

                    }
                }
                fis.close();

                for (XSSFWorkbook book : grouplist) {
                    int lastRowNum = book.getSheetAt(0).getLastRowNum();
                    String name = book.getSheetAt(0).getSheetName();
                    FileOutputStream out = new FileOutputStream(new File(file.getParentFile() + "/" + name + ".xlsx"));
                    book.write(out);
                    out.close();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static int validateGroup(Row header, String value) throws FileNotFoundException, IOException {
        for (int i = 0; i < grouplist.size(); i++) {
            XSSFWorkbook group = grouplist.get(i);
            XSSFSheet sheet = group.getSheet(value);
            if (sheet != null) {
                //group exist.
                return i;
            }
        }
        //create a new book
        XSSFWorkbook group = new XSSFWorkbook();
        //create a new sheet
        XSSFSheet sheet = group.createSheet(value);
        XSSFRow row = sheet.createRow(0);
        Iterator<Cell> iterator = header.iterator();
        while (iterator.hasNext()) {
            Cell cell = iterator.next();
            XSSFCell c = row.createCell(cell.getColumnIndex(), cell.getCellType());
            c.setCellValue(cell.getStringCellValue());
        }
        grouplist.add(group);
        return grouplist.size() - 1;

    }

}
于 2014-12-16T09:41:01.540 回答
0

每个组都有一个单独的数据集合,假设这里基于CELL_TYPE

List<Boolean> myboolData;
List<Integer> myIntData;
List<String> myStrings;

然后根据读取的数据中的组选择,分配到特定的集合(即组)

switch(cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                myboolData.add(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                 myIntData.add(cell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_STRING:
                 myStrings.add(cell.getStringCellValue());
                break;
        }
于 2013-02-28T07:44:10.077 回答
0

我还没有研究分组问题,但想在此声明中引起您的注意。

file.close();

从此方法的文档中java.io.FileInputStream.close()

关闭此文件输入流并释放与该流关联的任何系统资源。如果此流具有关联的通道,则该通道也将关闭。

这可能会关闭XSSFWorkbook实例workbook并使其无法使用。

于 2013-02-28T07:42:54.163 回答