IText生成doc文档需要三个包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jar

亲测无误,代码如下所示:

import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.rtf.RtfWriter2; import java.awt.*;
import java.io.FileOutputStream; /**
* 生成报修系统的报表doc文档
* User: HYY
* Date: 13-8-1
* Time: 下午9:54
* To change this template use File | Settings | File Templates.
*/
public class GenerateBaoxiu {
public static void main(String[] args) throws Exception {
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //设置纸张的大小对象
Rectangle rectangle = new Rectangle(PageSize.A4);
// 创建word文档,并旋转,使其横向
Document document = new Document(rectangle.rotate());
RtfWriter2.getInstance(document, new FileOutputStream("C:/無幽之路IText教程.doc"));
document.open();
//报表标题
Paragraph headTitle = new Paragraph("学期报修报表");
Font headFont = new Font(bfChinese);
headFont.setSize(22);
headFont.setStyle(Font.BOLD);
headTitle.setFont(headFont);
headTitle.setAlignment(1);//居中
document.add(headTitle); //正文表格
PdfPTable table = new PdfPTable(10);
table.setWidths(new float[]{15f,9f,9f,9f,11f,8f,20f,15f,9f,9f});
table.setTotalWidth(114); //设置表头字体样式
Font tableHeadFont = new Font(bfChinese);//表头字体样式
tableHeadFont.setSize(10.5f);//设置表头字体为五号字体
tableHeadFont.setStyle(Font.BOLD);//加粗 //表头第一列:表单申请时间
PdfPCell headCell1 = new PdfPCell();
Paragraph headCell1Phrase = new Paragraph("表单申请时间");
headCell1Phrase.setFont(tableHeadFont);
headCell1.setPhrase(headCell1Phrase);
headCell1Phrase.setAlignment(1);
headCell1.setHorizontalAlignment(1); //表头第二列:申请人
PdfPCell headCell2 = new PdfPCell();
Paragraph headCell2Phrase = new Paragraph("申请人");
headCell2Phrase.setFont(tableHeadFont);
headCell2.setPhrase(headCell2Phrase);
headCell2Phrase.setAlignment(1);
headCell2.setHorizontalAlignment(1); //表头第三列:维修人
PdfPCell headCell3 = new PdfPCell();
Paragraph headCell3Phrase = new Paragraph("维修人");
headCell3Phrase.setFont(tableHeadFont);
headCell3.setPhrase(headCell3Phrase);
headCell3Phrase.setAlignment(1);
headCell3.setHorizontalAlignment(1); //表头第四列:后勤人
PdfPCell headCell4 = new PdfPCell();
Paragraph headCell4Phrase = new Paragraph("后勤人");
headCell4Phrase.setFont(tableHeadFont);
headCell4.setPhrase(headCell4Phrase);
headCell4Phrase.setAlignment(1);
headCell4.setHorizontalAlignment(1); //表头第五列:设备类型
PdfPCell headCell5 = new PdfPCell();
Paragraph headCell5Phrase = new Paragraph("设备类型");
headCell5Phrase.setFont(tableHeadFont);
headCell5.setPhrase(headCell5Phrase);
headCell5Phrase.setAlignment(1);
headCell5.setHorizontalAlignment(1); //表头第六列:维修设备
PdfPCell headCell6 = new PdfPCell();
Paragraph headCell6Phrase = new Paragraph("维修设备");
headCell6Phrase.setFont(tableHeadFont);
headCell6.setPhrase(headCell6Phrase);
headCell6Phrase.setAlignment(1);
headCell6.setHorizontalAlignment(1); //表头第七列:维修地点
PdfPCell headCell7 = new PdfPCell();
Paragraph headCell7Phrase = new Paragraph("维修地点");
headCell7Phrase.setFont(tableHeadFont);
headCell7.setPhrase(headCell7Phrase);
headCell7Phrase.setAlignment(1);
headCell7.setHorizontalAlignment(1); //表头第八列:维修完成时间
PdfPCell headCell8 = new PdfPCell();
Paragraph headCell8Phrase = new Paragraph("维修完成时间");
headCell8Phrase.setFont(tableHeadFont);
headCell8.setPhrase(headCell8Phrase);
headCell8Phrase.setAlignment(1);
headCell8.setHorizontalAlignment(1); //表头第九列:维修评价
PdfPCell headCell9 = new PdfPCell();
Paragraph headCell9Phrase = new Paragraph("维修评价");
headCell9Phrase.setFont(tableHeadFont);
headCell9.setPhrase(headCell9Phrase);
headCell9Phrase.setAlignment(1);
headCell9.setHorizontalAlignment(1); //表头第十列:维修费用
PdfPCell headCell10 = new PdfPCell();
Paragraph headCell10Phrase = new Paragraph("维修费用");
headCell10Phrase.setFont(tableHeadFont);
headCell10.setPhrase(headCell10Phrase);
headCell10Phrase.setAlignment(1);
headCell10.setHorizontalAlignment(1); table.addCell(headCell1);
table.addCell(headCell2);
table.addCell(headCell3);
table.addCell(headCell4);
table.addCell(headCell5);
table.addCell(headCell6);
table.addCell(headCell7);
table.addCell(headCell8);
table.addCell(headCell9);
table.addCell(headCell10);
//表头添加完毕 //添加表格体
for(int i=0; i<1000; i++) {
table.addCell("");
}
document.add(table); //页脚段落
Paragraph paraFooter = new Paragraph();
Font font = new Font();
//页脚的字体大小
font.setSize(12f);
font.setColor(new Color(0, 0, 0));
paraFooter.setFont(font);
paraFooter.setAlignment("center");
//页脚的段落和是否有页码
HeaderFooter footer = new HeaderFooter(paraFooter, true);
//页脚的对齐方式(应该在footer设置而不是段落中设置)
footer.setAlignment(1);
document.setFooter(footer); document.close();
} }

