用XSSFWorkbook附加Excel列

时间:2018-12-06 09:17:05

标签: java apache-poi

我有一个Excel工作表,我想有选择地将其内容传输到列表中。该对象具有2个属性String id, String str

我想将第一列设置为id。我说对了。我还想附加第3、4、6、7列的值。例如,如果我的excel如下:

4404A01459C1    || A1 || 13 || 14 || B1 || 8 || 7

我想要4404A01459C1作为id(再次,我得到了这一部分)。然后我要13; 14; 8; 7,跳过A1和B1,用;分隔值我该如何实现?

FileInputStream inputStream = new FileInputStream("D:\\work\\calculatepi\\test.xlsx");
    Workbook workbook = new XSSFWorkbook(inputStream);

    Sheet firstSheet = workbook.getSheetAt(0);
    Iterator<Row> rowIterator = firstSheet.iterator();

    List<SampleGene> sgl=new ArrayList<SampleGene>();

    while(rowIterator.hasNext()){
        Row row  = rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();
        SampleGene sg = new SampleGene();
        sg.setId(row.getCell(0).toString());
        //need help here

        sgl.add(sg);
    }
    return null;

1 个答案:

答案 0 :(得分:1)

尝试使用StringBuilder并遍历cellIterator;将每个单元格值附加到StringBuilder。

StringBuilder sb = new StringBuilder();
while(cellIterator.hasNext())
{
sb.append(cellIterator.next().toString());
sb.append(";");
}
sg.setStr(sb.toString());