java 利用poi 工具生成word表单 的简单实现

来源:互联网 发布:平淡日子里的刺 知乎 编辑:程序博客网 时间:2024/06/04 18:02

一、简单的word表单样式



二、实现代码:

因为都是生成word文档,controller层可以和之前生成word文档复用,所以在此不对赘述,只展示关键方法内代码

/**
 * 
 * @Description:生成word 并把word 流 和录音流放入 fileMap中
 * @Date:2015-12-17
 * @return
 */
private void CreateWord(VisitApply info) {


XWPFDocument doc = new XWPFDocument();
ByteArrayOutputStream bt = new ByteArrayOutputStream();
 
try {
    

XWPFTable table1 = doc.createTable(3, 3);//创建一个几x几的表格
//设置单元格样式 单元格宽:
CTTbl ttbl1 = table1.getCTTbl();
CTTblPr tblPr1 = ttbl.getTblPr() == null ? ttbl1.addNewTblPr() : ttbl1.getTblPr();
CTTblWidth tblWidth1 = tblPr.isSetTblW() ? tblPr1.getTblW() : tblPr1.addNewTblW();
CTJc cTJc1=tblPr1.addNewJc();
cTJc1.setVal(STJc.Enum.forString("center"));
tblWidth1.setW(new BigInteger("8000"));
tblWidth1.setType(STTblWidth.DXA);

table1.getRow(0).getCell(0).setText("aaaaa");
table1.getRow(0).getCell(1).setText("aaaaa");
table1.getRow(0).getCell(2).setText("aaaaa");
table1.getRow(1).getCell(0).setText("aaaaa");
table1.getRow(1).getCell(1).setText("aaaaa");
table1.getRow(1).getCell(2).setText("aaaaa");
table1.getRow(2).getCell(0).setText("aaaaa");
table1.getRow(2).getCell(1).setText("aaaaa");
table1.getRow(2).getCell(2).setText("aaaaa");

doc.write(bt);
//word文件名拼装
String wordFileName="外访报告信息填写模板";
   wordFileName+="-"+DateUtil.formatDate(new Date())+".docx";
   
   //word 放入fileMap中
   fileMap.put(wordFileName, bt.toByteArray());
   
  // bt.close();
 
} catch (Exception e) {
e.printStackTrace();
log.error("create word {}" + e.getMessage());

}

这样就能生成一个简单的3*3的表格,

假如你想要生成一个复杂的表格,类似报名表之类的,有大有小的,这里提供一个简单的方法就是在创建一个table对象设置不同的行数,和其他宽度设置,这样就能在下面继续拼接行列展示不同样式,具体设置样式的文章,以后会归类统计。

谢谢观看
原创粉丝点击