使用POI分段落生成纯Word动态模板并导入数据
来源:互联网 发布:手机能注册淘宝卖家吗 编辑:程序博客网 时间:2024/05/14 11:54
导出数据,可以用word另存为xml格式的ftl文件,变量用${变量名}表示,然后在类中通过freemarker去替换变量。
但是怎么导入word数据。发现如果是xml格式,数据格式很易变。如一个标题中如果有中文,后面是个数字,另存成xml时就变成了2个元素了。太郁闷了。
后来找到方法可以分段落读入纯word文档。要找到了word基于模板替换的相关例子。于是方案如下。
纯word文件变量用${变量名}表示。导出动态模板时,把业务数据替换变量。
导入是分段落读入数据。
POIReportUtil.java文件
package com.sunrise.wbmp.manage.project.report.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.StyleDescription;
import org.apache.poi.hwpf.model.StyleSheet;
import org.apache.poi.hwpf.usermodel.CharacterProperties;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
public class POIReportUtil {
public static void main(String[] args) throws Exception {
Map<String, POIText> replaces = new HashMap<String, POIText>();
System.out.println("导出数据:");
replaces.put("${projectName}", POIText.str("CMMI"));
replaces.put("${graph1}", POIText.str("rongzhi_li"));
replaces.put("${内容1}", POIText.str("1123456"));
replaces.put("${graph2}", POIText.str("graph"));
replaces.put("${内容2}", POIText.str("2222"));
//
replaces.put("${graph3}", POIText.str("graph"));
replaces.put("${内容3}", POIText.str("33333"));
replaces.put("${graph4}", POIText.str("<END>"));
replaces.put("${内容4}", POIText.str(""));
for (int i=5;i<10;i++){
replaces.put("${graph"+i+"}", POIText.str(""));
replaces.put("${内容"+i+"}", POIText.str(""));
}
HWPFDocument hwpf=poiWordTableReplace("E:\\temp\\template\\week_report.doc", replaces);
FileOutputStream out = new FileOutputStream("E:\\temp\\template\\t2.doc");
hwpf.write(out);
out.flush();
out.close();
System.out.println("读入数据:");
FileInputStream in = new FileInputStream("E:\\temp\\template\\t2.doc");
HWPFDocument hwpf2 = new HWPFDocument(in);
Map<String,String> map=readWord( hwpf2);
for (Iterator iter=map.entrySet().iterator();iter.hasNext();){
java.util.Map.Entry entry=(java.util.Map.Entry)iter.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
public static Map<String,String> readWord(HWPFDocument hwpf
) throws Exception {
// FileInputStream in = new FileInputStream(sourceFile);
// HWPFDocument hwpf = new HWPFDocument(in);
Map<String, String> oldMap=new HashMap<String, String>();
Range r = hwpf.getRange();
List<String> list = new ArrayList<String>();
for (int i = 0; i < r.numParagraphs(); i++) {
Paragraph p = r.getParagraph(i);
// check if style index is greater than total number of styles
int numStyles =hwpf.getStyleSheet().numStyles();
int styleIndex = p.getStyleIndex();
if (numStyles > styleIndex) {
StyleSheet style_sheet = hwpf.getStyleSheet();
StyleDescription style = style_sheet.getStyleDescription(styleIndex);
String styleName = style.getName();
if (styleName!=null&&styleName.contains("标题 3")) {
// write style name and associated text
System.out.println(styleName + " -> " + p.text());
//System.out.println(p.text());
String text = p.text();
text=text.substring(0,text.length()-1);
list.add(text);
oldMap.put(text, null);
if ("<END>".equals(text)){
break;
}
}else{
if (list.size()>0){
String key=list.get(list.size()-1);
String oldS=oldMap.get(list.get(list.size()-1));
String s=( p.text()==null)?"": p.text();
oldS=(oldS==null)?s:(oldS+s);
if (oldS!=null && oldS.trim().length()>0){
oldMap.put(key, oldS);
}
}
}
}
}
return oldMap;
}
public static HWPFDocument poiWordTableReplace(String sourceFile,
Map<String, POIText> replaces) throws Exception {
FileInputStream in = new FileInputStream(sourceFile);
HWPFDocument hwpf = new HWPFDocument(in);
// Range range = hwpf.getRange();// 得到文档的读取范围
// TableIterator it = new TableIterator(range);
// // 迭代文档中的表格
// while (it.hasNext()) {
// Table tb = (Table) it.next();
// // 迭代行,默认从0开始
// for (int i = 0; i < tb.numRows(); i++) {
// TableRow tr = tb.getRow(i);
// // 迭代列,默认从0开始
// for (int j = 0; j < tr.numCells(); j++) {
// TableCell td = tr.getCell(j);// 取得单元格
// // 取得单元格的内容
// for (int k = 0; k < td.numParagraphs(); k++) {
// Paragraph para = td.getParagraph(k);
//
// String s = para.text();
// final String old = s;
// for (String key : replaces.keySet()) {
// if (s.contains(key)) {
// s = s.replace(key, replaces.get(key).getText());
// }
// }
// if (!old.equals(s)) {// 有变化
// para.replaceText(old, s);
// s = para.text();
// System.out.println("old:" + old + "->" + "s:" + s);
// }
//
// } // end for
// } // end for
// } // end for
// } // end while
Range r = hwpf.getRange();
CharacterProperties props = new CharacterProperties();
props.setFontSize(10);
List<String> list = new ArrayList<String>();
for (int i = 0; i < r.numParagraphs(); i++) {
Paragraph p = r.getParagraph(i);
// check if style index is greater than total number of styles
int numStyles =hwpf.getStyleSheet().numStyles();
int styleIndex = p.getStyleIndex();
if (numStyles > styleIndex) {
StyleSheet style_sheet = hwpf.getStyleSheet();
StyleDescription style = style_sheet.getStyleDescription(styleIndex);
String styleName = style.getName();
// if (styleName!=null&&styleName.contains("标题")) {
// // write style name and associated text
// //System.out.println(styleName + " -> " + p.text());
// System.out.println(p.text());
// String text = p.text();
// list.add(text);
// }else{
// System.out.println( p.text());
// }
System.out.println( styleName);
String s = p.text();
final String old = s;
for (String key : replaces.keySet()) {
if (s.contains(key)) {
s = s.replace(key, replaces.get(key).getText());
}
}
if (!old.equals(s)) {// 有变化
p.replaceText(old, s);
s = p.text();
System.out.println("old:" + old + "->" + "s:" + s);
}
}
}
return hwpf;
}
}
POIReportUtil.java文件
package com.sunrise.wbmp.manage.project.report.util;public abstract class POIText {
public abstract String getText();
public static POIText str(final String string) {
return new POIText() {
@Override
public String getText() {
return string;
}
};
}
}
- 使用POI分段落生成纯Word动态模板并导入数据
- poi实现根据excel模板,生成excel并导入数据
- POI 使用word模板生成word文档
- POI操作word模板并生成新的word.docx
- POI操作word模板并生成新的word.docx
- POI操作word模板并生成新的word.docx
- poi动态生成word
- 使用 poi 根据 word 模板生成 word 文件
- 使用 poi 根据 word 模板生成 word 文件
- POI 使用替换字符方式进行模板生成word
- POI动态生成数据并删去多余模版
- freemarker+poi 模板生成 word添加图片
- java poi通过模板生成word文档
- 使用函数生成excel模板并将excel中的数据导入到内表中
- C#也能动态生成Word文档并填充数据
- C#也能动态生成Word文档并填充数据
- C#也能动态生成Word文档并填充数据
- C#也能动态生成Word文档并填充数据
- STC15F2K60 脉宽测量程序
- Lua程序数据类型及其使用
- linux 混杂设备驱动之adc驱动
- EasyBCD
- 五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT)
- 使用POI分段落生成纯Word动态模板并导入数据
- ubuntu 系统学习笔记
- 推荐一本好书-----《Java解惑》
- linux正则表达式处理文本
- 如何在ASP.NET中实现验证码?
- 传入一个字符串,如何获得这个字符串中所有字符出现的个数
- Linux命令详解之file
- Java数组及引用类型内存分配
- 在linux下用wget整站下载网站