• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Find/Replace using Apache POI

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Ive just started doing Java so am at beginner level. I need to produce documents based on templates (.dot) and replace a few key words. Ive already done this in VB in the past.

Im using Apache POI. Ive created a new document based on a template so far. My template was 5 pages, the genrated document was around 70 pages. There were 65 pages of gobbledygook. Unsure as to how that happened.

I now need to replace some key words.

Can someone advise how to do this (is it doable) ? Im not doing any other formatting.


Thanks


 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rico and welcome to Javaranch! Can you post some code snippets of how you are performing this transform?
 
Rico Thomas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
H Martin

This is what I wrote. Ive tried a couple of templates , and each time around 20pages get added to the end which are full of rubbish.

POIFSFileSystem fs;
try {
fs = new POIFSFileSystem(new FileInputStream("C:\\Test\\Template1.dot"));
HWPFDocument doc = new HWPFDocument(fs);
doc.write(new FileOutputStream("C:\\Test\\Documents\\Document1.doc"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Forgot to mention, all i want to do is generate some docs based on some templates(.dot) and replace a few keywords. I do have some logos in those templates, but Im assuming they would be 'carried' over.
Thanks
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not "rubbish". That's just things which MS Word hides from you because it knows that "template" isn't a real Word document but a Word template.

POI is designed to work with Word documents. It isn't too surprising that feeding it a Word template instead results in confusing behaviour. Try using a real Word document as your "template".
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There was a discussion about replacing placeholders in DOC files using POI at https://coderanch.com/t/448084/open-source/updating-placeholder-poi
 
Rico Thomas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before I start on the replace, I'd like to get the doument generation sorted. I changed my .dot template to a .doc one and even then my 2 page template turns into a 40page document.I tweakd the code so im doing a docOut.close(); at the end but it made no difference. Does the version of MS Office make any difference ? (I have office 2007).
reply
    Bookmark Topic Watch Topic
  • New Topic