Until I can get back on my office/home PC, I cannot answer this question as I
have no way of compiling/testing code on the current machine. Sadly, I will
not get access to these until the end of the week - I am currently leading a
group of people building a dipping platform on a local nature reserve. This
morning, I did find some older code that attempted to create a table in a
document but it was littered with comments saying that the document was
corrupted. Here is the code which you are more than welcome to play with and
test as I have not tried it out since version 3.5 of the api and the
original problems may have been fixed
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;
CharacterRun run = null;
TableProperties tableProps = null;
Table table = null;
TableRow row = null;
TableCell cell = 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.
document.getRange();
overallRange = document.getRange();
table = overallRange.insertBefore(new TableProperties(3), 3);
for(int i = 0; i < 3; i++) {
row = table.getRow(i);
for(int j = 0; j < 3; j++) {
cell = row.getCell(j);
para = cell.getParagraph(0);
run = para.getCharacterRun(0);
run.insertBefore("How about inserting the text before
the run?");
}
}
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
}
}
}
}
HWPF is not able to create a new Word document - at least that used to be
the case when this was written. So, you need to use Word to create an empty
document, save that away on your machine and then open it using HWPF. That
is why this method accepts two arguments - the first the path to and name of
the empty document, the second the path to and name of the document you wish
to produce.
Bear in mind that this code produced invalid document - I think there was a
problem with the tables properties but cannot remember for certain. When you
try to open the resulting file, use the Open and Repair option - if this is
available to you - and it ought to tell you of any problems.
Yours
Mark B
PS I always intended to create a document with a table using Word and then
examine that tables properties to see if this is where the problem lies but
I never had the chance. Maybe that is something that you could do if the
code still fails to yield a valid table.
--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/Create-and-read-table-cotents-in-doc-file-tp4293563p4296126.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]