gpt4 book ai didi

Java:从 Excel 中删除行

转载 作者:行者123 更新时间:2023-11-30 02:53:30 24 4
gpt4 key购买 nike

我改编了一小段代码,但无法运行。我总是检索到异常:

Fail to save: an error occurs while saving the package : duplicate entry: docProps/core.xml

此行抛出错误:

wb.write(out);

我认为会引发此异常,因为我不允许在同一工作簿上读取和写入,但如何在没有现有 Excel 文件的情况下初始化新工作簿?

代码:(来自这里(感谢发起者):How to remove a row using apache poi)

public class TestExcel{

public static void main(String[] args) {
ExcelLineRemover elr = new ExcelLineRemover();
elr.lineRemover("testFiles/Test_orig.xlsx", "testFiles/Test_mod.xlsx");
}

}

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class ExcelLineRemover {

public void lineRemover(String fileInName, String fileOutName) {

try {

File file = new File(fileInName);

Workbook wb = WorkbookFactory.create(file);
Sheet sheet = wb.getSheetAt(0);
removeRow(sheet, 3);

File fileOut = new File(fileOutName);
OutputStream out = new FileOutputStream(fileOut);
wb.write(out);
out.flush();
out.close();

} catch (Exception e) {
e.printStackTrace();
}

}

public void removeRow(Sheet sheet, int rowIndex) {
int lastRowNum = sheet.getLastRowNum();
if (rowIndex >= 0 && rowIndex < lastRowNum) {
sheet.shiftRows(rowIndex + 1, lastRowNum, -1);
}
if (rowIndex == lastRowNum) {
Row removingRow = sheet.getRow(rowIndex);
if (removingRow != null) {
sheet.removeRow(removingRow);
}
}
}

}

[编辑2016-06-21]:

我添加

<dependency> 
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>

到我的 POM,现在它生成了

"Exception in thread "main" java.lang.NoSuchMethodError: org.apache.poi.util.POILogger.log(I[Ljava/lang/Object;)V"

更改为版本 3.9 后,我又出现了旧错误,奇怪的行为。

[编辑2016-06-22]将我的代码移至一个单独的项目中。我改成了poi版本3.14

POM:

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>

这是我包含的唯一依赖项。但我仍然遇到类似的错误:

org.apache.poi.openxml4j.exceptions.InvalidOperationException: A part with the name '/docProps/core.xml' already exists : Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12] at

org.apache.poi.openxml4j.opc.OPCPackage.addPackagePart(OPCPackage.java:905) at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:444) at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1467) at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:217) at ExcelLineRemover.lineRemover(ExcelLineRemover.java:28) at test.TestExcel.main(TestExcel.java:14)

最佳答案

问题找到了。

不是代码的问题,是excel文档本身的问题。

经过大量实验后,我发现,如果我在 MS Excel 中打开 Excel 文档,然后再次保存它而不进行编辑,我的代码就可以正常工作。

所以我认为问题出在excel文档的生成上。 (由工具自动生成)

关于Java:从 Excel 中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37923858/

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