That will be a problem then because HWPF - the API you will need to use to
work with the binary (OLE2CDF) files is still in development. Recently, I
tried inserting Tables into Word documents and the result was a corrupted
file. There is a method in the API - insertBefore() that promises to allow
you to insert a table, but as I said, this results I am sorry to say in a
corrupted file.

If you want to check this out for yourself, try this code;

    public void insertTable(String inputFilename, String outputFilename) {
        File inputFile = null;
        File outputFile = null;
        FileInputStream fis = null;
        FileOutputStream fos = null;
        HWPFDocument document = null;
        Range overallRange = null;
        Paragraph para = null;
        TableProperties tableProps = null;
        Table table = null;
        try {
            inputFile = new File(inputFilename);
            fis = new FileInputStream(inputFile);
            document = new HWPFDocument(fis);

            // Get the overall Range object and try to insert a Table. This
            // produced a file Word refused to open - saying it was damaged.
            overallRange = document.getOverallRange();
            table = overallRange.insertBefore(new TableProperties(3), 3);

            // Initially, I thought the problems arose as I was working with
            // the overall Range object as above. So, I got the only
Paragraph
            // that I could find encapsulated by that object and tried to
            // insert the Table before it - thus
            //para = overallRange.getParagraph(0);
            //table = para.insertBefore(new TableProperties(3), 3);
            // Still the file was corrupt.

            fis.close();
            fis = null;

            outputFile = new File(outputFilename);
            fos = new FileOutputStream(outputFile);
            document.write(fos);
        }
        catch(Exception ex) {
            System.out.println("Caught an: " + ex.getClass().getName());
            System.out.println("Message: " + ex.getMessage());
            System.out.println("Stacktrace follows........");
            ex.printStackTrace(System.out);
        }
        finally {
            if(fis != null) {
                try {
                    fis.close();
                    fis = null;
                }
                catch(IOException ioEx) {
                    // I G N O R E
                }
            }
            if(fos != null) {
                try {
                    fos.close();
                    fos = null;
                }
                catch(IOException ioEx) {
                    // I G N O R E
                }
            }
        }
    }

You will notice that there two filenames passed to this method. The first is
the path to an name of an emprty Word document that I created using Word
itself as I do not think that HWPF can yet generate a brand new document
itself - the no-arg constructor for the HWPFDocument class is protected.

Yours

Mark B


Kett wrote:
> 
> 
> 
> MSB wrote:
>> 
>> Which version of the API are you intending to use? Are you trying to
>> create binary files (.doc) or OpenXML files (.docx)?
>> 
>> Also, you do need to be aware that the API - certainly this is true of
>> HWPF, I am not so sure about XWPF - is immature and still in the early
>> stages of development. As a result, it cannot yet be used to create
>> anything other than simple documents.
>> 
>> Yours
>> 
>> Mark B
>> 
>> 
>> Kett wrote:
>>> 
>>> I am new to this forum. I want to create Word document and also wants to
>>> add some text.
>>> I tried to create .doc file. File has created successfully but while
>>> reading i'm getting exception
>>>  "java.io.IOException: Unable to read entire header; -1 bytes read;
>>> expected 512 bytes"
>>> Plz suugest me How to solve this .If i get sample code i can learn from
>>> it.
>>> Thanx
>>> 
>> 
>> 
> 
> I jzt want to create simple .doc file. With some lines and Table. I am
> using POI 3.1
> 

-- 
View this message in context: 
http://www.nabble.com/Word-file-creation-issue-tp25012234p25019952.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to