gpt4 book ai didi

java - 使用 shiftRows 按列对 Excel 进行排序- Apache POI - XmlValueDisconnectedException

转载 作者:行者123 更新时间:2023-12-01 18:59:27 25 4
gpt4 key购买 nike

我有一个包含 n 列的 XSSFWorkbook。我的要求是按第一列对整个工作表进行排序。我提到了这个link但没有得到任何有关排序的信息。

我也尝试过 here 中的代码但它给出了异常

sheet.shiftRows(row2.getRowNum(), row2.getRowNum(), -1);

我正在使用 Apache POI 3.17

有人有什么建议或解决方案吗?

最佳答案

移动列时 POI 似乎有一个错误,他们说它在 3.9 中已修复,但我使用 3.17 并且仍然存在:

Exception in thread "main" org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
at org.apache.xmlbeans.impl.values.XmlObjectBase.check_orphaned(XmlObjectBase.java:1258)
at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTRowImpl.getR(Unknown Source)
at org.apache.poi.xssf.usermodel.XSSFRow.getRowNum(XSSFRow.java:394)
...

我猜你的情况是一样的。所以我想出了另一种方法:

对行进行排序,然后创建一个新工作簿并按正确的顺序复制行。然后将此排序后的工作簿写入原始文件。

为了简单起见,我假设所有单元格值都是字符串。 (如果没有,则进行相应修改)

private static final String FILE_NAME = "/home/userName/Workspace/fileToSort.xlsx";

public static void main(String[] args) {

Workbook originalWorkbook;

//create a workbook from your file
try(FileInputStream excelFile = new FileInputStream(new File(FILE_NAME))) {
originalWorkbook = new XSSFWorkbook(excelFile);
} catch (IOException e) {
throw new RuntimeException("Couldn't open file: " + FILE_NAME);
}

Sheet originalSheet = originalWorkbook.getSheetAt(0);

// Create a SortedMap<String, Row> where the key is the value of the first column
// This will automatically sort the rows
Map<String, Row> sortedRowsMap = new TreeMap<>();

// save headerRow
Row headerRow = originalSheet.getRow(0);

Iterator<Row> rowIterator = originalSheet.rowIterator();
// skip header row as we saved it already
rowIterator.next();
// sort the remaining rows
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
sortedRowsMap.put(row.getCell(0).getStringCellValue(), row);
}

// Create a new workbook
try(Workbook sortedWorkbook = new XSSFWorkbook();
FileOutputStream out = new FileOutputStream(FILE_NAME)) {
Sheet sortedSheet = sortedWorkbook.createSheet(originalSheet.getSheetName());

// Copy all the sorted rows to the new workbook
// - header first
Row newRow = sortedSheet.createRow(0);
copyRowToRow(headerRow, newRow);
// then other rows, from row 1 up (not row 0)
int rowIndex = 1;
for(Row row : sortedRowsMap.values()) {
newRow = sortedSheet.createRow(rowIndex);
copyRowToRow(row, newRow);
rowIndex++;
}

// Write your new workbook to your file
sortedWorkbook.write(out);
} catch (Exception e) {
e.printStackTrace();
}
}


// Utility method to copy rows
private static void copyRowToRow(Row row, Row newRow) {
Iterator<Cell> cellIterator = row.cellIterator();
int cellIndex = 0;
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
Cell newCell = newRow.createCell(cellIndex);
newCell.setCellValue(cell.getStringCellValue());
cellIndex++;
}
}

我在以下文件上尝试过

   A      B
---------------
Header1 Header2
a one
c three
d four
b two

它是这样排序的:

   A      B
---------------
Header1 Header2
a one
b two
c three
d four

关于java - 使用 shiftRows 按列对 Excel 进行排序- Apache POI - XmlValueDisconnectedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49534540/

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