gpt4 book ai didi

java - Apache POI 找到行中的最后一个单元格

转载 作者:太空宇宙 更新时间:2023-11-04 14:00:01 25 4
gpt4 key购买 nike

我有以下代码,可以读取电子表格并写入管道分隔文件。行具有不同的长度。最后一个单元格不应附加管道字符(但确实如此)。我尝试过类似 https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Row.html#getLastCellNum()但无法使其发挥作用。

XSSFWorkbook wb = new XSSFWorkbook(pathandfilename);
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
XSSFSheet sheet = wb.getSheetAt(i);
String outfilename = path + "\\" + prefix + sheetname + postfix;
PrintWriter outfilewriter = new PrintWriter(outfilename, "UTF-8");
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
outfilewriter.print(cell.getStringCellValue() + "|");
}
outfilewriter.print("\n");
}
outfilewriter.close();
}
wb.close();

最佳答案

我这样修复了它:

while (rowIterator.hasNext()) {    
Row row = rowIterator.next();
List<String> recordArry = new ArrayList<String>();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
recordArry.add(cell.getStringCellValue() + "|");
}
StringBuilder recordString = new StringBuilder();
for (String elem : recordArry) {
recordString.append(elem);
}
String recordString2 = StringUtils.chomp(recordString.toString(), "|");
outfilewriter.print(recordString2);
outfilewriter.print("\n");
}

关于java - Apache POI 找到行中的最后一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29386722/

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