You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Pedro Jorge da Silva Pires <pe...@link.pt> on 2009/12/16 17:53:52 UTC

Working with docx files (XSSF )

Hi all,

I'm sure this is a basic question, but i can't find any information on POI site about docx files other than that it supports and it's in early stages.

Does POI really supports ".docx" files, or it's better to work with the old ".doc"?
I'm looking to do some replacing in word document files, and possible to add some additional data other than replacing.

I've tried to open a ".docx" file with the following code, but both throw an exception.
And can anyone send me a code example how to open a file.

InputStream a = new FileInputStream("c:\\teste.docx");
XWPFDocument doc = new XWPFDocument(a);

Exception in thread "main" org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: 'e:\temp\UserTemp\poifiles\poi-ooxml-996872636.tmp'
                at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:102)
                at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:199)
                at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:178)
                at org.apache.poi.util.PackageHelper.open(PackageHelper.java:53)
                at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:98)
                at pt.link.poi.TestPoi.main(TestPoi.java:24)


XWPFDocument doc = new XWPFDocument(XWPFDocument.openPackage("c:\\teste.docx"));
Exception in thread "main" org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: 'c:\teste.docx'
                at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:102)
                at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:199)
                at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:178)
                at org.apache.poi.POIXMLDocument.openPackage(POIXMLDocument.java:62)
                at pt.link.poi.TestPoi.main(TestPoi.java:29)

Thanks
Pedro Pires

Re: Working with docx files (XSSF )

Posted by MSB <ma...@tiscali.co.uk>.
It is not yet possible to perform a completely successful search and replace
operation with either HWPF or XWPF - I have not tested version 3.6 Final
that has only just been relased so do not accept this as the final statement
on the matter - as neither API is yet fully mature enough. It is possible,
for example, to search for an replace text using HWPF but any formatting
applied to the original will be lost if you work at the Paragraph level and
working with CharacterRuns seems to risk corrupting the file.

Further HWPF - and I have not tested XWPF at all in this regard so cannot
comment - is quite limited when it comes to creating Word documents. For
example, I have run into problems trying to apply more than one 'treatment'
to some text. By this, I mean that I could set the texts colour or I could
embolden it but if I tried to do both then HWPF threw an exception. Again,
this comment relates to versions prior to 3.6 Final and matters could well
have moved on.

If you want to work extensively with WordProcessingML Word files (.docx)
then it may be worth while taking a look at docx4j;
http://dev.plutext.org/blog/category/docx4j/. OpenOffice can be used to
process the older binary format (.doc) files and it exposes an interface
that you can manipulate using Java code. However, whilst it can open
WordProcessingML files, it cannot - at least at version 3.0 - save them in
the same format; it converts and saves them in ODF (Open Document Format).
Using OpenOffice also imposes quite an overhead, as you need to install the
application, it will execute quite slowly because you are actually
manipulating an instance of OpenOffice rather than working directly with the
file, and does not do too well in multi-user environments.

Yours

Mark B


pedro.pires wrote:
> 
> Hi all,
> 
> I'm sure this is a basic question, but i can't find any information on POI
> site about docx files other than that it supports and it's in early
> stages.
> 
> Does POI really supports ".docx" files, or it's better to work with the
> old ".doc"?
> I'm looking to do some replacing in word document files, and possible to
> add some additional data other than replacing.
> 
> I've tried to open a ".docx" file with the following code, but both throw
> an exception.
> And can anyone send me a code example how to open a file.
> 
> InputStream a = new FileInputStream("c:\\teste.docx");
> XWPFDocument doc = new XWPFDocument(a);
> 
> Exception in thread "main"
> org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open
> the specified file: 'e:\temp\UserTemp\poifiles\poi-ooxml-996872636.tmp'
>                 at
> org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:102)
>                 at
> org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:199)
>                 at
> org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:178)
>                 at
> org.apache.poi.util.PackageHelper.open(PackageHelper.java:53)
>                 at
> org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:98)
>                 at pt.link.poi.TestPoi.main(TestPoi.java:24)
> 
> 
> XWPFDocument doc = new
> XWPFDocument(XWPFDocument.openPackage("c:\\teste.docx"));
> Exception in thread "main"
> org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open
> the specified file: 'c:\teste.docx'
>                 at
> org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:102)
>                 at
> org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:199)
>                 at
> org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:178)
>                 at
> org.apache.poi.POIXMLDocument.openPackage(POIXMLDocument.java:62)
>                 at pt.link.poi.TestPoi.main(TestPoi.java:29)
> 
> Thanks
> Pedro Pires
> 
> 

-- 
View this message in context: http://old.nabble.com/Working-with-docx-files-%28XSSF-%29-tp26814454p26814875.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org