首页 > 代码库 > iText导出word入门
iText导出word入门
最近一个项目需要导出word文档,网上了解了一下,发现poi导出excel比较好,但是导出word就力不从心了,大家都比较推荐iText,于是试了试,感觉还不错。
我用的是2.1.7版本。
1. 使用maven导入依赖
<dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext-rtf</artifactId> <version>2.1.7</version> </dependency>
还有一个包是itextasian-1.5.2.jar, 这是在各个maven库没有找到,而我因为一些原因不方便建自己的maven库,所以只好直接在项目里面添加依赖。
2. 贴上完整的代码
package com.sofree.web.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; import com.lowagie.text.BadElementException; import com.lowagie.text.Cell; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Table; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.rtf.RtfWriter2; import com.sofree.business.model.TaKyxmApplyperson; import com.sofree.business.model.TaKyxmApplyteacher; import com.sofree.business.model.TaKyxmProjectinfo; /** * @author Jack * */ public class ExportWordUtil { public static ByteArrayOutputStream exportProjectWord(TaKyxmProjectinfo obj) { try { // 设置纸张大小 Document document = new Document(PageSize.A4); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); // 建立一个书写器,与document对象关联 RtfWriter2.getInstance(document, buffer); document.open(); // 设置中文字体 BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // 正文字体风格 Font contextFont = new Font(bfChinese, 10, Font.NORMAL); document.add(getTitle(bfChinese)); document.add(getProjectName(contextFont, obj)); // 设置Table表格,创建一个六列的表格 Table table = new Table(6); int width[] = { 5, 10, 15, 25, 15, 30 };// 设置每列宽度比例 table.setWidths(width); table.setWidth(95);// 占页面宽度比例 table.setAlignment(Element.ALIGN_CENTER);// 居中 //table.setAlignment(Element.ALIGN_MIDDLE);// 垂直居中 table.setAutoFillEmptyCells(true);// 自动填满 table.setBorderWidth(1);// 边框宽度 table.setPadding(5); // 设置表头 /*Cell haderCell = new Cell("表格表头"); haderCell.setHeader(true); haderCell.setColspan(4); table.addCell(haderCell); table.endHeaders();*/ Font fontChinese = new Font(bfChinese, 10, Font.NORMAL); table.addCell(getCell("项目名称", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell(obj.getXmmc(), 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("项目负责人", fontChinese)); table.addCell(getCell(obj.getOwner().getYhm(),fontChinese)); table.addCell(getCell("申请经费", 1, 2, Element.ALIGN_MIDDLE,fontChinese)); table.addCell(getCell(String.valueOf(obj.getSqjf()), 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("依托实验室名称", fontChinese)); table.addCell(getCell(obj.getLab().getSysmc(), fontChinese)); List<TaKyxmApplyperson> stuList = obj.getStudentList(); table.addCell(getCell("团队成员", stuList.size() + 1, 1, Element.ALIGN_CENTER, fontChinese)); table.addCell(getCell("姓名", fontChinese)); table.addCell(getCell("学号", fontChinese)); table.addCell(getCell("所学专业", fontChinese)); table.addCell(getCell("联系电话", fontChinese)); table.addCell(getCell("E-mail", fontChinese)); for(TaKyxmApplyperson stu : stuList){ table.addCell(getCell(stu.getXm(), fontChinese)); table.addCell(getCell(stu.getXh(), fontChinese)); table.addCell(getCell(stu.getSxzy(), fontChinese)); table.addCell(getCell(stu.getLxdh(), fontChinese)); table.addCell(getCell(stu.getEmail(), fontChinese)); } List<TaKyxmApplyteacher> teaList = obj.getTeacherList(); table.addCell(getCell("指导教师", teaList.size() + 1, 1, Element.ALIGN_CENTER, fontChinese)); table.addCell(getCell("姓名", fontChinese)); table.addCell(getCell("单位", fontChinese)); table.addCell(getCell("职务/职称", fontChinese)); table.addCell(getCell("联系电话", fontChinese)); table.addCell(getCell("E-mail", fontChinese)); for(TaKyxmApplyteacher tea : teaList){ table.addCell(getCell(tea.getXm(), fontChinese)); table.addCell(getCell(tea.getDw(), fontChinese)); table.addCell(getCell(tea.getZw(), fontChinese)); table.addCell(getCell(tea.getLxdh(), fontChinese)); table.addCell(getCell(tea.getEmail(), fontChinese)); } table.addCell(getCell("立项的意义及预期达到的效果", 1, 2, Element.ALIGN_MIDDLE,fontChinese)); Paragraph lxyyxg = new Paragraph(obj.getLxyyxg(), fontChinese); lxyyxg.setAlignment(Element.ALIGN_LEFT); lxyyxg.setFirstLineIndent(20); lxyyxg.add(Chunk.NEWLINE); lxyyxg.add(Chunk.NEWLINE); table.addCell(getCell(lxyyxg, 1, 4, Element.ALIGN_MIDDLE)); Paragraph xmzynr = new Paragraph(obj.getXmzynr(), fontChinese); xmzynr.setAlignment(Element.ALIGN_LEFT); xmzynr.setFirstLineIndent(20); xmzynr.add(Chunk.NEWLINE); xmzynr.add(Chunk.NEWLINE); table.addCell(getCell("项目的主要内容、实验方案设计及创新点", 1, 2, Element.ALIGN_MIDDLE,fontChinese)); table.addCell(getCell(xmzynr, 1, 4, Element.ALIGN_MIDDLE)); table.addCell(getCell("经费具体预算(元)", 7, 2, Element.ALIGN_MIDDLE,fontChinese)); table.addCell(getCell("科目", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("预算明细", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("实验耗材", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("实验小工具", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("加工费", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("机时费", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("其它", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("合计:", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("", 1, 2, Element.ALIGN_MIDDLE, fontChinese)); table.addCell(getCell("指导教师意见", 1, 2, Element.ALIGN_MIDDLE,fontChinese)); Paragraph pa = new Paragraph("", fontChinese); pa.add(Chunk.NEWLINE); pa.add(Chunk.NEWLINE); pa.add(Chunk.NEWLINE); pa.add(Chunk.NEWLINE); pa.add(Chunk.NEWLINE); pa.add(Chunk.NEWLINE); Phrase p1 = new Phrase(" 负责人签字:"); pa.add(p1); pa.add(Chunk.NEWLINE); pa.add(new Phrase(" 年 月 日")); table.addCell(getCell(pa, 1, 4, Element.ALIGN_MIDDLE)); table.addCell(getCell("依托实验室意见(可行性)", 1, 2, Element.ALIGN_MIDDLE,fontChinese)); table.addCell(getCell(pa, 1, 4, Element.ALIGN_MIDDLE)); table.addCell(getCell("组织申报单位意见", 1, 2, Element.ALIGN_MIDDLE,fontChinese)); table.addCell(getCell(pa, 1, 4, Element.ALIGN_MIDDLE)); Paragraph pa2 = new Paragraph("", fontChinese); pa2.add(Chunk.NEWLINE); pa2.add(Chunk.NEWLINE); pa2.add(Chunk.NEWLINE); pa2.add(Chunk.NEWLINE); pa2.add(Chunk.NEWLINE); pa2.add(Chunk.NEWLINE); Phrase p2 = new Phrase(" 组长:"); pa2.add(p2); pa2.add(Chunk.NEWLINE); Phrase p3 = new Phrase(" 成员:"); pa2.add(p3); pa2.add(Chunk.NEWLINE); pa2.add(new Phrase(" 年 月 日")); table.addCell(getCell("学校专家组意见", 1, 2, Element.ALIGN_MIDDLE,fontChinese)); table.addCell(getCell(pa2, 1, 4, Element.ALIGN_MIDDLE)); table.addCell(getCell("学校审批意见", 1, 2, Element.ALIGN_MIDDLE,fontChinese)); table.addCell(getCell(pa, 1, 4, Element.ALIGN_MIDDLE)); document.add(table); document.close(); return buffer; } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } private static Cell getCell(String content, Font font){ return getCell(content, 1, 1, Element.ALIGN_MIDDLE, font); } private static Cell getCell(String content, int rowSpan, int colSpan, int align, Font font){ Paragraph pa = new Paragraph(content, font); pa.setAlignment(Element.ALIGN_CENTER); return getCell(pa, rowSpan, colSpan, align); } private static Cell getCell(Paragraph content, int rowSpan, int colSpan, int align){ try { Cell cell = new Cell(content); cell.setColspan(colSpan); cell.setRowspan(rowSpan); cell.setUseAscender(true); cell.setVerticalAlignment(align); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setLeading(30); return cell; } catch (BadElementException e) { e.printStackTrace(); } return null; } private static Paragraph getTitle(BaseFont bfChinese){ Paragraph title = new Paragraph("某某大学教学实验室开放基金立项申请表"); // 设置标题格式对齐方式 title.setAlignment(Element.ALIGN_CENTER); // 标题字体风格 Font titleFont = new Font(bfChinese, 12, Font.BOLD); title.setFont(titleFont); return title; } private static Paragraph getProjectName(Font contextFont, TaKyxmProjectinfo obj){ Paragraph context = new Paragraph(); context.add(Chunk.NEWLINE); context.add(Chunk.NEWLINE); context.setAlignment(Element.ALIGN_LEFT); context.setFont(contextFont); // 段间距 //context.setSpacingBefore(1); // 设置第一行空的列数 context.setFirstLineIndent(20); Phrase p1 = new Phrase("申报单位名称(公章):" + obj.getSbdw().getDwmc()); context.add(p1); Phrase p2 = new Phrase(" 编号:" + obj.getXmbh()); context.add(p2); return context; } }
生成出来的大概效果如图:
这里有几个地方要注意:
1. 设置表格居中,我开始是设置 cell.setVerticalAlignment(Element.ALIGN_CENTER); 但是没有效果,后来设置对cell里面的Paragraph设置居中,就有效果了
Paragraph pa = new Paragraph(content, font);
pa.setAlignment(Element.ALIGN_CENTER);
2. 通过Chunk.NEWLINE 可以增加一个新行。
参考资料:
http://www.cnblogs.com/julyluo/archive/2012/06/23/2559580.html
iText导出word入门
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。