java 将流写入excel文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15199879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:49:10  来源:igfitidea点击:

Writing a stream to excel file

javanetbeansapache-poi

提问by Silentdarkness

In the below code i have streamed in data from an excel spreadsheet and grouped it, my question is how do i export each grouped stream to an excel file?

在下面的代码中,我从 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);
        }

    }


}
}

I am aware that there are some incorrectly coded elements but that is not of concern at the moment, the general idea works.

我知道有一些不正确编码的元素,但目前这并不重要,总体思路是有效的。

here is the data i stream in and how the grouped output looks:

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

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  

采纳答案by Aaron Digulla

The tutorials contain exampleshow to create Excel files with POI. For your case, start with the "Timesheet" demo, it's probably closest to what you need.

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

回答by Silentdarkness

I edited my code to look like follows:

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

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();
        }

    }


}
}

This now writes each group to a text file, however its not writing the last group to the text file but the general concept is there.

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