如何将我在 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 分组