You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by sanjeevatPOI <sa...@gmail.com> on 2011/04/09 23:34:55 UTC
Create and read table cotents in .doc file.
hi all, i am quit new to POI. I want to know how to create a table in a word
doc using hwpf and how to insert data into the table in row and column wise
format. again how i can read the data from the table in row and column wise
format.
eg : data=table[row][column].
Please help me out.
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-and-read-table-cotents-in-doc-file-tp4293563p4293563.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
Re: Create and read table cotents in .doc file.
Posted by sanjeevatPOI <sa...@gmail.com>.
Hi, I am getting the same problem regarding inserting a table. So please help
me if any body has the solution.
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-and-read-table-cotents-in-doc-file-tp4293563p4497707.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
Re: Create and read table cotents in .doc file.
Posted by MrTables <ud...@fortiss.org>.
Hello,
I'm pretty new to java and apache poi. Recently I'm trying to create a
document including several tables. I read your posts and code and tried to
implement a little document on my own. Here's the method I'm using:
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;
Table table = null;
try {
inputFile = new File(inputFilename);
fis = new FileInputStream(inputFile);
POIFSFileSystem fs = new POIFSFileSystem(fis);
document = new HWPFDocument(fs);
// Get the overall Range
overallRange = document.getRange();
// Set a bold, italic, big heading
CharacterRun run1 = overallRange.insertAfter("table");
run1.setBold(true);
run1.setItalic(true);
run1.setFontSize(36);
//trying to insert a paragraph. Works but neutralizes the
formatting of run1 and par1
/*Paragraph par1 = overallRange.insertAfter(new
ParagraphProperties(), 0);
par1.setSpacingAfter(200);
CharacterRun run2 = par1.insertAfter("paragraph number one");
run2.setBold(true);*/
// trying to insert a table, but word says document is corrupted
/*TableProperties tp = new TableProperties(3);
table = overallRange.insertBefore(tp, 3);
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);
}
}
Everythig works fine, until I include the first commented code (Paragraph
1), afterwards the document is still readable with word but all formattings
are gone. When I include the commented table code, I get an error by word,
saying the document is damaged.
In the hwpf javadocs I could not find a different way to implement tables.
Can anyone give me a hint what I'm doing wrong, especially with the tables?
Thanks and Greetings
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-and-read-table-cotents-in-doc-file-tp4293563p4346155.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
Re: Create and read table cotents in .doc file.
Posted by Mark Beardsley <ma...@tiscali.co.uk>.
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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: Create and read table cotents in .doc file.
Posted by sanjeevatPOI <sa...@gmail.com>.
there is one more query regarding the table in the word file. How can i do
the same process for writing the table in the word file in the paragraph.
Please help me out.
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-and-read-table-cotents-in-doc-file-tp4293563p4296098.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
Re: Create and read table cotents in .doc file.
Posted by sanjeevatPOI <sa...@gmail.com>.
Thank you very much Mr. Mark. This is what I was looking for.
Regards,
Sanjeev
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-and-read-table-cotents-in-doc-file-tp4293563p4295884.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
Re: Create and read table cotents in .doc file.
Posted by Mark Beardsley <ma...@tiscali.co.uk>.
I found this lurking on the hard drive, I worte it a long time ago and have
not tested it but remember it working sucessfully. It functions by
retrieving the tables one at a time - in the manner described earlier - and
writing them into an ArrayList for later processing which may not be what
you want. The code should, however, point you in the correct direction.
Complie and run it on a test machine to make sure it does what you require
please; remember this was test code I wrote a long time ago and have not
tested recently.
public void getTables() {
BufferedInputStream bufIStream = null;
FileInputStream fileIStream = null;
File inputFile = null;
HWPFDocument doc = null;
Range range = null;
ArrayList
tables = null;
Paragraph para = null;
TableRow row = null;
TableCell cell = null;
boolean inTable = false;
int numParas = 0;
int numRowsInTable = 0;
int numCellsInRow = 0;
try {
tables = new ArrayList
();
inputFile = new File("C:/temp/tables.doc");
fileIStream = new FileInputStream(inputFile);
bufIStream = new BufferedInputStream(fileIStream);
//
// Open a Word document.
//
doc = new HWPFDocument(bufIStream);
//
// Get the highest level Range object that represents the
// contents of the document.
//
range = doc.getRange();
//
// Get the number of paragraphs
//
numParas = range.numParagraphs();
//
// Step through each Paragraph.
//
for(int i = 0; i < numParas; i++) {
para = range.getParagraph(i);
//
// Is the Paragraph within a table?
//
if(para.isInTable()) {
//
// The inTable flag is used to ensure that a call is
made
// to the getTable() method of the Range class once only
// when the first Paragraph that is within a table is
// recovered. So......
//
if(!inTable) {
//
// Get the table and add it to an ArrayList for
later
// processing. You do not have to do this, it would
// be possible to process the table here. There are
// methods defined on the Table class that allow you
// to get at the number of rows in the table and to
// recover a reference to each row in turn. Once you
// have a row, it is possible then to get at each
cell
// in turn. Look at the Table, TableRow and
TableCell
// classes.
//
tables.add(range.getTable(para));
inTable = true;
}
}
else {
//
// Set the flag false to indicate that all of the
paragrphs
// in the table have been processed. A single blank line
is
// sufficient to indicate the end of the tbale within
the
// Word document.
//
// This is also the place that you could deal with any
// non-table paragraphs.
//
inTable = false;
}
}
//
// This line simply prints out the number of tables found in the
// document - used for testing purposes here.
//
System.out.println("Found " + tables.size() + " tables in the
document.");
for(Table table : tables) {
numRowsInTable = table.numRows();
for(int i = 0; i < numRowsInTable; i++) {
row = table.getRow(i);
numCellsInRow = row.numCells();
for(int j = 0; j < numCellsInRow; j++) {
cell = row.getCell(j);
System.out.println("Cell at row " + i + " column " +
j + " contains: " + cell.text());
}
}
}
}
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(bufIStream != null) {
try {
bufIStream.close();
bufIStream = null;
fileIStream = null;
}
catch(Exception ex) {
// I G N O R E //
}
}
}
}
Yours
Mark B
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-and-read-table-cotents-in-doc-file-tp4293563p4295318.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
Re: Create and read table cotents in .doc file.
Posted by Mark Beardsley <ma...@tiscali.co.uk>.
No single method exists that allows you to read data from tables in the way
you are seeking; you will have to write that yourself. I do not have the
code on hand now but the basic approach will be this;
Open the Word document.
>From the document retrieve the Range object that represents the contents of
the document by calling the getRange() method.
On that Range object, call the numParagraphs() method to discover how many
paragraphs there are in the document.
Using this. number, yoiu can create a conditioned loop within which you can
call the getParagraph(int) method of the top level Range object to recover
the Paragraph objects one at a time.
Check if the Paragarph object represents text that is contained within a
table by calling the Paragraphs isInTable() method.
If this method returns true then you can get at the actual table holding the
data by calling the getTable() method of the high level Range and pass the
Paragraph object to it. Be aware though that when I did this originally,
getTable() could only be called on the first paragraph in the table, it
threw an exception if I tried to call it for the second, third, etc
paragraph.
Now that you have a Table object it is a simple task to iterate through the
rows - numRows() will tell you how many there are and getRow(int) will
retrieve a row for you. Once you have the row object, get each cell it
contains - numCells() and getCell(int). With an instance of the TableCell
class in hand, you have a choice to make, do you simply want the text - in
which case you can call the text() method of the cell, or do you want more
information - font etc.
Of course, if all you want is text from the table and you do not care where
abouts in the table it came from, simply get the paragraph and check if it
is in a table. If it is, store the data if not discard it. Much simpler but
maybe not wnat you want.
Try that for starters. Writing is a similar process but in reverse if I
remember correctly. If I have the time I will try to put some code together
but I am currently away from my office desk and do not have access to the
api so cannot promise when I will be able to do this.
Yours
Mark B
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-and-read-table-cotents-in-doc-file-tp4293563p4295291.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