Java使用iText生成word文件的完美解决方案(亲测可行)
JAVA生成WORD文件的方法目前有以下种:
一种是jacob 但是局限于windows平台 往往许多JAVA程序运行于其他操作系统 在此不讨论该方案
一种是pio但是他的excel处理很程序 word模块还局限于读取word的文本内容,写word文件就更弱项了
当我使用这个JAVA生成RTF文件时费了好大的劲,原本是想生成WORD文档的,但是WORD文档POI只支持往生成的WORD中填入文本,对于图片根本就不支持。后来想想,RTF格式的也可用WORD打开,不如生成RTF。结果上网搜了很多技术,Itext,jacob,java2word,rtftemplate,reportrunner看了近一天也没什么头绪,写这些示例的几乎没有,还好Itext的例子有那么几个,可是我上官网下了最新核心包后发现,导入例子中居然全部红叉,原本以为上错网站了,再经过核实还是对的,于是断定网上的例子肯定有误,itext或许不能用。绕了大半天其他的技术我真的没法弄了,还是回到了iText,静下心来思考觉得包肯定有问题,仔细一看原来最新版的是支持PDF版的iText-5.0.1.jar是不对的,本来以为最新的功能最全了,没想到错了,想到这里赶紧下了稍微iText-2.1.7.jar结果终于成功了,感慨不已!现贡献代码如下记住官网上只能下到核心包:iText-1.2.7.jar和支持rtf的iText-rtf-2.1.7.jar这两个貌似对了,其实还有一个包是比较重要的iTextAsian.jar这个包对于设置字体什么的起了关键作用上网可以搜到的.
本文介绍的是itext生成rtf文件并保存格式为word 此方案本人已实践过 并已在项目中使用
用到的jar包:
iText-2.1.7.jar
iText-rtf-2.1.7.jar
iTextAsian.jar
- package com.rye.test;
- import java.awt.Color;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import com.lowagie.text.Cell;
- import com.lowagie.text.Document;
- import com.lowagie.text.DocumentException;
- import com.lowagie.text.Font;
- import com.lowagie.text.PageSize;
- import com.lowagie.text.Paragraph;
- import com.lowagie.text.Table;
- import com.lowagie.text.rtf.RtfWriter2;
- /**
- * 创建word文档 步骤:
- * 1,建立文档
- * 2,创建一个书写器
- * 3,打开文档
- * 4,向文档中写入数据
- * 5,关闭文档
- */
- public class WordDemo {
- public WordDemo() {
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- // 创建word文档,并设置纸张的大小
- Document document = new Document(PageSize.A4);
- try {
- RtfWriter2.getInstance(document,
- new FileOutputStream("E:/word.doc"));
- document.open();
- //设置合同头
- Paragraph ph = new Paragraph();
- Font f = new Font();
- Paragraph p = new Paragraph("出口合同",
- new Font(Font.NORMAL, 18, Font.BOLDITALIC, new Color(0, 0, 0)) );
- p.setAlignment(1);
- document.add(p);
- ph.setFont(f);
- // 设置中文字体
- // BaseFont bfFont =
- // BaseFont.createFont("STSongStd-Light",
- "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
- // Font chinaFont = new Font();
- /*
- * 创建有三列的表格
- */
- Table table = new Table(4);
- document.add(new Paragraph("生成表格"));
- table.setBorderWidth(1);
- table.setBorderColor(Color.BLACK);
- table.setPadding(0);
- table.setSpacing(0);
- /*
- * 添加表头的元素
- */
- Cell cell = new Cell("表头");//单元格
- cell.setHeader(true);
- cell.setColspan(3);//设置表格为三列
- cell.setRowspan(3);//设置表格为三行
- table.addCell(cell);
- table.endHeaders();// 表头结束
- // 表格的主体
- cell = new Cell("Example cell 2");
- cell.setRowspan(2);//当前单元格占两行,纵向跨度
- table.addCell(cell);
- table.addCell("1,1");
- table.addCell("1,2");
- table.addCell("1,3");
- table.addCell("1,4");
- table.addCell("1,5");
- table.addCell(new Paragraph("用java生成的表格1"));
- table.addCell(new Paragraph("用java生成的表格2"));
- table.addCell(new Paragraph("用java生成的表格3"));
- table.addCell(new Paragraph("用java生成的表格4"));
- document.add(new Paragraph("用java生成word文件"));
- document.add(table);
- document.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (DocumentException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
代码2:
- <span style="">import java.awt.Color;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import com.lowagie.text.Cell;
- 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.Table;
- import com.lowagie.text.pdf.BaseFont;
- import com.lowagie.text.rtf.RtfWriter2;
- public class CreateWordDemo {
- public void createDocContext(String file,String contextString)throws DocumentException, IOException{
- //设置纸张大小
- Document document = new Document(PageSize.A4);
- //建立一个书写器,与document对象关联
- RtfWriter2.getInstance(document, new FileOutputStream(file));
- document.open();
- //设置中文字体
- BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
- //标题字体风格
- Font titleFont = new Font(bfChinese,12,Font.BOLD);
- //正文字体风格
- Font contextFont = new Font(bfChinese,10,Font.NORMAL);
- Paragraph title = new Paragraph("标题");
- //设置标题格式对齐方式
- title.setAlignment(Element.ALIGN_CENTER);
- title.setFont(titleFont);
- document.add(title);
- Paragraph context = new Paragraph(contextString);
- context.setAlignment(Element.ALIGN_LEFT);
- context.setFont(contextFont);
- //段间距
- context.setSpacingBefore(3);
- //设置第一行空的列数
- context.setFirstLineIndent(20);
- document.add(context);
- //设置Table表格,创建一个三列的表格
- Table table = new Table(3);
- int width[] = {25,25,50};//设置每列宽度比例
- table.setWidths(width);
- table.setWidth(90);//占页面宽度比例
- table.setAlignment(Element.ALIGN_CENTER);//居中
- table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
- table.setAutoFillEmptyCells(true);//自动填满
- table.setBorderWidth(1);//边框宽度
- //设置表头
- Cell haderCell = new Cell("表格表头");
- haderCell.setHeader(true);
- haderCell.setColspan(3);
- table.addCell(haderCell);
- table.endHeaders();
- Font fontChinese = new Font(bfChinese,12,Font.NORMAL,Color.GREEN);
- Cell cell = new Cell(new Paragraph("这是一个3*3测试表格数据",fontChinese));
- cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- table.addCell(cell);
- table.addCell(new Cell("#1"));
- table.addCell(new Cell("#2"));
- table.addCell(new Cell("#3"));
- document.add(table);
- document.close();
- }
- public static void main(String[] args) {
- CreateWordDemo word = new CreateWordDemo();
- String file = "test.doc";
- try {
- word.createDocContext(file, "测试iText导出Word文档");
- } catch (DocumentException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }</span>
图片版:
- <span style="font-size: medium;">/**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- try {
- RTFCreate rtfMain = new RTFCreate();
- rtfMain.createRTFContext(FILE_NAME);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (DocumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void createRTFContext(String path) throws DocumentException,
- IOException {
- Document document = new Document(PageSize.A4);
- RtfWriter2.getInstance(document, new FileOutputStream(path));
- document.open();
- // 设置中文字体
- BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
- "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- // 标题字体风格
- Font titleFont = new Font(bfChinese, 12, Font.BOLD);
- // 正文字体风格
- Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
- Paragraph title = new Paragraph("标题");
- // 设置标题格式对齐方式
- title.setAlignment(Element.ALIGN_CENTER);
- title.setFont(titleFont);
- document.add(title);
- String contextString = "iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。使用iText与PDF能够使你正确的控制Servlet的输出。";
- Paragraph context = new Paragraph(contextString);
- // 正文格式左对齐
- context.setAlignment(Element.ALIGN_LEFT);
- context.setFont(contextFont);
- // 离上一段落(标题)空的行数
- context.setSpacingBefore(20);
- // 设置第一行空的列数
- context.setFirstLineIndent(20);
- document.add(context);
- // //在表格末尾添加图片
- Image png = Image.getInstance("c:/fruit.png");
- document.add(png);
- document.close();
- }
- }
- </span>
Java使用iText生成word文件的完美解决方案(亲测可行)的更多相关文章
- Java 使用模板生成 Word 文件---基于 Freemarker 模板框架
Java项目引入 Freemarker 插件自行完成. 步骤如下: .编写 Word 模板,并将模板中要用代码动态生成数据用 Freemarker 变量取代,即${变量名},如${username}: ...
- 关于java poi itext生成pdf文件的例子以及方法
最近正在做导出pdf文件的功能,所以查了了一些相关资料,发现不是很完善,这里做一些小小的感想,欢迎各位“猿”童鞋批评指正. poi+itext,所需要的jar包有itext-2.1.7.jar,poi ...
- Windows10没有修改hosts文件权限的解决方案(亲测有效)
当遇到有hosts文件不会编辑或者,修改了没办法保存”,以及需要权限等问题如图: 或者这样: 我学了一招,现在教给你: 1.win+R 2.进入hosts的文件所在目录: 3.我们开始如何操作才能不出 ...
- Java Itext 生成PDF文件
利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.O ...
- java使用iText生成pdf表格
转载地址:http://www.open-open.com/code/view/1424011530749 首先需要你自己下载itext相关的jar包并添加引用,或者在maven中添加如下引用配置: ...
- Java利用poi生成word(包含插入图片,动态表格,行合并)
转(小改): Java利用poi生成word(包含插入图片,动态表格,行合并) 2018年12月20日 09:06:51 wjw_11093010 阅读数:70 Java利用poi生成word(包含插 ...
- 利用html模板生成Word文件(服务器端不需要安装Word)
利用html模板生成Word文件(服务器端不需要安装Word) 由于管理的原因,不能在服务器上安装Office相关组件,所以只能采用客户端读取Html模板,后台对模板中标记的字段数据替换并返回给客户端 ...
- JSP生成word文件
1.jsp生成word文件,直接改动jsp格式: <%@ page contentType="application/vnd.ms-word;charset=GB2312"% ...
- ORA-03113:通信通道的文件结尾-完美解决方案
ORA-03113:通信通道的文件结尾-完美解决方案 oracle 文档中对这个错误这样解释: ORA-03113 错误就是说连接到数据库的网络中断了.有些错误由于频繁出现.原因复杂而被 Orac ...
随机推荐
- 标准webservice调用
现代定义的webservice一般都倾向于restfull风格的http请求,但工作中还是会遇到前辈们写的时代代码. 我们更倾向于封装代码来调用,而不是服务引用.请看: Service.asmx服务的 ...
- 如何查找消耗资源较大的SQL
对于优化来讲,查找消耗资源较大的SQL至关重要,下面介绍几个之前用到的SQL. 1.从V$SQLAREA中查询最占用资源的查询. select b.username username,a.disk_r ...
- springmvc4集成swagger2
首先在原有的springmvc工程的pom文件中增加swagger <dependency> <groupId>io.springfox</groupId> < ...
- (微信小程序)二 : 创建一个页面。
首先先看一下pages的目录结构吧. 我创建了一个topics页面.3个文件全创建好了之后 我往topics.js添加数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- python模块之contexlib
一.上下文管理器 with是python实现上下文管理器的核心关键词.它能够在代码执行前和执行后做一些额外的事情. 最常见的代码恐怕就是文件操作了. with open("file" ...
- LVS(Linux Virtual Server)
LVS的英文全称是Linux Virtual Server,即Linux虚拟服务器.它是我国的章文嵩博士的一个开源项目.在linux内存2.6中,它已经成为内核的一部分,在此之前的内核版本则需 ...
- HDU 2167 Pebbles 状态压缩dp
Pebbles Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 基于Maven的Spring + Spring MVC + Mybatis的环境搭建
基于Maven的Spring + Spring MVC + Mybatis的环境搭建项目开发,先将环境先搭建起来.上次做了一个Spring + Spring MVC + Mybatis + Log4J ...
- SQL COUNT DISTINCT
Create table trade ( sell_id int, --卖家 buy_id int, -- 卖家 time date --交易时间 ) sell_id, buy_id, time s ...
- aop postsharp的使用:在方法进入/成功/失败/退出时获取方法名和参数值
1.nuget安装postsharp 2.编写attribute标记 [Serializable] [AttributeUsage(AttributeTargets.Method, AllowMult ...