0

我的数据以一种格式存储(向下看):[-] 表示空白单元格,右侧可能只有 10 列,在空格之后。像这样:
[string0] [-] [string1] [string2] [string3] .. [string10] [-]

如何将此代码更改为:

1) 仅获取 [string0]

2) 仅获取 [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();

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

4

3 回答 3

1

如果我很清楚你只想过滤你的第一列字符串并单独休息。

为什么不为此使用一个简单的计数器:

 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 将在每次迭代中连接单行的每个单元格。

于 2013-08-06T16:53:38.843 回答
0

就像您在带有“break”的开关块中所做的那样。但我认为你想要的是:

Iterator<Cell> cellIterator = row.cellIterator();
boolean stop = false;
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());
      stop = true;
      break;
  }

  if (stop) {
    break;
  }
}

当您找到一个字符串单元格时,这将停止 while 循环,然后对下一行进行操作。使您需要打破while循环的任何可能条件。例如收集字符串列,当您发现所需的设置停止为真时,进入下一行。

于 2013-08-06T14:14:03.947 回答
0

外部链接繁忙的 HSSF 和 XSSF 功能开发人员指南

这是一个应该工作的例子。

Maven依赖:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
</dependency>

代码:

import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

public class StackOverflowQuestion18095443 {

    public static void main(String[] args) {
        if(args.length != 1) {
            System.out.println("Please specify the file name as a parameter");
            System.exit(-1);
        }
        String sfilename = args[0];
        File file = new File("C:\\Users\\student3\\" + sfilename + ".xls");
        read(file);
    }

    public static void read(File file) {
        try (InputStream in = new FileInputStream(file)) {
            HSSFDataFormatter formatter = new HSSFDataFormatter();
            Workbook workbook = WorkbookFactory.create(in);
            Sheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                StringBuilder rowText = new StringBuilder();
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    String cellAsStringValue = formatter.formatCellValue(cell);
                    rowText.append(cellAsStringValue).append(" ");
                }
                System.out.println(rowText.toString().trim());
            }
        } catch (InvalidFormatException | IOException e) {
            e.printStackTrace();
        }
    }
}

至于终止迭代,您可以有条件地从循环中中断。或者,您也可以不使用迭代器。请注意,您可以使用命名引用从行中获取单元格(这允许您按名称引用单元格,例如“A2”,就像在 Excel 中一样)或仅通过行中的列索引

于 2013-08-07T06:15:03.357 回答