I tried to change a value of a xlsx-file which is embedded in a docx-file with apache poi. Sadly, I havent found a proper solution to this problem. After running my programm, the new created docx-file with the embedded xlsx-table hasnt changed. Here is what I have tried so far:
我试图改变xlsx文件的值,该值嵌入在apache poi的docx文件中。不幸的是,我还没有找到一个合适的方法来解决这个问题。在运行我的程序之后,新创建的带有嵌入式xlsx-table的docx文件没有更改。以下是我迄今为止所做的尝试:
FileInputStream fis = new FileInputStream("old.docx");
XWPFDocument xdoc = new XWPFDocument(OPCPackage.open(fis));
System.out.println(xdoc.getAllEmbedds().get(0));
File file2 = new File("new.docx");
for (PackagePart pPart : xdoc.getAllEmbedds()) {
String contentType = pPart.getContentType();
// Excel Workbook - OpenXML file format
if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(pPart.getInputStream());
XSSFSheet sheet = embeddedWorkbook.getSheetAt(0);
sheet.getRow(1).getCell(1).setCellValue("someValue"); //change value here
embeddedWorkbook.write(pPart.getOutputStream());
}
}
xdoc.write(new FileOutputStream(file2));
any idea how to fix this problem?
你知道怎么解决这个问题吗?
1 个解决方案
#1
1
I don't believe that this will be possible with fulfillment of all wishes. In fact, the embedded XSSFWorkbook
is updated. But what you see if you open the new.docx
is not the XSSFWorkbook
but a EMF
picture of it. This picture will only be updated if you doubleclick it to opening the embedded XSSFWorkbook
and then click outside it to close it again.
我不相信这能实现所有的愿望。事实上,嵌入的XSSFWorkbook已经更新。但是如果你打开新的。docx不是XSSFWorkbook,而是它的EMF图片。只有当您双击它打开嵌入的XSSFWorkbook,然后单击它的外部以再次关闭它时,这幅图片才会被更新。
Some other posts suggest that it will be updated while opening the Word
file if this EMF
picture is not actually within the ZIP
archive. But it will not.
其他一些文章建议,如果这个EMF图片实际上不在ZIP归档中,那么它将在打开Word文件时进行更新。但是它不会。
Try:
试一试:
for (PackagePart pPart : xdoc.getPackage().getPartsByName(Pattern.compile(".*emf$"))) {
System.out.println(pPart.getPartName());
//xdoc.getPackage().removePartRecursive(pPart.getPartName());
xdoc.getPackage().removePart(pPart.getPartName());
}
So the "solution" would be, new creating a EMF
snapshot picture form the updated XSSFWorkbook
and replace the old EMF
picture with this new one. Not really possible in my opinion.
因此,“解决方案”应该是,从更新的XSSFWorkbook创建一个EMF快照图片,并将旧的EMF图片替换为新的。在我看来不太可能。
Seems as if the program routine for creating the EMF
snapshot picture is part of the Word
application if the embedded XSSFWorkbook
is closed. But it is not part of apache-poi
until now. And of course it is not part of the XML
program routines.
似乎创建EMF快照图片的程序例程是Word应用程序的一部分,如果嵌入的XSSFWorkbook是关闭的。但直到现在它还不是阿帕奇-poi的一部分。当然,它不是XML程序例程的一部分。
#1
1
I don't believe that this will be possible with fulfillment of all wishes. In fact, the embedded XSSFWorkbook
is updated. But what you see if you open the new.docx
is not the XSSFWorkbook
but a EMF
picture of it. This picture will only be updated if you doubleclick it to opening the embedded XSSFWorkbook
and then click outside it to close it again.
我不相信这能实现所有的愿望。事实上,嵌入的XSSFWorkbook已经更新。但是如果你打开新的。docx不是XSSFWorkbook,而是它的EMF图片。只有当您双击它打开嵌入的XSSFWorkbook,然后单击它的外部以再次关闭它时,这幅图片才会被更新。
Some other posts suggest that it will be updated while opening the Word
file if this EMF
picture is not actually within the ZIP
archive. But it will not.
其他一些文章建议,如果这个EMF图片实际上不在ZIP归档中,那么它将在打开Word文件时进行更新。但是它不会。
Try:
试一试:
for (PackagePart pPart : xdoc.getPackage().getPartsByName(Pattern.compile(".*emf$"))) {
System.out.println(pPart.getPartName());
//xdoc.getPackage().removePartRecursive(pPart.getPartName());
xdoc.getPackage().removePart(pPart.getPartName());
}
So the "solution" would be, new creating a EMF
snapshot picture form the updated XSSFWorkbook
and replace the old EMF
picture with this new one. Not really possible in my opinion.
因此,“解决方案”应该是,从更新的XSSFWorkbook创建一个EMF快照图片,并将旧的EMF图片替换为新的。在我看来不太可能。
Seems as if the program routine for creating the EMF
snapshot picture is part of the Word
application if the embedded XSSFWorkbook
is closed. But it is not part of apache-poi
until now. And of course it is not part of the XML
program routines.
似乎创建EMF快照图片的程序例程是Word应用程序的一部分,如果嵌入的XSSFWorkbook是关闭的。但直到现在它还不是阿帕奇-poi的一部分。当然,它不是XML程序例程的一部分。