欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-30 18:28:29
...
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 如下:

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

report.doc 如下:

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

相关标签: poi