我的数据以格式(向下看)存储:[ - ]表示空白单元格,右侧可能 在空间之后只有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();
我不知道如何阻止Iterator。他吸收了所有......
答案 0 :(得分: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将在每次迭代中为每行的每个单元格提供串联。
答案 1 :(得分: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循环的任何可能条件。例如,收集字符串列,当您找到所需的set stop为true时,转到下一行。
答案 2 :(得分:0)
外部链接:Busy Developers' Guide to HSSF and XSSF Features
这是一个应该有效的例子。
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();
}
}
}
至于终止迭代,你可以有条件地从循环中断开。或者,您也可以不使用迭代器。请注意,您可以使用named reference从行中获取单元格(这允许您按名称引用单元格,例如&#34; A2&#34;,就像在Excel中一样)或者仅通过行中的column index。