爪哇.高铁.阿帕奇-poi.如何修改代码 [英] Java. HSSF. Apache-poi. How to modificate code

查看:48
本文介绍了爪哇.高铁.阿帕奇-poi.如何修改代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据存储格式(往下看):[-]表示空白单元格,右边可以在空格之后只有 10 列.像这样:
[string0] [-] [string1] [string2] [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] [-]

如何更改此代码:

1) 只获取 [string0]

1) obtain only [string0]

2) 只获取 [string1] [string2] [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.

这篇关于爪哇.高铁.阿帕奇-poi.如何修改代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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