您的位置:首页 > 编程语言 > Java开发

Java操作Word报告

2016-04-11 14:16 561 查看
一.需要用到两个jar包,这只是针对word,针对pdf的还需要另加jar包。

itext.jar核心包

下载地址:

http://cn.jarfire.org/itext.html

iTextAsian.jar解决word样式、编码问题扩展包

下载地址:

http://cn.jarfire.org/itextasian.html

二.以Java工程为例添加jar包









一.在java工程中的src下新建一个package,在新建一个类,代码如下:

package com.nit.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.Image;
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 WordDemo {

public WordDemo() {
}

/**
* @param args
*/

public static void main(String[] args) {
// 创建word文档,并设置纸张的大小,接下来的参数分别是左、右、上和下页边距。Document document = new
// Document(PageSize.A4,100,100,100,100);
Document document = new Document(PageSize.A4);// 默认都是36边距
try {
// 创建写入器
// 第一个参数是对文档对象的引用,第二个参数是输出的文件.
RtfWriter2.getInstance(document, new FileOutputStream(
"E:/sreffdsaq.doc"));
// 打开文档
document.open();
Paragraph ph = new Paragraph();// 创建段落
ph.setLeading(30);// 行间距
document.add(ph);

// 设置中文字体
BaseFont bfFont = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 调用itextasin.jar中的字体设置(宋体)
Font f1 = new Font(bfFont, 26, Font.BOLD, new Color(0, 0, 255));// 设置字体
Paragraph p1 = new Paragraph("计算机", f1);
p1.setAlignment(1);// 设置居中,1居中,0居左,2居右
document.add(p1);

Font f2 = new Font(bfFont, 20, Font.NORMAL, new Color(0, 100, 100));
Paragraph p2 = new Paragraph(
"生成表格生成表格生成表格生成表格生成表格生成表格生成表格生成表格生成表格生成表格生成表格", f2);
Paragraph p21 = new Paragraph("生成表格1生成表格1生成表格1", f2);
Paragraph p22 = new Paragraph("生成表格2生成表格2生成表格2生成表格2生成表格2", f2);
p2.setFirstLineIndent(40);// 首行缩进
p2.setAlignment(Paragraph.ALIGN_JUSTIFIED);// 对齐方式
p21.setIndentationLeft(60);// 左边距,右边距
p22.setIndentationRight(60);
document.add(p2);
document.add(p21);
document.add(p22);

Table table = new Table(4,4);
table.setBorderWidth(1);
table.setBorderColor(Color.BLACK);//如果要设置成其他颜色,会被单元格边框的默认值覆盖
table.setPadding(0);
table.setSpacing(0);//大于0时,单元格的边框和table边框分离

//添加表头元素
Cell cell = new Cell("表头");//单元格
cell.setHeader(true);
cell.setColspan(4);
table.addCell(cell);
table.endHeaders();// 表头结束

// 表格的主体
cell = new Cell("Example cell 2");
cell.setRowspan(2);//当前单元格占两行,纵向跨度
table.addCell(cell);
table.addCell(new Paragraph("用java生成的表格1"));
table.addCell(new Paragraph("用java生成的表格2"));
table.addCell(new Paragraph("用java生成的表格3"));
table.addCell(new Paragraph("用java生成的表格4"));

Table table1 = new Table(4,4);
table1.setBorderWidth(1);
table1.setBorderColor(Color.BLUE);
table1.setPadding(0);
table1.setSpacing(1);
table1.addCell(cell);
table1.addCell(cell);
table1.addCell(new Paragraph("用java生成的表格1"));
table1.addCell(new Paragraph("用java生成的表格2"));
table1.addCell(new Paragraph("用java生成的表格3"));
table1.addCell(new Paragraph("用java生成的表格4"));
document.add(table);
document.add(table1);

Image images=Image.getInstance("http://img4.duitang.com/uploads/item/201309/20/20130920211733_naFKL.thumb.700_0.jpeg");
images.scaleAbsolute(200,200);
images.setAlignment(Image.MIDDLE);
document.add(images);
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private static void Table(int i, int j) {
// TODO Auto-generated method stub

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: