Java的。 HSSF。 Apache的POI。如何modificate code [英] Java. HSSF. Apache-poi. How to modificate code

查看:255
本文介绍了Java的。 HSSF。 Apache的POI。如何modificate code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据存储在一个格式(往下看):[ - ]表示一个空白单元格,右侧可
  只有10列,空间之后。事情是这样的:结果
     [string0] [ - ] [字符串1] [字符串2] [STRING3]。[string10] [ - ]

My data is stored in a format(look down): [-] means a blank cell, on the right may be only 10 columns, after the space. Something like this:
[string0] [-] [string1] [string2] [string3] .. [string10] [-]

如何改变这种code为:

How to change this code for:

1)只获得[string0]

1) obtain only [string0]

2)只获得[字符串1] [字符串2] [STRING3]。[string10] [ - ]

2) obtain only [string1] [string2] [string3] .. [string10] [-]

    try {

    FileInputStream file = new FileInputStream("C:\\Users\\student3\\"+sfilename+".xls");

    //Get the workbook instance for XLS file
    HSSFWorkbook workbook = new HSSFWorkbook(file);

    //Get first sheet from the workbook
    HSSFSheet 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_STRING:
                    System.out.print(cell.getStringCellValue() + "\t\t");
                    list1.add(cell.getStringCellValue());
                    break;
            }
        }
        System.out.println("");
    }
    file.close();
    FileOutputStream out =
            new FileOutputStream("C:\\Users\\student3\\"+sfilename+".xls");
    workbook.write(out);
    out.close();

我不知道如何停止迭代器。他吸收所有..

I don't know how to stop Iterator. He absorbs all..

推荐答案

如果我明白你只是想过滤的第一列字符串和休息分开。

If I am clear you just want to filter your first column string and rest seperately.

为什么不干脆用一个简单的计数器这样的:

Why not you just use a simple counter for this:

 while(rowIterator.hasNext()) {
    Row row = rowIterator.next();
    String RowContent = null;
    Iterator<Cell> cellIterator = row.cellIterator();
    while(cellIterator.hasNext()) {
        Cell cell = cellIterator.next();
        RowContent=RowContent+cell.toString();
    }
    //Code for saving RowContent or printing or whatever you want for text in complete row
}

RowContent会给单个行的各单元的级联在每次迭代

RowContent will give concatenation of each cells of a single row in each iteration.

这篇关于Java的。 HSSF。 Apache的POI。如何modificate code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