如何组,我的分组数据的每一部分都在Excel和输出到单个数据Excel表?的Java 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
是什么本代码吗?它有什么问题? – 2013-02-28 07:34:48
本的代码读取的Excel文件,提取信息,将其显示在NetBeans并且它输出到另一个excel文件(完全相同的数据)。没有问题,我只是不知道如何首先对数据进行分组,然后将每个分组的部分输出到不同的Excel表格中。例如,手机目录,您希望将不同手机的所有手机合同分组,并通过电话分组,并将每部手机的全部合同输出到不同的工作表。 – Silentdarkness 2013-02-28 07:38:17
你能给出一个预期分组的例子吗? – 2013-02-28 07:50:58