一:包及相关软件:
项目实现之前需要导入poi包,链接:链接:https://pan.baidu.com/s/1PZsmxfzI0R4eCV-tQYBzTA
提取码:ffeh
二:代码实现
package com.bjsxt.util;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.Map;
import java.util.Set;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
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.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
/**
* 将对象中的数据转换成word文档格式,需要导入poi.jar包
* @author CarlosXu
*/
public class TestPOIWordOut {
public void outToWord(Forum form) {
//Blank Document
XWPFDocument document= new XWPFDocument();
//Write the Document in file system
FileOutputStream out;
try {
out = new FileOutputStream(new File("D:\\eclipse\\sxtWorkspace01\\UserInfo01\\word\\poi_word.docx"));
//添加标题
XWPFParagraph titleParagraph = document.createParagraph();
//设置段落居中
titleParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun titleParagraphRun = titleParagraph.createRun();
titleParagraphRun.setText("个人简历");
titleParagraphRun.setColor("000000");
titleParagraphRun.setFontSize(20);
//段落
XWPFParagraph firstParagraph = document.createParagraph();
XWPFRun run = firstParagraph.createRun();
run.setText("自我简介:"+form.getMessage());
run.setColor("698869");
run.setFontSize(11);
//设置段落背景颜色
CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
cTShd.setVal(STShd.CLEAR);
cTShd.setColor("97FF45");
//换行
XWPFParagraph paragraph1 = document.createParagraph();
XWPFRun paragraphRun1 = paragraph1.createRun();
paragraphRun1.setText("\r");
//段落
XWPFParagraph firstParagraph2 = document.createParagraph();
XWPFRun run2 = firstParagraph2.createRun();
run2.setText("生活标语:"+form.getProfile());
run2.setColor("698869");
run2.setFontSize(11);
//设置段落背景颜色
CTShd cTShd2 = run2.getCTR().addNewRPr().addNewShd();
cTShd2.setVal(STShd.CLEAR);
cTShd2.setColor("97FF45");
//换行
XWPFParagraph paragraph2 = document.createParagraph();
XWPFRun paragraphRun2 = paragraph2.createRun();
paragraphRun2.setText("\r");
//基本信息表格
XWPFTable infoTable = document.createTable();
//去表格边框
infoTable.getCTTbl().getTblPr().unsetTblBorders();
//列宽自动分割
CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
infoTableWidth.setType(STTblWidth.DXA);
infoTableWidth.setW(BigInteger.valueOf(9072));
//表格第一行
XWPFTableRow infoTableRowOne = infoTable.getRow(0);
infoTableRowOne.getCell(0).setText("姓名:");
infoTableRowOne.addNewTableCell().setText(form.getFullName());
//表格第二行
XWPFTableRow infoTableRowTwo = infoTable.createRow();
infoTableRowTwo.getCell(0).setText("出生日期:");
infoTableRowTwo.getCell(1).setText(form.getBirthDate());
//表格第三行
XWPFTableRow infoTableRowThree = infoTable.createRow();
infoTableRowThree.getCell(0).setText("应聘职位:");
infoTableRowThree.getCell(1).setText(form.getJob());
//表格第四行
XWPFTableRow infoTableRowFour = infoTable.createRow();
infoTableRowFour.getCell(0).setText("个人网站:");
infoTableRowFour.getCell(1).setText(form.getTelephone());
//表格第五行
XWPFTableRow infoTableRowFive = infoTable.createRow();
infoTableRowFive.getCell(0).setText("邮箱:");
infoTableRowFive.getCell(1).setText(form.getEmail());
//两个表格之间加个换行
XWPFParagraph paragraph = document.createParagraph();
XWPFRun paragraphRun = paragraph.createRun();
paragraphRun.setText("\r");
//段落
XWPFParagraph firstParagraph3 = document.createParagraph();
XWPFRun run3 = firstParagraph3.createRun();
run3.setText("个人计划:"+form.getSkills());
run3.setColor("698869");
run3.setFontSize(11);
//设置段落背景颜色
CTShd cTShd3 = run3.getCTR().addNewRPr().addNewShd();
cTShd3.setVal(STShd.CLEAR);
cTShd3.setColor("97FF45");
//换行
XWPFParagraph paragraph3 = document.createParagraph();
XWPFRun paragraphRun3 = paragraph3.createRun();
paragraphRun3.setText("\r");
//工作经历表格
XWPFTable ComTable = document.createTable();
//列宽自动分割
CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();
comTableWidth.setType(STTblWidth.DXA);
comTableWidth.setW(BigInteger.valueOf(9072));
//表格第一行
XWPFTableRow comTableRowOne = ComTable.getRow(0);
comTableRowOne.getCell(0).setText("技能");
comTableRowOne.addNewTableCell().setText("技能熟练度");
//表格第二行
Map<String, String> skillInfo = form.getSkillInfo();
Set<String> set=skillInfo.keySet();
for (String string : set) {
XWPFTableRow comTableRowTwo = ComTable.createRow();
for (int i = 0; i < 2; i++) {
if (i%2==0) {
comTableRowTwo.getCell(i).setText(string);
}else {
comTableRowTwo.getCell(i).setText(skillInfo.get(string)+"%");
}
}
}
document.write(out);
out.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("create_table document written success.");
}
}
提示:本文是将数据从xml中读取数据传入对象,所以用到了dom4j包,comment包一定要导入,不然会jvm会报错。