转自【http://www.31km.cn/post/450.html】
JAVA生成WORD文件的方法目前有以下种:
一种是jacob 但是局限于windows平台 往往许多JAVA程序运行于其他操作系统 在此不讨论该方案
一种是pio但是他的excel处理很程序 word模块还局限于读取word的文本内容,写word文件就更弱项了
本文介绍的是itext生成rtf文件并保存格式为word 此方案本人已实践过 并已在项目中使用
用到的jar包:
iText-2.1.5.jar
iText-rtf-2.1.4.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();
}
}
}
分享到:
相关推荐
客户要求用程序生成标准的word文档,要能打印,而且不能变形,以前用过很多解决方案,都在客户严格要求下牺牲的无比惨烈。 POI读word文档还行,写文档实在不敢恭维,复杂的样式很难控制不提,想象一下一个20多页,...
exportDoc该demo是使用Itext实现Java生成word文档,当然,还有其他方式可以,例如:jacob,但是它局限于windows平台 往往许多JAVA程序运行于其他操作系统 在此不讨论该方案;另一种是pio但是它是excel处理程序 对于...
该代码提供了word、excel、pdf三种类型的文档生成方案,其中word文档使用“freemarker + xml + 模板”技术;excel使用“POI + 模板”技术;pdf使用“freemarker + itext + flying saucer”技术
Java生成密钥、保存密钥的实例源码,通过本源码可以了解到Java如何产生单钥加密的密钥(myKey)、产生双钥的密钥对(keyPair)、如何保存公钥的字节数组、保存私钥到文件privateKey.dat、如何用Java对象序列化保存私钥...
Java生成密钥、保存密钥的实例源码,通过本源码可以了解到Java如何产生单钥加密的密钥(myKey)、产生双钥的密钥对(keyPair)、如何保存公钥的字节数组、保存私钥到文件privateKey.dat、如何用Java对象序列化保存私钥...
每个实例都是经过笔者精心筛选的,具有很强的实用性,其中一些实例是开发人员难于寻觅的解决方案。 本书两卷共计1200个例子,包括了开发中各个方面最常用的实例,是目前市场上实例最全面的开发类图书;本书实例来源...