gpt4 book ai didi

java 。 HSSF。 Apache -poi。如何修改代码

转载 作者:行者123 更新时间:2023-12-01 14:16:19 25 4
gpt4 key购买 nike

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]

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();

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

最佳答案

如果我清楚的话,您只想过滤第一个列字符串并单独休息。

为什么不使用一个简单的计数器来实现这一点:

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

关于 java 。 HSSF。 Apache -poi。如何修改代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18082358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com