0

Hi Iam trying to convert xlsx to pdf using apache.poi at first time it's work perfectly until i add some code (convert docx to pdf) which need to use 3.17 version. After that when i try to convert xlsx i got this error

java.lang.NoSuchMethodError: org.apache.poi.util.POILogger.log(ILjava/lang/Object;Ljava/lang/Throwable;)V

I don't know what happen. Anyway here is my some code to get the file

ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OPCPackage pkg = OPCPackage.open(file);
    XSSFWorkbook xls = new XSSFWorkbook(pkg);
    XSSFSheet worksheet = xls.getSheetAt(0);

i got error when OPCPackage open file(InputStream). Please any one can help me?

Lib : poi 3.17 and all dependencies, itextpdf 5.5.13

4

1 Answer 1

2

Check actual versions of your dependencies, please (e.g. with mvn dependency:tree).

I see nothing wrong with poi 3.17 and itextpdf 5.5.13. Unit test with pom below get passed.

package stackoverflow.q59892610;

import org.junit.Rule;
import org.junit.Test;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.rules.TemporaryFolder;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.UUID;

import static org.apache.poi.xssf.usermodel.XSSFWorkbookType.XLSX;
import static org.junit.Assert.*;

public class PoiTest
{
    @Rule
    public TemporaryFolder folder= new TemporaryFolder(new File("target"));
    @Test
    public void xlsTest() throws Exception
    {
        File file = new File(folder.getRoot(), UUID.randomUUID() + ".xlxs");
        String cellValue = UUID.randomUUID().toString();
        try (XSSFWorkbook xls = new XSSFWorkbook(XLSX)) {
            xls.createSheet("new sheet").createRow(0).createCell(0).setCellValue(cellValue);
            try(OutputStream out = new FileOutputStream(file)) {xls.write(out);}
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XSSFWorkbook xls = new XSSFWorkbook(file);
        XSSFSheet worksheet = xls.getSheetAt(0);
        assertEquals("cell value mismatch", cellValue, worksheet.getRow(0).getCell(0).getStringCellValue());
    }
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>stackoverflow</groupId>
  <artifactId>poi-logger</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>poi-logger</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <poi-version>3.17</poi-version>
    <itextpdf-version>5.5.13</itextpdf-version>
    <junit-version>4.13</junit-version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit-version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>${poi-version}</version>
    </dependency>
    <dependency>
      <groupId>com.itextpdf</groupId>
      <artifactId>itextpdf</artifactId>
      <version>${itextpdf-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>${poi-version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.