Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Find/Replace using Apache POI

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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).
 
Shiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!