IText导出word之页眉页脚页码
本文主要通过Itext jar包对已有数据进行封装并导出rtf文档。rtf也可以用office打开。
代码中包含了设置页眉页脚、添加table等
详细见源码:内含注释说明
[java]
/**
* WordServer.java
* 版权所有(C) 2012
* 创建:cuiran 2012-11-07 13:20:40
*/ www.zzzyk.com
package com.cayden.web.base;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.cayden.web.action.qybj.QueryProducesAction;
import com.cayden.web.domain.qybj.QybjCompany;
import com.cayden.web.domain.qybj.QybjProduce;
import com.cayden.web.domain.qybj.QybjProduceDevice;
import com.cayden.web.domain.qybj.QybjProduceParam;
import com.cayden.web.dto.qybj.QybjProduceDto;
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.HeaderFooter;
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.field.RtfPageNumber;
import com.lowagie.text.rtf.field.RtfTotalPageNumber;
import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter;
/**
* IText
* 数据导出word类
* @author cuiran
* @version TODO
*/
public class WordServer {
private static Log log = LogFactory.getLog(WordServer.class.getName());
/**
*
* 创建word文档
* @param filename 文件名称
* @param dto 数据封装对象
* @throws DocumentException
* @throws IOException
*/
public String createRTFContext(String filename,QybjProduceDto dto) throws DocumentException,IOException {
String path="";
String imagepath="";
try{
/**
* 先获取对象
*/
QybjProduce qybjProduce=dto.getQybjProduce();
List<QybjProduceDevice> devices=dto.getPdevices();
List<QybjProduceParam> params=dto.getPparams();
QybjCompany qybjCompany=dto.getQybjCompany();
/**
* 获取导出文件路径
*/
URL url=WordServer.class.getClassLoader().getResource("") ;
path=url.getPath().replace("WEB-INF/classes/", "file/"+filename + ".rtf");
imagepath=url.getPath().replace("WEB-INF/classes/", qybjCompany.getImagepath());
Document document = new Document(PageSize.A4);
RtfWriter2 rtfWriter2=RtfWriter2.getInstance(document, new FileOutputStream(path));
document.open();
// 设置中文字体
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 标题字体风格
Font titleFont = new Font(bfChinese, 12, Font.BOLD);
Font titleBaoFont = new Font(bfChinese, 20, Font.BOLD);
Font underLineFont = new Font(bfChinese, 12, Font.UNDERLINE);
// 正文字体风格
Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
// 正文标题1 风格
Font contextTitle1Font = new Font(bfChinese, 12, Font.NORMAL);
//设置加粗字体
Font boldFont = new Font(bfChinese, 10, Font.BOLD);
补充:软件开发 , Java ,