在实际的项目开发中我们需要将后台大量数据导出为word或者是excel方便用户操作,当然能完成这一功能的有freemarker,itext,poi等技术,本文讲述以itext导出word。
首先我们需要明白的是无论是freemarker,itext,poi都是先做好模板或者是先画出模板然后在其中填充内容,那么有了这种思想,就能做好这一功能。
开发必须的三个架包:
下面我们直接上代码:
package com.csg.action;
import java.awt.Color;
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.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.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.style.RtfFont;
public class WordTest2 {
public void createDocContext(String file) throws DocumentException,
IOException {
// 设置纸张大小
Document document = new Document(PageSize.A4);
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
RtfWriter2.getInstance(document, new FileOutputStream(file));
document.open();
// 设置中文字体
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 标题字体风格
RtfFont titleFont = new RtfFont("宋体", 21, Font.BOLD, Color.BLACK);
/* Font titleFont = new Font(bfChinese, 21, Font.BOLD); */
// 正文字体风格
Font contextFont = new Font(bfChinese, 12, Font.NORMAL);
Paragraph title = new Paragraph("xx画院艺术品收藏审核表");
// 设置标题格式对齐方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
document.add(title);
// 设置 Table 表格
Table aTable = new Table(2);// 设置表格为2列
int width[] = { 4, 96 };// 每列的占比例
aTable.setWidths(width);// 设置每列所占比例
aTable.setWidth(100); // 占页面宽度 100%
aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示
aTable.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示
aTable.setAutoFillEmptyCells(true); // 自动填满
aTable.setBorderWidth(1); // 边框宽度
aTable.setBorderColor(Color.BLACK); // 边框颜色
aTable.setPadding(0);// 衬距,看效果就知道什么意思了
aTable.setSpacing(0);// 即单元格之间的间距
aTable.setBorder(1);// 边框
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.black);
Cell cell = new Cell(new Phrase("艺术品情况说明", fontChinese));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorderColor(Color.BLACK);
aTable.addCell(cell);
cell = new Cell(new Phrase("收藏xxx版画作品14张合共6.5万元,从本人手中直接购买。",
fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
aTable.addCell(cell);
cell = new Cell("#5");
aTable.addCell(cell);
cell = new Cell(new Phrase("填表人:_________ 日期:___年___月___日" + " ",
fontChinese));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
aTable.addCell(cell);
document.add(aTable);
// 设置Table表格,创建一个10列的表格,后台循环数据
aTable = new Table(10);
aTable.setWidths(new int[] { 4, 10, 20, 10, 20, 10, 20, 10, 20, 6 });
aTable.setWidth(100);
aTable.addCell(new Cell("#1"));
aTable.addCell(new Cell("序号"));
aTable.addCell(new Cell("作品名称"));
aTable.addCell(new Cell("作者"));
aTable.addCell(new Cell("年代"));
aTable.addCell(new Cell("品名"));
aTable.addCell(new Cell("尺寸"));
aTable.addCell(new Cell("件数"));
aTable.addCell(new Cell("收藏金额(不含税)"));
aTable.addCell(new Cell("税收"));
for (int i = 0; i < 10; i++) {
aTable.addCell(new Cell(""));
aTable.addCell(new Cell("第" + (i + 1) + "件"));
aTable.addCell(new Cell("1234"));
aTable.addCell(new Cell("1234"));
aTable.addCell(new Cell("123"));
aTable.addCell(new Cell("123"));
aTable.addCell(new Cell("123"));
aTable.addCell(new Cell("123"));
aTable.addCell(new Cell("123"));
aTable.addCell(new Cell("123"));
}
;
document.add(aTable);
aTable = new Table(2);// 设置表格为2列
aTable.setWidths(new int[] { 5, 95 });// 设置每列所占比例
aTable.setWidth(100); // 占页面宽度 100%
cell = new Cell(new Phrase("征集小组组员执行征集情况说明", fontChinese));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorderColor(Color.BLACK);
aTable.addCell(cell);
cell = new Cell(
new Phrase(
"志强、学灵经手向xxx征集版画作品14张,合共6.5万元,具体明细如上。经综合分析,这14件作品建议列入“藏品”收藏,发收藏证书。当否,请审核",
fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
aTable.addCell(cell);
cell = new Cell(new Phrase("征集小组组员签名", fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(8);// 合并行
aTable.addCell(cell);
cell = new Cell(new Phrase("", fontChinese));
cell.setRowspan(8);
aTable.addCell(cell);
cell = new Cell(new Phrase("征集小组副组长审核", fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(9);
aTable.addCell(cell);
cell = new Cell(new Phrase("", fontChinese));
cell.setRowspan(9);
aTable.addCell(cell);
cell = new Cell(new Phrase("征集小组组长审核", fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(8);
aTable.addCell(cell);
cell = new Cell(new Phrase("", fontChinese));
cell.setRowspan(8);
aTable.addCell(cell);
cell = new Cell(new Phrase("艺术委员会意见", fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(8);
aTable.addCell(cell);
cell = new Cell(new Phrase("", fontChinese));
cell.setRowspan(8);
aTable.addCell(cell);
document.add(aTable);
// 设置5个表格 循环5次
for (int i = 0; i < 5; i++) {
// 打印图像
aTable = new Table(2);// 设置表格为2列3行
aTable.setWidths(new int[] { 50, 50 });// 设置每列所占比例
aTable.setWidth(100); // 占页面宽度 100%
// 添加图像1
cell = new Cell();
cell.setBorderWidth(0);// 边框线设为0
Image img = Image.getInstance("d:/201305.jpg");
img.setAbsolutePosition(0, 0);
img.scaleAbsolute(12, 35);// 直接设定显示尺寸
img.scalePercent(50);// 表示显示的大小为原尺寸的50%
img.scalePercent(25, 50);// 图像高宽的显示比例
img.setRotation(30);// 图像旋转一定角度
cell.add(img);
aTable.addCell(cell);
// 添加图像2
cell = new Cell();
cell.setBorderWidth(0);
cell.setVerticalAlignment(Element.ALIGN_RIGHT);
img.setAlignment(Image.RIGHT);// 设置图片显示位置
img = Image.getInstance("d:/201307.jpg");
/*
* img.scaleAbsolute(12,50);//直接设定显示尺寸
*/img.scalePercent(50);// 表示显示的大小为原尺寸的50%
img.scalePercent(25, 50);// 图像高宽的显示比例
img.setRotation(30);// 图像旋转一定角度
cell.add(img);
aTable.addCell(cell);
// 信息
cell = new Cell(new Phrase("作品1:《文明符号迴响》", fontChinese));
cell.setBorderWidth(0);
aTable.addCell(cell);
cell = new Cell(new Phrase("作品2:《石语之一》", fontChinese));
cell.setBorderWidth(0);
aTable.addCell(cell);
cell = new Cell(
new Phrase("落款:23/25 文明符号迴响 xxx 2009", fontChinese));
cell.setBorderWidth(0);
aTable.addCell(cell);
cell = new Cell(new Phrase("落款:7/15石语之一 石版画 xxx 2011", fontChinese));
cell.setBorderWidth(0);
aTable.addCell(cell);
document.add(aTable);
}
document.add(new Paragraph("\n" + "\n"));
title = new Paragraph("作者简介");
// 设置标题格式对齐方式
title.setAlignment(Element.ALIGN_CENTER);
titleFont = new RtfFont("宋体", 16, Font.BOLD, Color.BLACK);
title.setFont(titleFont);
title.setSpacingAfter(10);
document.add(title);
// 正文
String contextString = "xxx1969-年生于北京南靖,籍贯广东潮州。1996年毕业于广州美术学院版画系并留校工作, 现为广州美术学院版画系副教授,山西大学客座教授;中国美术家协会会员,中国美术家协会藏书票研究会常务副主席,广东青年画院画家。"
+ "多件作品入选全国美展、全国版画展、国际版画展等学术展。曾获第九届全国美术作品展优秀奖、当代中国青年作品展二等奖、×××庆祝澳门回归祖国宣传招贴画十佳作品奖、全军廉政文化优秀作品展二等奖、广东省美术作品展铜奖、广东版画奖等学术奖项。"
+ " \n"// 换行
+ "作品、论文发表于《美术》、《美术观察》、《中国版画》、《美术学报》、《北方美术》等学术刊物。出版教材《石版画》、《版画》(合著)等 ;出版作品集《中锐xxx作品集》;入编《中国美术大事记》、《当代中国艺术》、《中国当代中青年版画家石版画精品集》等。"
+ " \n"// 换行
+ "作品被广东美术馆、神州版画博物馆、中华世纪坛、绍兴鲁迅纪念馆、观澜美术馆、汕头博物馆、广州美术学院、伊斯坦布尔国际版画馆等学术机构及个人收藏。";
Paragraph context = new Paragraph(contextString);
// 正文格式左对齐
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
// 离上一段落(标题)空的行数
context.setSpacingBefore(10);
// 设置第一行空的列数
context.setFirstLineIndent(20);
document.add(context);
document.close();
}
public static void main(String[] args) {
WordTest2 b = new WordTest2();
try {
b.createDocContext("d:/demo.doc");
System.out.println("导出成功");
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
本文示例图片请直接拷贝到 D:盘!
本文以测试为主,实战本代码只做参考。匆忙之作,欢迎指正!
×××地址:http://down.51cto.com/data/1071741
转载于:https://blog.51cto.com/1936625305/1352558