java.lang.nosuchmethoderror:org.apache.poi.util.poilogger.log(iljava/lang/object;ljava/lang/throwable;)v

vq8itlhq  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(702)

嗨,我第一次尝试用apache.poi将xlsx转换成pdf,它工作得很好,直到我添加了一些需要使用3.17版本的代码(将docx转换成pdf)。之后,当我试图转换xlsx我得到了这个错误
java.lang.nosuchmethoderror:org.apache.poi.util.poilogger.log(iljava/lang/object;ljava/lang/throwable;)v
我不知道会发生什么。不管怎样,这是我的一些代码来获取文件

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

opcpackage打开文件(inputstream)时出错。有人能帮我吗?
lib:poi3.17和所有依赖项,itextpdf 5.5.13

6ju8rftf

6ju8rftf1#

请检查依赖项的实际版本(例如 mvn dependency:tree ).
我认为poi3.17和itextpdf 5.5.13没有问题。pom以下的单元测试通过。

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>

相关问题