我使用apache poi完成了excel表的读写。但是如果我试图使用相同的apache poi下载ms-word,它会抛出错误。 java.lang.IllegalAccessError:尝试从类org.apache.poi.openxml4j.opc.ZipPackage
访问方法org.apache.poi.util.POILogger.log(ILjava / lang / Object;) <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.13</version>
</dependency>
esp.setContentType( “应用程序/ msword”);
resp.setHeader("Content-Disposition", "attachment;filename=\"" + "xx.docx" + "\"");
XWPFDocument document= new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run=paragraph.createRun();
run.setText("The endeavour started by Mohtashim, an AMU " +
"alumni, who is the founder and the managing director " +
"of Tutorials Point (I) Pvt. Ltd. He came up with the " +
"website tutorialspoint.com in year 2006 with the help" +
"of handpicked freelancers, with an array of tutorials" +
" for computer programming languages. ");
OutputStream outputStream = resp.getOutputStream();
PrintStream stream = new PrintStream(outputStream);
document.write(stream);
请给我这个问题的解决方案
答案 0 :(得分:1)
我使用他的maven依赖并编写我的测试代码
这是我的代码
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class SampleWord {
public static void main(String[] args) throws Exception{
XWPFDocument document= new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run=paragraph.createRun();
run.setText("The endeavour started by Mohtashim, an AMU " +
"alumni, who is the founder and the managing director " +
"of Tutorials Point (I) Pvt. Ltd. He came up with the " +
"website tutorialspoint.com in year 2006 with the help" +
"of handpicked freelancers, with an array of tutorials" +
" for computer programming languages. ");
FileOutputStream fileOut = new FileOutputStream("/Users/chenpengye/Documents/excel/test/test.docx");
document.write(fileOut);
document.close();
}
}
但是,没有出现任何错误;
答案 1 :(得分:0)
请在同一个版本检查你的poi; 这是我的pom.xml
<!-- poi excel 2003 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>
<!-- poi excel 2007 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
答案 2 :(得分:-1)
请检查你的jar版本。我认为是jar版本冲突引起的这个错误,请保持poi,poi-ooxml,poi-ooxml-schemas,poi-scratchpad都有相同的版本。