LStrike
LStrike

Reputation: 1648

Java: remove lines from Excel

I adapted a small piece of code, but can't get it run. I retrieve always an exception:

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

The error is thrown in this line:

wb.write(out);

I think this exception is raised, because I am not allowed to read and write on the same Workbook, but how can I initialize a new Workbook without an existing Excel-File?

Code: (which is from here (thanks to the originator): 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");
    }

}

and

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

    }

[EDIT 2016-06-21]:

I added

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

to my POM, now it produces

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

After Changing to Version 3.9 I got the old error back, strange behaviour.

[EDIT 2016-06-22] Moved my Code into a separate project. I changed to poi version 3.14

POM:

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

This is the only dependency which I included. But I get still a similar error:

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)

Upvotes: 1

Views: 1557

Answers (2)

LStrike
LStrike

Reputation: 1648

The problem is found.

It was not a problem with the code, it was a problem with the excel document itself.

After experimenting a lot I found out, that if I open the excel document in MS Excel and just save it again without editing it my code is working.

So I think the problem is to find in the generation of the excel document. (which is automatically generated by a tool)

Upvotes: 2

RBB
RBB

Reputation: 874

I had the same issue in different scenario. I have removed files (input files), and restored the older version (files snapshot - before running the piece of code). It worked for me.

Note: I have used *.xlsx formats.

Upvotes: 0

Related Questions