public void exportDoc () {
String filePath = AAA.class.getClassLoader()
.getResource(".." + File.separator + ".." + File.separator + "/template/XXX.docx").getPath();
File file = new File(filePath);
if (!file.exists()) {
this.addErrorMsg("导出失败");
return ;
}
OPCPackage pack = null;
XWPFDocument doc = null;
try {
Map
map.put("fileNo", archiveRecord.getFileNo());
int loantype = archiveRecord.getLoantype();
map.put("zqFlag", "□");
map.put("jqzdFlag", "□");
map.put("zqFlag", "☑");
map.put("jqzdFlag", "☑");
pack = POIXMLDocument.openPackage(filePath);
doc = new XWPFDocument(pack);
//段落
List
for (XWPFParagraph tmp : paragraphs) {
String oneparaString = tmp.getText();
if (Utility.isEmpty(oneparaString)) {
continue;//表格
}
for (Entry
oneparaString = ContextUtil.matchReplase(oneparaString,
e.getKey(), e.getValue());// 替换零时字符
}
ContextUtil.replaceInParagraph(tmp, oneparaString, map);// 将零时替换的字符在段落中替换
}
// 表格
Iterator
int tableCount = 0;
int count = 0;
while (it.hasNext()) {
XWPFTable table = it.next();
List
if(tableCount++ == 1){//第二个表需要增行
for (Iterator
Entry
Integer key = entry.getKey();
String Name = "XXX";
List datas = entry.getValue();
for (int j = 0; j < datas.size(); j++) {
Data data = datas.get(j);
XWPFTableRow newRow = table.createRow(); // 增加行
List
count++;
newRowCells.get(0).setText(count+"");
XWPFTableCell cell1 = newRowCells.get(1);
if(j==0){
cell1.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
}else{
cell1.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);// 合并多行
}
cell1.setText(TextUtility.noNull(Name));
newRowCells.get(2).setText(TextUtility.noNull(data.getTemplateName()));
}
}
continue;
}
// 填充修改表格
for (int i=0;i
List
for (XWPFTableCell cell : cells) {
List
for (XWPFParagraph tmp : paras) {
String oneparaString = tmp.getText();
if (Utility.isEmpty(oneparaString)) {
continue;
}
for (Entry
oneparaString = ContextUtil.matchReplase(oneparaString,
e.getKey(), e.getValue());
}
ContextUtil.replaceInParagraph(tmp, oneparaString, map);
}
}
}
}
ContextUtil.export("导出", doc);
} catch (IOException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return;
}
ContextUtil
public static String matchReplase(String source, String regix, String value) {
// 生成匹配模式的正则表达式
String patternString = "\\$\\{(" + regix + ")\\}";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(source);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, value);
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* 对word 2007中的段落关键字进行替换....
*
*/
ContextUtil
public static void replaceInParagraph(XWPFParagraph para, String param, Map
List
runs = para.getRuns();
int stringStartPos = 0;
String startString = "";
boolean startFlag = false;
boolean endFlag = false;
if (runs != null && runs.size() > 0) {
for (int i = 0; i < runs.size(); i++) {
XWPFRun run = runs.get(i);
String text = run.getText(run.getTextPosition());
if (text == null || text.trim().equals("")) {
continue;
}
// 匹配字符串....
boolean matchFlag = false;
try {
matchFlag = matchReplase(param.substring(stringStartPos), text);
}
catch (Exception e) {
matchFlag = false;
}
if (matchFlag) {
stringStartPos = stringStartPos + text.length();
}
else {
if (text.trim().startsWith("${") || text.trim().startsWith("{")) {
startFlag = true;
if (text.trim().endsWith("}")) {
startString = startString + text.trim();
endFlag = true;
}
}
else if (text.trim().endsWith("}")) {
endFlag = true;
startString = startString + text.trim();
}
if (startFlag && !endFlag) {
startString = startString + text.trim();
// 删掉该字符...
para.removeRun(i);
run = para.insertNewRun(i);
run.setText("");
// run.setText(null,run.getTextPosition());
}
// 找到${name},从系统里找相关的值....
if (startFlag && endFlag) {
startFlag = false;
endFlag = false;
startString = startString.substring(2, startString.length() - 1);
String value = map.get(startString);
if (value == null) {
value = "";
}
stringStartPos = stringStartPos + value.length();
run.setText(value, 0);
startString = "";
}
}
}
}
}
// 导出Word 2007..
ContextUtil
public static void export(String fileName, XWPFDocument doc) {
try {
// 输出word内容文件流,提供下载
HttpServletResponse response = getResponse();
// response.reset();
// response.setContentType("application/x-msdownload");
// response.setContentType("application/msword");
response.setContentType("application/octet-stream");
fileName = fileName + ".docx";
response.setHeader("name", fileName);
// attachment --- 作为附件下载
// inline --- 在线打开
response.addHeader("Content-Disposition", "inline; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
OutputStream servletOS = response.getOutputStream();
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
doc.write(ostream);
servletOS.write(ostream.toByteArray());
servletOS.flush();
servletOS.close();
}
catch (Exception e) {
e.printStackTrace();
}
}