Java 如何解决Word文档中Package应包含内容类型部分[M1.13]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27582610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:38:40  来源:igfitidea点击:

How to solve Package should contain a content type part [M1.13] in Word Document

javaapache-poi

提问by lucifer

I am trying to create a header in word file using Apache POI but i am getting, Package should contain a content type part [M1.13] exception.I am using the following jars

我正在尝试使用 Apache POI 在 word 文件中创建一个标题,但我得到了,包应该包含一个内容类型部分 [M1.13] 异常。我正在使用以下 jars

dom4j-1.7-2.jar
poi-ooxml-3.9-2.jar
poi-3.9.jar
poi-scratchpad-3.9.jar
xmlbeans-2.4.0.jar
openxml4j.jar
poi-ooxml-schemas-3.9.jar

the code i am trying to run is

我试图运行的代码是

   public class TestHeader {

/**
 * @param args
 * @throws IOException
 * @throws XmlException
 */
public static void main(String[] args) throws IOException, XmlException {
    InputStream input = new FileInputStream("D:\test.docx");
    XWPFDocument document = new XWPFDocument(input);
    XWPFHeaderFooterPolicy headerPolicy = new XWPFHeaderFooterPolicy(
            document);

    XWPFHeader header = headerPolicy.getDefaultHeader();
    List<XWPFTable> table = header.getTables();

    for (XWPFTable xwpfTable : table) {
        xwpfTable.getRow(1).getCell(0).setText("aaa"); // document.setTable(0,
                                                        // xwpfTable);
        // header.insertTable(1, xwpfTable);
    }
    // System.out.println(header.getTables());
    FileOutputStream out = new FileOutputStream("D:\test.docx");
    document.write(out);
    out.close();

   }

}

This is the exception i am getting

这是我得到的例外

Exception in thread "main" org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:41)
at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:120)
at com.core.poi.TestHeader.main(TestHeader.java:24)
Caused by: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:178)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:662)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:269)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
... 2 more

Somebody please help me.

有人请帮助我。

回答by user2703801

Please check your xls file name it should not be in the form like (my.file.xsl) it should be like myfile.xsl

请检查您的 xls 文件名,它不应该是 (my.file.xsl) 之类的形式,而应该是 myfile.xsl

回答by csaszi

I get this exception too if I create a file in intellij.

如果我在 intellij 中创建文件,我也会收到此异常。

New -> file -> with word.docx name.

Instead of this we should create a .docx file with openoffice/word/libreoffice and save it, then copy it into the destination folder.

我们应该使用 openoffice/word/libreoffice 创建一个 .docx 文件并保存它,然后将其复制到目标文件夹中,而不是这样。