将流写入excel文件

时间:2013-03-04 10:54:23

标签: java netbeans apache-poi

在下面的代码中,我从excel电子表格中流式传输数据并对其进行分组,我的问题是如何将每个分组流导出到Excel文件?

package excelgroupdata;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;


import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelGroupData {

public static void main(String args[]) throws Exception
{
//create a input stream for your excel file from which data will be read.  
    FileInputStream excelSheetInput = new FileInputStream("C:/test1.xlsx");
    //POIFSFileSystem myFileSystem = new POIFSFileSystem(excelSheetInput);
    XSSFWorkbook myWorkBook = new XSSFWorkbook(excelSheetInput);
    //get first work sheet in your excel file.
    Sheet sheet = myWorkBook.getSheetAt(0);
    //we will read data in first rows(0) second column(1)
    Iterator<Row> rowIterator = sheet.iterator();
    Row myRow = sheet.getRow(1);
    Cell myCell= myRow.getCell(0);
    Iterator<Cell> cellIterator ;
    String firstCell = myCell.getStringCellValue();
    int count =1;
    String nextCell;
    String Matter = "Matter Number: "+firstCell;
    System.out.println(Matter);
    while(rowIterator.hasNext())
    {


        myRow = sheet.getRow(count);
        cellIterator = myRow.cellIterator();
        myCell= myRow.getCell(0);
        nextCell= myCell.getStringCellValue();


        if(nextCell.equals(firstCell))
        {

            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:
                                    cell.setCellType(Cell.CELL_TYPE_STRING);
                                    System.out.print(cell.getStringCellValue() + "\t\t");
                                    break;
                                case Cell.CELL_TYPE_STRING:
                                    System.out.print(cell.getStringCellValue() + "\t\t");
                                    break;
                            }
                        }
            System.out.println();
            count++;

        }
        else
        {          
            myRow = sheet.getRow(count);
            myCell= myRow.getCell(0);
            nextCell=myCell.getStringCellValue();
            firstCell=nextCell;
            Matter = "Matter Number: "+firstCell;
            System.out.println(Matter);
        }

    }


}
}

我知道有一些错误编码的元素,但目前并不关心,一般的想法是有效的。

这里是我输入的数据以及分组输出的外观:

Matter Number: A4041222
A4041222        Sihlaba     2011/09/16      2013/09/15      2012/11/20      
Matter Number: A4041231
A4041231        Gwavu       2011/09/26      2013/09/26      2012/11/22      
Matter Number: A4041260
A4041260        Lin         2011/11/21      2013/11/20      2012/11/29      
A4041260        Lin         2011/09/16      2013/09/15      2012/11/29      
Matter Number: A4041281
A4041281        Sharma      2011/09/16      2013/09/15      2013/01/21      
Matter Number: A4041336
A4041336        Nkwankwana  2011/09/16      2013/09/15      2013/01/21      
A4041336        Nkwankwana  2011/09/16      2013/09/15      2013/01/21      
Matter Number: A4041420
A4041420        Gqozo       2011/09/22      2013/09/21      2012/07/18      
A4041420        Gqozo       2011/09/22      2013/09/21      2012/07/20      
Matter Number: A4041494
A4041494        Henneberry  2011/09/21      2013/09/20      2013/01/21      
Matter Number: A4041522
A4041522        Monepya     2011/09/16      2013/09/15      2013/01/21      
Matter Number: A4041600
A4041600        Vezi        2011/09/16      2013/09/15      2012/12/13      
Matter Number: A4041640
A4041640        Cupido      2011/09/27      2013/09/26      2012/09/25      
A4041640        Cupido      2011/09/26      2013/09/25      2012/11/27      
Matter Number: A4041644
A4041644        Mfingwana   2011/09/27      2013/09/26      2013/01/21      
A4041644        Mfingwana   2011/09/27      2013/09/27      2013/01/21      
Matter Number: A4041665
A4041665        Mafura      2011/09/29      2013/09/28      2012/12/13      
Matter Number: A4041770
A4041770        Mlangeni    2011/09/17      2013/09/16      2012/10/12      
Matter Number: A4041965
A4041965        Vukeya      2011/09/17      2013/09/17      2012/11/22      
Matter Number: A4042005
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      
Matter Number: A4042029
A4042029        Wallace     2011/09/17      2013/09/16      2013/01/21      
Matter Number: A4042188
A4042188        Khoza       2011/10/04      2013/10/04      2012/04/04      
Matter Number: A4042212
A4042212        Gocini      2011/09/30      2013/09/29      2012/10/29  

2 个答案:

答案 0 :(得分:1)

The tutorials contain examples如何使用POI创建Excel文件。对于您的情况,从“时间表”演示开始,它可能最接近您的需要。

答案 1 :(得分:1)

我编辑了我的代码,如下所示:

import java.io.BufferedWriter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.Iterator;


import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelGroupData {

public static void main(String args[]) throws Exception
{
//create a input stream for your excel file from which data will be read.  
    FileInputStream excelSheetInput = new FileInputStream("C:/book2.xlsx");
    //POIFSFileSystem myFileSystem = new POIFSFileSystem(excelSheetInput);
    XSSFWorkbook myWorkBook = new XSSFWorkbook(excelSheetInput);
    //get first work sheet in your excel file.
    Sheet sheet = myWorkBook.getSheetAt(0);
    //we will read data in first rows(0) second column(1)
    Iterator<Row> rowIterator = sheet.iterator();
    Row myRow = sheet.getRow(1);
    Cell myCell= myRow.getCell(0);
    Iterator<Cell> cellIterator ;
    String firstCell = myCell.getStringCellValue();
    int count =1;
    String nextCell;
    String Matter = "Matter Number: "+firstCell;
    String num = firstCell;
    System.out.println(Matter);
    FileWriter fWriter = null;
    BufferedWriter writer = null;
    fWriter = new FileWriter(num+".txt");
    writer = new BufferedWriter(fWriter);
    writer.write(Matter);
    writer.newLine();
    while(rowIterator.hasNext())
    {


        myRow = sheet.getRow(count);
        cellIterator = myRow.cellIterator();
        myCell= myRow.getCell(0);
        nextCell= myCell.getStringCellValue();


        if(nextCell.equals(firstCell))
        {

            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:
                                    cell.setCellType(Cell.CELL_TYPE_STRING);
                                    System.out.print(cell.getStringCellValue() + "\t\t");
                                    writer.write(cell.getStringCellValue()+ "\t");
                                    break;
                                case Cell.CELL_TYPE_STRING:
                                    System.out.print(cell.getStringCellValue() + "\t\t");
                                    writer.write(cell.getStringCellValue()+ "\t");
                                    break;
                            }
                        }
            System.out.println();
            writer.newLine();
            count++;

        }
        else
        {          
            writer.close();
            myRow = sheet.getRow(count);
            myCell= myRow.getCell(0);
            nextCell=myCell.getStringCellValue();
            firstCell=nextCell;
            Matter = "Matter Number: "+firstCell;
            num = firstCell;
            System.out.println(Matter);
            fWriter = new FileWriter(num+".txt");
            writer = new BufferedWriter(fWriter);
            writer.write(Matter);
            writer.newLine();
        }

    }


}
}

现在将每个组写入文本文件,但是它没有将最后一个组写入文本文件,但一般概念就在那里。