itext导出word

该博客详细介绍了如何利用Java的iText库创建并导出Word文档,包括设置纸张大小、定义字体样式、创建表格、添加页眉和页脚等步骤。通过实例代码展示了如何生成包含标题和数据的表格,为开发者提供了一个实用的参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;


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.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;


public class ExportWordTest {
public void createDocContext()throws DocumentException, IOException{  
        //设置纸张大小  
        Document document = new Document(PageSize.A4.rotate());  
        //建立一个书写器,与document对象关联  
        RtfWriter2.getInstance(document, new FileOutputStream("test.doc"));  
        
        document.open(); 
        
        //设置中文字体  
        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);  
        //标题字体风格  
        Font titleFont = new Font(bfChinese,12,Font.BOLD);  
        //正文字体风格  
        Font contextFont = new Font(bfChinese,8,Font.NORMAL);  
        
        // 添加页眉   
        HeaderFooter header = new HeaderFooter(new Phrase("header"), false);   
        header.setAlignment(Rectangle.ALIGN_CENTER);   
        document.setHeader(header);  
        
        //设置Table表格,创建一个25列的表格  
        Table table = new Table(33);  
        table.setWidth(100);//占页面宽度比例  
        table.setAlignment(Element.ALIGN_CENTER);//居中  
        table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中  
        table.setAutoFillEmptyCells(true);//自动填满  
        table.setBorderWidth(1);//边框宽度  
        table.setCellsFitPage(true);
        
        //设置表数据
        //标题
        Paragraph date = new Paragraph("登记表",contextFont);
    //设置对齐方式  
    date.setAlignment(Element.ALIGN_CENTER);
    Cell cell = new Cell(date);  
        cell.setColspan(33);
        cell.setBorderWidth(0);
        table.addCell(cell);
        //日期
        Date d = new Date();
        date = new Paragraph(d.getDate()+":"+d.getDate(),contextFont);
    //设置对齐方式  
    date.setAlignment(Element.ALIGN_LEFT);
    cell = new Cell(date);  
        cell.setColspan(33);
        cell.setBorderWidth(0);
        table.addCell(cell); 
        for(int i=0;i<1;i++){
        date = new Paragraph("姓名姓名",contextFont);
        //设置对齐方式  
        date.setAlignment(Element.ALIGN_CENTER);
        cell = new Cell(date);  
            cell.setColspan(2);
            table.addCell(cell); 
        for(int j=0;j<31;j++){
        date = new Paragraph(""+(j+1),contextFont);
            //设置对齐方式  
        date.setAlignment(Element.ALIGN_CENTER);
            cell = new Cell(date);  
    table.addCell(cell); 
            }
        }
        table.endHeaders();
        //设置表数据
        for(int i=0;i<100;i++){
        date = new Paragraph("wy"+i,contextFont);
        //设置对齐方式  
        date.setAlignment(Element.ALIGN_CENTER);
        cell = new Cell(date);  
            cell.setColspan(2);
            table.addCell(cell); 
        for(int j=0;j<31;j++){
        date = new Paragraph("data"+(j+1),contextFont);
            //设置对齐方式  
        date.setAlignment(Element.ALIGN_CENTER);
            cell = new Cell(date);  
    table.addCell(cell); 
            }
        }

        document.add(table);
        
        // 添加页脚   
        HeaderFooter footer = new HeaderFooter(new Phrase("页脚",contextFont), true);   
        footer.setAlignment(Rectangle.ALIGN_LEFT);   
        
        document.setFooter(footer);
        
        document.close();  
    }  

    public static void main(String[] args) {  
    ExportWordTest word = new ExportWordTest();  
        try {  
            word.createDocContext();  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值