用java创建word文档_java创建word文档

import java.awt.Color;

import java.io.FileOutputStream;

import java.util.Date;

import com.lowagie.text.Cell;

import com.lowagie.text.Document;

import com.lowagie.text.Element;

import com.lowagie.text.Font;

import com.lowagie.text.Image;

import com.lowagie.text.PageSize;

import com.lowagie.text.Paragraph;

import com.lowagie.text.Rectangle;

import com.lowagie.text.Table;

import com.lowagie.text.rtf.RtfWriter2;

import com.lowagie.text.rtf.style.RtfFont;

public class CreateWord {

public static void main(String[] args) throws Exception {

/** 创建Document对象(word文档) author:yyli Sep 15, 2010 */

Rectangle rectPageSize = new Rectangle(PageSize.A4);

rectPageSize = rectPageSize.rotate();

// 创建word文档,并设置纸张的大小

Document doc = new Document(PageSize.A4);

String fileName="E:/企业详细信息登记表_"+System.currentTimeMillis()+".doc";

/** 建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中 author:yyli Sep 15, 2010 */

RtfWriter2.getInstance(doc, new FileOutputStream(fileName));

doc.open();

/** 标题字体 author:yyli Sep 15, 2010 */

RtfFont titleFont = new RtfFont("仿宋_GB2312", 15, Font.BOLD,

Color.BLACK);

/** 正文字体 author:yyli Sep 15, 2010 */

RtfFont contextFont = new RtfFont("仿宋_GB2312", 9, Font.NORMAL,

Color.BLACK);

/** 表格设置 author:yyli Sep 15, 2010 */

Table table = new Table(4, 3);

int[] withs = { 15, 35, 15, 35};

/** 设置每列所占比例 author:yyli Sep 15, 2010 */

table.setWidths(withs);

/** 表格所占页面宽度 author:yyli Sep 15, 2010 */

table.setWidth(100);

/** 居中显示 author:yyli Sep 15, 2010 */

table.setAlignment(Element.ALIGN_CENTER);

/** 自动填满 author:yyli Sep 15, 2010 */

table.setAutoFillEmptyCells(true);

table.setBorderWidth(5); // 边框宽度

table.setBorderColor(new Color(0, 125, 255)); // 边框颜色

table.setPadding(12);// 衬距,看效果就知道什么意思了

table.setSpacing(0);// 即单元格之间的间距

table.setBorder(5);// 边框

/** 第一行(标题) author:yyli Sep 15, 2010 */

String titleString = "企业详细信息登记表";

Paragraph title = new Paragraph(titleString);

// 设置标题格式对其方式

title.setAlignment(Element.ALIGN_CENTER);

title.setFont(titleFont);

doc.add(title);

/** 第二行(正文) author:yyli Sep 15, 2010 */

String contextString = "登记人:"+"admin "+"登记时间:"+new Date().toLocaleString();

Paragraph context = new Paragraph(contextString);

// 正文格式对齐方式

context.setAlignment(Element.ALIGN_RIGHT);

context.setFont(contextFont);

// 与上一段落(标题)的行距

context.setSpacingBefore(10);

// 设置第一行空的列数(缩进)

// context.setFirstLineIndent(20);

doc.add(context);

Cell cell=null;

cell=new Cell("企业名称:");

cell.setHeader(true);

cell.setVerticalAlignment(Element.ALIGN_CENTER);

cell.setHorizontalAlignment(Element.ALIGN_RIGHT);

table.addCell(cell);

cell=new Cell("中国安芯");

cell.setColspan(3);

cell.setVerticalAlignment(Element.ALIGN_CENTER);

cell.setHorizontalAlignment(Element.ALIGN_LEFT);

table.addCell(cell);

cell=new Cell("企业地址:");

cell.setVerticalAlignment(Element.ALIGN_CENTER);

cell.setHorizontalAlignment(Element.ALIGN_RIGHT);

table.addCell(cell);

cell=new Cell("淮安市洪泽县");

cell.setVerticalAlignment(Element.ALIGN_CENTER);

cell.setHorizontalAlignment(Element.ALIGN_LEFT);

table.addCell(cell);

cell=new Cell("企业代码:");

cell.setVerticalAlignment(Element.ALIGN_CENTER);

cell.setHorizontalAlignment(Element.ALIGN_RIGHT);

table.addCell(cell);

cell=new Cell("zxc797ddd797979");

cell.setVerticalAlignment(Element.ALIGN_CENTER);

cell.setHorizontalAlignment(Element.ALIGN_LEFT);

table.addCell(cell);

doc.add(table);

//在表格末尾添加图片

Image png = Image.getInstance("E:/baby.jpg");

doc.add(png);

doc.close();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值