IText 生成简单表格(报表)doc文档 单元居中的更多相关文章

  1. itext 生成doc文档 小结(自己备忘)

    1.引入maven <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</ ...

  2. IText 生成横向的doc文档

    IText生成doc文档需要三个包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jar 亲测无误,代码如下: import com.lowagie.t ...

  3. IText 中文字体解决方案 生成doc文档

    IText生成doc文档需要三个包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jar 亲测无误,代码如下: import com.lowagie.t ...

  4. 使用FastReport报表工具生成报表PDF文档

    在我们开发某个系统的时候,客户总会提出一些特定的报表需求,固定的报表格式符合他们的业务处理需要,也贴合他们的工作场景,因此我们尽可能做出符合他们实际需要的报表,这样我们的系统会得到更好的认同感.本篇随 ...

  5. Java中常用到的文件操作那些事(一)——替换doc文档模板,生成真实合同案例

    工作中,我们时常会遇到一些操作文件的操作,比如在线生成合同模板,上传/下载/解析Excel,doc文档转为pdf等操作.本文就已工作中遇到的在线生成合同为例,简要地介绍一种文档替换写法. 本文目的:给 ...

  6. doc文档生成带目录的pdf文件方法

    准备软件: 福昕PDF阅读器 下载地址:http://rj.baidu.com/soft/detail/12882.html?ald 安装福昕PDF阅读器,会自动安装pdf打印机. 准备好设置好各级标 ...

  7. poi读取word2003(.doc文档)中的表格

    poi读取word2003(.doc文档)中的表格 Jakarta POI 是apache的子项目,目标是处理ole2对象.它提供了一组操纵Windows文档的Java API.在网上见到好多通过po ...

  8. java标识符,关键字,注释及生成Doc文档

    # java语法基础 ## 标识符,关键字与注释 ### 标识符 1.类名,变量名,方法名都称为标识符. 2.命名规则:(1):所有的标识符都应该以字母(AZ,或者az)美元符($)或者下划线(_)开 ...

  9. Java eclipse生成doc文档

    这里讲解下eclipse成为doc文档,首先代码: /** * @author szy * @version 1.0 */ package com.founder.sun; class Cat{ pu ...

随机推荐

  1. insert---插入记录

    insert into table_name (column1,column2,.......) values(value1,value2,......); 例: insert into userin ...

  2. 忍者无敌-实例讲解Cocos2d-x瓦片地图

    实例比较简单,如图所示,地图上有一个忍者精灵,玩家点击他周围的上.下.左.右,他能够向这个方向行走.当他遇到障碍物后是无法穿越的,障碍物是除了草地以为部分,包括了:树.山.河流等. 忍者实例地图(TO ...

  3. Swift内存管理-示例讲解

    具体而言,Swift中的ARC内存管理是对引用类型的管理,即对类所创建的对象采用ARC管理.而对于值类型,如整型.浮点型.布尔型.字符串.元组.集合.枚举和结构体等,是由处理器自动管理的,程序员不需要 ...

  4. Qt 串口通信

    在Qt5之前,串口通信基本依赖于第三方库,下面是我曾接触过的串口通信类库: 名称 语言 平台   QextSerialPort QT C++ Win/Linux http://sourceforge. ...

  5. iOS开发基础之排序

    Objective-C 有排序的API,省了我们很多事. 主要有以下3种方法. NSComparator NSArray *unsortedArray = @[@5,@3,@8,@1,@7]; NSA ...

  6. FKCL-OS——自主的操作系统

    我想搞一个操作系统,这是因为我对windows非常不满意,对linux非常讨厌,我想要开发一个真正自己的OS,然后让自己和别人使用它.得到方便.我将在这篇文章中写下我对操作系统的不满,然后构思出我的操 ...

  7. Science论文"Clustering by fast search and find of density peaks"学习笔记

    "Clustering by fast search and find of density peaks"是今年6月份在<Science>期刊上发表的的一篇论文,论文中 ...

  8. javascript学习笔记(5

    1.string Array Date Math 内置对象的属性和方法? 答案: ①String 字符串 属性 :length  获取字符串长度 方法: indexOf()  从左到右检索子字符串在原 ...

  9. android studio引入第三方包记录

    1.添加jar文件 将jar文件复制至app module目录下的libs文件夹下,然后打开app module目录下的build.gradle配置文件,在dependencies项中添加配置命令,这 ...

  10. 运用百度开放平台接口根据ip地址获取位置

    使用百度开放平台接口根据ip地址获取位置 今天无意间发现在百度开放平台接口,就把一段代码拿了下来,有需要的可以试试看:http://opendata.baidu.com/api.php?query=5 ...