public static XWPFDocument createDdtzdTable(DataObject para) throws AppException, IOException {
boolean isxz = para.getBoolean("isxz", true);//是否新增
boolean issyq = para.getBoolean("issyq", false);//是否试用期
String nd = para.getString("nd","");
String tzxh = para.getString("tzxh","");
String dwmc = para.getString("dwmc","");//"综合保税区发展中心
String tzrq = para.getDateToString("tzrq", "yyyy年MM月dd日");
String csdwmc = para.getString("csdwmc","");//"服务业促进局,社会事业局,科技经济运行局"
String dgrq = para.getDateToString("dgrq", "yyyy年MM月dd日");
DataStore ryds = para.getDataStore("ryds",new DataStore());
String xmInZw = "";//正文中姓名
int rs = 0;//人数
String rsStr = "";
if(ryds!=null && ryds.rowCount()>0){
xmInZw = ryds.getString(0, "xm");
rs = ryds.rowCount();
rsStr = GAUtil.getNumberCap(rs);
}
XWPFDocument doc = new XWPFDocument();
CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr();
CTPageMar pageMar = sectPr.addNewPgMar();
pageMar.setLeft(BigInteger.valueOf(1587L));// 参数/567=实际CM
pageMar.setTop(BigInteger.valueOf(2097L));
pageMar.setRight(BigInteger.valueOf(1587L));
pageMar.setBottom(BigInteger.valueOf(2097L));
// 标题
XWPFParagraph pTitle = doc.createParagraph();
//setFontFormat(pTitle, "人力资源管理部", 30, "方正小标宋简体", true, ParagraphAlignment.CENTER, false, null);
pTitle.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = pTitle.createRun();//一个XWPFRun代表具有相同属性的一个区域。
run.setText("人力资源管理部");
run.setBold(true);
//run.setFontFamily("方正小标宋简体");只对数字,英文起作用
run.setFontSize(30);
run.setColor("FF0000");
run.setUnderline(UnderlinePatterns.SINGLE);
CTRPr rpr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : run.getCTR().addNewRPr();
CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts();
fonts.setEastAsia("方正小标宋简体");//汉字字体
fonts.setAscii("方正小标宋简体");//数字字体
fonts.setHAnsi("方正小标宋简体");//括号符号等字体
//年度、序号
XWPFParagraph pNdxh = doc.createParagraph();
setFontFormat(pNdxh, "("+nd+")"+String.valueOf(tzxh)+"号", 16, "楷体", false, ParagraphAlignment.RIGHT, false, null);
//正文标题:岗位调配通知书
XWPFParagraph pZwbt = doc.createParagraph();
setFontFormat(pZwbt, "岗位调配通知书", 22, "文星标宋", false, ParagraphAlignment.CENTER, false, null);
//正文内容
XWPFParagraph pZw = doc.createParagraph();
//新增人员
if(isxz){
//单位名称
setFontFormat(pZw, dwmc, 16, "楷体", false, ParagraphAlignment.LEFT, false, UnderlinePatterns.SINGLE);
setFontFormat(pZw, ":", 16, "楷体", false, ParagraphAlignment.LEFT, true, null);
//内容
setFontFormat(pZw, " 因工作需要,根据岗位聘任管理办法,经研究决定:", 16, "仿宋", false, ParagraphAlignment.LEFT, true, null);
setFontFormat(pZw, " ", 16, "仿宋", false, ParagraphAlignment.LEFT, false, null);
setFontFormat(pZw, xmInZw, 16, "仿宋", false, ParagraphAlignment.LEFT, false, UnderlinePatterns.SINGLE);
if(rs>1){
setFontFormat(pZw, "等", 16, "仿宋", false, ParagraphAlignment.LEFT, false, null);
setFontFormat(pZw, String.valueOf(rsStr), 16, "仿宋", false, ParagraphAlignment.LEFT, false, UnderlinePatterns.SINGLE);
setFontFormat(pZw, "名", 16, "仿宋", false, ParagraphAlignment.LEFT, false, null);
}
setFontFormat(pZw, "同志到你处工作", 16, "仿宋", false, ParagraphAlignment.LEFT, false, null);
if(issyq){//试用期
setFontFormat(pZw, "(", 16, "仿宋", false, ParagraphAlignment.LEFT, false, null);
setFontFormat(pZw, "试用期2个月", 16, "华文行楷", false, ParagraphAlignment.LEFT, false, null);
setFontFormat(pZw, ")", 16, "仿宋", false, ParagraphAlignment.LEFT, false, null);
}
setFontFormat(pZw, "。请据此办理相关手续。", 16, "仿宋", false, ParagraphAlignment.LEFT, true, null);
setFontFormat(pZw, " 特此通知。", 16, "仿宋", false, ParagraphAlignment.LEFT, true, null);
run = pZw.createRun();
run.addBreak();//换行
run.addBreak();//换行
setFontFormat(pZw, " "+tzrq, 16, "仿宋", false, ParagraphAlignment.LEFT, true, null);
//调动人员
}else{
//单位名称
setFontFormat(pZw, dwmc, 16, "楷体", false, ParagraphAlignment.LEFT, false, UnderlinePatterns.SINGLE);
setFontFormat(pZw, ":", 16, "楷体", false, ParagraphAlignment.LEFT, true, null);
//内容
setFontFormat(pZw, " 因工作需要,根据岗位聘任管理办法,经研究决定:", 16, "仿宋", false, ParagraphAlignment.LEFT, true, null);
setFontFormat(pZw, " ", 16, "仿宋", false, ParagraphAlignment.LEFT, false, null);
setFontFormat(pZw, xmInZw, 16, "仿宋", false, ParagraphAlignment.LEFT, false, UnderlinePatterns.SINGLE);
if(rs>1){
setFontFormat(pZw, "等", 16, "仿宋", false, ParagraphAlignment.LEFT, false, null);
setFontFormat(pZw, String.valueOf(rsStr), 16, "仿宋", false, ParagraphAlignment.LEFT, false, UnderlinePatterns.SINGLE);
setFontFormat(pZw, "名", 16, "仿宋", false, ParagraphAlignment.LEFT, false, null);
}
setFontFormat(pZw, "同志到你处工作,于"+dgrq+"前报到。请据此办理相关手续。", 16, "仿宋", false, ParagraphAlignment.LEFT, true, null);
setFontFormat(pZw, " 特此通知。", 16, "仿宋", false, ParagraphAlignment.LEFT, true, null);
run = pZw.createRun();
run.addBreak();//换行
setFontFormat(pZw, " "+tzrq, 16, "仿宋", false, ParagraphAlignment.LEFT, true, null);
run = pZw.createRun();
run.addBreak();//换行
if(csdwmc!=null && !"".equals(csdwmc)){
setFontFormat(pZw, "抄送:", 16, "仿宋", true, ParagraphAlignment.LEFT, false, null);
String[] csdwmcList = csdwmc.split(",");
for(int i=0; i<csdwmcList.length; i++){
if(i<csdwmcList.length-1){
setFontFormat(pZw, csdwmcList[i]+",", 16, "仿宋", false, ParagraphAlignment.LEFT, false, null);
}else if(i==csdwmcList.length-1){
setFontFormat(pZw, csdwmcList[i]+"。", 16, "仿宋", false, ParagraphAlignment.LEFT, true, null);
}else{
}
}
}
run = pZw.createRun();
run.addBreak();//换行
}
setFontFormat(pZw, " (附名单)", 16, "仿宋", false, ParagraphAlignment.LEFT, true, null);
// 表单
XWPFTable table = doc.createTable(ryds.rowCount()+1, 4);
// 表格宽度
CTTblPr tablePr = table.getCTTbl().addNewTblPr();
CTTblWidth width = tablePr.addNewTblW();
width.setW(BigInteger.valueOf(4000));
tablePr = table.getCTTbl().addNewTblPr();
width = tablePr.addNewTblW();
width.setW(BigInteger.valueOf(10000));
//设置高度
table.getRow(0).setHeight(400);
for(int i=1; i<table.getNumberOfRows(); i++){
table.getRow(i).setHeight(510);
}
// 表格
List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
XWPFTableCell cell = tableCells.get(0);
XWPFParagraph pTable = new XWPFParagraph(cell.getCTTc().addNewP(), cell);
setFontFormat(pTable, "序号", 14, "黑体", false, ParagraphAlignment.CENTER, false, null);
cell = tableCells.get(1);
pTable = new XWPFParagraph(cell.getCTTc().addNewP(), cell);
setFontFormat(pTable, "姓名", 14, "黑体", false, ParagraphAlignment.CENTER, false, null);
cell = tableCells.get(2);
pTable = new XWPFParagraph(cell.getCTTc().addNewP(), cell);
if(isxz){
setFontFormat(pTable, "岗位", 14, "黑体", false, ParagraphAlignment.CENTER, false, null);
}else{
setFontFormat(pTable, "原岗位", 14, "黑体", false, ParagraphAlignment.CENTER, false, null);
}
cell = tableCells.get(3);
pTable = new XWPFParagraph(cell.getCTTc().addNewP(), cell);
setFontFormat(pTable, "备注", 14, "黑体", false, ParagraphAlignment.CENTER, false, null);
// run = pTable.createRun();
// pTable.setAlignment(ParagraphAlignment.CENTER);//左右居中
// pTable.setVerticalAlignment(TextAlignment.CENTER);//上下居中
// run.setText("备注");
for(int i=0; i<ryds.rowCount(); i++){
tableCells = table.getRow(i+1).getTableCells();
cell = tableCells.get(0);
pTable = new XWPFParagraph(cell.getCTTc().addNewP(), cell);
setFontFormat(pTable, String.valueOf(i+1), 12, "仿宋", false, ParagraphAlignment.CENTER, false, null);
cell = tableCells.get(1);
pTable = new XWPFParagraph(cell.getCTTc().addNewP(), cell);
setFontFormat(pTable, ryds.getString(i, "xm"), 12, "仿宋", false, ParagraphAlignment.CENTER, false, null);
cell = tableCells.get(2);
pTable = new XWPFParagraph(cell.getCTTc().addNewP(), cell);
setFontFormat(pTable, ryds.getString(i, "gw"), 12, "仿宋", false, ParagraphAlignment.LEFT, false, null);
cell = tableCells.get(3);
pTable = new XWPFParagraph(cell.getCTTc().addNewP(), cell);
setFontFormat(pTable, ryds.getString(i, "bz"), 12, "仿宋", false, ParagraphAlignment.LEFT, false, null);
}
// FileOutputStream out;
// try {
// out = new FileOutputStream("C:/Users/xbx/Desktop/人员调动通知书.docx");
// doc.write(out);
// out.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
return doc;
}
/**
* 设置生成word时 内容的格式
* @author xbx
* @date 2018-01-15
* @param paragraph 段落
* @param content 文字内容
* @param fontSize 字体大小
* @param fontTypeStyle 字体类型
* @param isBold 是否加粗
* @param horizontalPosition 水平位置
* @param isAddBreak 是否换行
* @param underLineStytle 下换线样式
*/
public static void setFontFormat(XWPFParagraph paragraph, String content, int fontSize, String fontTypeStyle, boolean isBold, ParagraphAlignment horizontalPosition, boolean isAddBreak, UnderlinePatterns underLineStytle){
//水平位置
if(horizontalPosition!=null && !"".equals(horizontalPosition)){
paragraph.setAlignment(horizontalPosition);
}
XWPFRun run = paragraph.createRun();//一个XWPFRun代表具有相同属性的一个区域。
run.setText(content);//内容
run.setFontSize(fontSize);//字体大小
//换行
if(isAddBreak){
run.addBreak();
}
//下划线样式
if(underLineStytle!=null && !"".equals(underLineStytle)){
run.setUnderline(underLineStytle);
}
//是否加粗
run.setBold(isBold);
//字体
if(fontTypeStyle!=null && !"".equals(fontTypeStyle)){
CTRPr rpr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : run.getCTR().addNewRPr();
CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts();
fonts.setEastAsia(fontTypeStyle);//汉字字体
fonts.setAscii(fontTypeStyle);//数字字体
fonts.setHAnsi(fontTypeStyle);//括号符号等字体
}
}
poi 生成word 示例
于 2018-01-23 14:21:21 首次发布