java - 将流写入 Excel 文件

标签 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  

最佳答案

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

关于java - 将流写入 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15199879/

相关文章:

java - 如果我有 2 个引用类型,具有相同的值,是否意味着内存中只有 1 个对象

java - 如何从Java中的URL获取标签后的值

java - Apache POI 默认情况下锁定所有单元格。如何让所有单元格解锁?

apache-poi - apache poi cellIterator 跳过空白单元格但不在第一行

java - Akka添加slf4j时出现ClassNotFoundException

java - String equalsIgnoreCase() 与 JDK 7 Objects.equals(Object, Object)

java - 升级Android项目以使用java 8并超出GC开销限制

SVN 命令返回以下错误

svn - 将新的可信 CA 添加到 Netbeans SVN 管理器

java - 读取JTextfield放入word文件