poi生成word文档,包含插入表格

package com.controller;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.xmlbeans.XmlCursor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value="/base")  
public class TestController {
	
	@RequestMapping(value="/test")
	public void docTest()   {
		String templatePath = "D:\\demo.doc";
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(templatePath);
			XWPFDocument doc = new XWPFDocument(in);
			//文本替换
			Map<String, String> param = new HashMap<String, String>();
			param.put("PartyA", "我是甲方");
			param.put("PartyB", "我是乙方");
			param.put("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			param.put("text", "合同内容合同内容合同内容合同内容合同内容合同内容合同内容合同内容合同内容合同内容合同内容合同内容合同内容");
			List<XWPFParagraph> allXWPFParagraphs = doc.getParagraphs();
			for (XWPFParagraph xwpfParagraph : allXWPFParagraphs) {
				List<XWPFRun> runs = xwpfParagraph.getRuns();
				for (XWPFRun run : runs) {
					String text = run.getText(0);
					if (StringUtils.isNoneBlank(text)) {
						if (text.equals("table")) {
							//表格生成 6行5列.
							int rows = 6;
							int cols = 5;
							XmlCursor cursor = xwpfParagraph.getCTP().newCursor();
							XWPFTable tableOne = doc.insertNewTbl(cursor);
							
							//样式控制
							CTTbl ttbl = tableOne.getCTTbl();
							CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();  
					        CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();  
					        CTJc cTJc = tblPr.addNewJc();  
					        cTJc.setVal(STJc.Enum.forString("center"));//表格居中
					        tblWidth.setW(new BigInteger("9000"));//每个表格宽度
					        tblWidth.setType(STTblWidth.DXA);  
					        
							//表格创建
							XWPFTableRow tableRowTitle = tableOne.getRow(0);
							tableRowTitle.getCell(0).setText("标题");
							tableRowTitle.addNewTableCell().setText("内容");
							tableRowTitle.addNewTableCell().setText("姓名");
							tableRowTitle.addNewTableCell().setText("日期");
							tableRowTitle.addNewTableCell().setText("备注");
							for (int i = 1; i < rows; i++) {
								XWPFTableRow createRow = tableOne.createRow();
								for (int j = 0; j < cols; j++) {
									createRow.getCell(j).setText("我是第"+i+"行,第"+(j+1)+"列");
								}
							}
							run.setText("", 0);
						}else {
							for (Entry<String, String> entry : param.entrySet()) {
								String key = entry.getKey();
								if (text.indexOf(key) != -1) {
									text = text.replace(key, entry.getValue());
									run.setText(text, 0);
								}
							}
						}
					}
				}
			}
			
			out = new FileOutputStream("D:\\report.doc");
			// 输出
			doc.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

demo.doc 如下:

report.doc 如下:


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值