Hi, I have a similar project at https://github.com/centic9/poi-mail-merge, it allows to do "mail merges", i.e. replace tags in a template document multiple times. Only it is based on XWPF not HWPF, so it will only be useful to you if you can switch to the newer docx-format.
Dominik Am 20.10.2015 16:13 schrieb "Edilmar LISTAS" <[email protected]>: > Hi, > > I use POI for simple .doc/.xls handling. > But now I have a little more complex .doc file to handle, > and POI has done something wrong (or my code). > I searched in the internet and POI docs but didn't find the solution. > > The template file has a table with many cells like a client record, > with name, address, phone, etc. I have tags like <NAME>, etc > that I would like to change by database values. > > The result file after POI processing to replace tags is misconfigured, > the table is lost and all the text values become all together. > > I used this code to replace: > > public void replaceTags(String origFile, String destFile, Map<String, > String> properties) throws IOException { > POIFSFileSystem fs = new POIFSFileSystem(new > FileInputStream(origFile)); > HWPFDocument doc = new HWPFDocument(fs); > Range range = doc.getRange(); > Set<Entry<String, String>> set = properties.entrySet(); > Iterator<Entry<String, String>> it = set.iterator(); > while (it.hasNext()) { > Entry<String, String> entry = it.next(); > range.replaceText(entry.getKey(), entry.getValue()); > } > OutputStream output = new FileOutputStream(destFile); > doc.write(output); > } > > This is the call to replaceTags: > > Map<String, String> properties = new HashMap<>(); > properties.put("<NAME>", "INTERSITE INFORMÁTICA"); > new > TemplateWord().replaceTags("/home/ed/orig.doc","/home/ed/dest.doc",properties); > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > >
