• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

HWPF Mail Merging

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am using POI's HWPF to read word doc from my java program. I am basically doing a mail merge program. I'm able to read the doc file and replace the placeholders using the RANGE Object. Now the problem is when I write it out to a new doc. the actual formatting like bold, font etc., is not getting reflected in my outputted doc. javascript:%20x()

Please let me know whether I can do this using HWPF.

Thanks
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you specifying the formatting? Can you post a short code example that applies formatting that doesn't take effect?
 
vijay makam
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm reading a character run doing the mail merge using lookup() function and replacing the character run with the returned string. Also, now I am not seeing any contents at all in my destination doc (referred by rangeB)


for(int i=0;i<range.numParagraphs();i++){
Paragraph para = range.getParagraph(i);
for(int j=0;j<para.numCharacterRuns();j++){
CharacterRun chrun = para.getCharacterRun(j);

String newText= this.lookup(chrun.text());
chrun.replaceText(chrun.text(), newText);

}
}


rangeB.replaceText(rangeB.text(), range.text());

// System.out.println("RangeB text is "+rangeB.text());
filewriter.write(rangeB.text());

}
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you writing the altered file? This:

looks like you're using the java.io classes; is that correct? If so, that's not going to work. You need to use the POI classes and methods to write DOC files.
 
vijay makam
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm creating a HWPF document for writing and using a java.io class to write to it(FileWriter). As I didn't find any write methods in HWPF. Can you please tell me which classes in POI should I use. I have spent a lot of time on this and I'm running short of time.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the HWPFDocument.write method.
 
vijay makam
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The HWPF's write method accepst OutputStream as the paramter.
But when I create an output stream like this
FileOutputStream fos = new FileOutputStream(Test.doc)

I am getting an error "Unable to read entire header; 6 bytes read; expected 512 bytes".
 
vijay makam
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the code I am using to write to the word file.

file = new File("Test.doc");
file1 = new File("Outout.doc");
fis= new FileInputStream(file);

docA = new HWPFDocument(fis);

fos = new FileOutputStream(file1);

Range range = docA.getRange();

for(int i=0;i<range.numParagraphs();i++){
Paragraph para = range.getParagraph(i);
for(int j=0;j<para.numCharacterRuns();j++){
CharacterRun chrun = para.getCharacterRun(j);

String newText= this.lookup(chrun.text());
chrun.replaceText(chrun.text(), newText);

}
}

docA.write(fos); //Is this correct ??
}
When I try to open my Output.doc. I am getting a "word was unable to read". I am not able to recover it also.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There was a discussion on how to replace text in DOC files using POI on the POI mailing list recently, and someone posted code that does that; it may help you. The download location is mentioned in this mail.
 
vijay makam
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ulf for the pointer. I tried the code that was posted in the MailingList and I am facing the same problem which I was facing with my code. "word not able to open file: the file is corrupted"

Can you suggest some alternative to achieve that from my Java Program. Can JASPER Reporting tool be way to go.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm fairly certain that Jasper can't export to DOC, but a quick check on its web site will tell you for sure.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay,
which version you had used for using POI.
Can you please help for word document generation using poi.
If you have any suggestions, please help me.

vijay makam wrote:I'm reading a character run doing the mail merge using lookup() function and replacing the character run with the returned string. Also, now I am not seeing any contents at all in my destination doc (referred by rangeB)


for(int i=0;i&amp;lt;range.numParagraphs();i++){
Paragraph para = range.getParagraph(i);
for(int j=0;j&amp;lt;para.numCharacterRuns();j++){
CharacterRun chrun = para.getCharacterRun(j);

String newText= this.lookup(chrun.text());
chrun.replaceText(chrun.text(), newText);

}
}


rangeB.replaceText(rangeB.text(), range.text());

// System.out.println(&quot;RangeB text is &quot;+rangeB.text());
filewriter.write(rangeB.text());

}

 
Acetylsalicylic acid is aspirin. This could be handy too:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic