2013-02-28 20 views
1

如何組,我的分組數據的每一部分都在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

+0

是什麼本代碼嗎?它有什麼問題? – 2013-02-28 07:34:48

+0

本的代碼讀取的Excel文件,提取信息,將其顯示在NetBeans並且它輸出到另一個excel文件(完全相同的數據)。沒有問題,我只是不知道如何首先對數據進行分組,然後將每個分組的部分輸出到不同的Excel表格中。例如,手機目錄,您希望將不同手機的所有手機合同分組,並通過電話分組,並將每部手機的全部合同輸出到不同的工作表。 – Silentdarkness 2013-02-28 07:38:17

+0

你能給出一個預期分組的例子嗎? – 2013-02-28 07:50:58

回答

0

我還沒有看過分組親但還是希望引起你的注意。

file.close(); 

java.io.FileInputStream.close()此方法的文檔:

關閉該文件輸入流並釋放與該流相關聯的所有系統資源 。如果此流具有關聯的頻道 ,那麼該頻道也會關閉。

這可能會關閉XSSFWorkbook實例workbook並使其無法使用。

+1

有我的輸出沒有問題,它輸出到文件罰款,我的問題是在定義的問題和你以前的評論:) – Silentdarkness 2013-02-28 07:45:12

0

有一個單獨的數據集合爲一組,基於CELL_TYPE

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

,然後根據組選擇從數據讀取這裏假設,分配給purticular集合(即組)

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; 
     } 
+0

注意到下:)然而,這似乎並不能幫我解決我的真正問題:( – Silentdarkness 2013-02-28 07:49:16

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; 

    } 

} 
相關問題