Java操作Word报告
来源:互联网 发布:向日葵软件如何视频 编辑:程序博客网 时间:2024/06/06 03:54
一.需要用到两个jar包,这只是针对word,针对pdf的还需要另加jar包。
itext.jar核心包
下载地址:
http://cn.jarfire.org/itext.htmliTextAsian.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}}
0 0
- Java操作Word报告
- Java操作Excel,Word
- java 操作 word
- java 操作 word
- java中操作word
- java操作word文档
- Java操作Excel,Word
- java操作word
- java 操作word
- JAVA操作WORD文档
- Java操作word
- Java操作word
- 关于java操作word
- java操作word文档
- 关于java操作word
- Java 之Word操作
- java操作word
- java操作word示例
- db2 SQL1336N 找不到远程主机 SQLSTATE=08001,编目 TCP/IP 节点时指定的主机名值不正确。
- 将Nginx添加到系统服务(使其可使用service命令控制)
- 导入样式表与链入样式表区别
- 手机兼容【手机显示与电脑(或指定)的显示不一致】
- 以下为官方对这款免费的VPN的定义
- Java操作Word报告
- Android 定制关机界面
- 越看对方不顺眼,越要找对方优点好好相处
- 努力不一定收获,不努力一定没收获
- 所有的伟大 都源于一个勇敢的开始
- viewFlipper的学习使用,可做引导页
- 构建C1000K的服务器(2) – 实现百万连接的comet服务器
- 只有 DBA 才能导入由其他 DBA 导出的文件
- 安卓开发学习资源:控件+图标