itext生成word文档方法
- 时间:2015年04月02日 11:00:12 来源:魔法猪系统重装大师官网 人气:19171
需要的jar包:iTextAsian.jar,iText-rtf-2.1.3.jar,iText-2.1.3.jar;
参考代码:
response.addHeader("Content-disposition", "attachment;filename=\"" + new String("信息处理表.doc".getBytes("gb2312"), "ISO8859-1"));
String letrId = request.getParameter("letrId");
// 创建word文档,并设置纸张的大小
com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4);
try {
RtfWriter2.getInstance(document,"e:\123.doc");
document.open();
Font f = new Font();
String topic= "处理反馈表";
Paragraph p = new Paragraph(topic, new Font(Font.NORMAL, 18,Font.BOLDITALIC, new Color(0, 0, 0)));
p.setAlignment(1);
document.add(p);
// 设置中文字体
BaseFont bfFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
//创建有三列的表格
Table table = new Table(3);
table.setBorderWidth(1);
table.setBorderColor(Color.BLACK);
table.setPadding(0);
table.setSpacing(0);
//添加表头的元素
Cell cell = new Cell(title);// 单元格
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setHeader(true);
cell.setColspan(3);// 设置表格为三列
cell.setRowspan(2);// 设置表格为2行
table.addCell(cell);
table.endHeaders();// 表头结束
cell = new Cell("时间:");
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new Cell(new Paragraph((vl.getLetrDate()).toString()));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setColspan(2);
table.addCell(cell);
cell = new Cell("重要程度:");
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new Cell(new Paragraph((vl.getImptLevel()).toString()));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setColspan(2);
table.addCell(cell);
//测试1
cell = new Cell("测试1:");
cell.setRowspan(5);// 当前单元格占两行,纵向跨度
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
table.addCell("姓名:");
table.addCell(vl.getPernName());
table.addCell("性别:");
table.addCell(vl.getPernSex());
table.addCell("公司:");
table.addCell(vl.getPernUnit());
table.addCell("职务:");
table.addCell(vl.getPernPost());
table.addCell("地址:");
table.addCell(vl.getPernAddress());
// 测试2
cell = new Cell("测试2:");
cell.setRowspan(4);// 当前单元格占两行,纵向跨度
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
table.addCell("姓名:");
table.addCell(vl.getWrirName());
table.addCell("单位 :");
table.addCell(vl.getWrirOrganization());
table.addCell("性别:");
table.addCell(vl.getWrirSex());
table.addCell("电话:");
table.addCell(vl.getWrirTnur());
// 简要内容
cell = new Cell("简要内容:");
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new Cell(new Paragraph(vl.getLetrNote()));
cell.setColspan(2);
table.addCell(cell);
// 测试3
cell = new Cell("测试3:");
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new Cell(new Paragraph(vl.getDeptVerifyOpinion()));
cell.setColspan(2);
table.addCell(cell);
// 测试4
cell = new Cell("测试4:");
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new Cell(new Paragraph(vl.getChargeVerifyOpinion()));
cell.setColspan(2);
table.addCell(cell);
//测试5
cell = new Cell("测试5:");
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new Cell(new Paragraph(vl.getHandleContent()));
cell.setColspan(2);
table.addCell(cell);
document.add(table);
document.add(new Paragraph("\n"));
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
在word中可以按照你所要求的格式,利用itext的table、cell、图片等“画”出想要的文档格式,然后再将数据往其中填充;如果想要将报表导出成pdf格式的,用itext也可以很轻松的实现。