java生成doc文件代码

package com.huawei;

import java.awt.Color;
import java.io.File;
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.FontFactory;
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.headerfooter.RtfHeaderFooter;
import com.lowagie.text.rtf.headerfooter.RtfHeaderFooterGroup;

//用iText生成word文件
public class CreateDoc
{
    public void createDocFile(String file) throws DocumentException,
            IOException
    {
        // 设置纸张大小
        Document document = new Document(PageSize.A4);
        // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
        RtfWriter2.getInstance(document, new FileOutputStream(file));
        document.open();
        // 设置中文字体
        BaseFont macintosh = BaseFont.createFont(
            "C:\\WINDOWS\\Fonts\\SIMHEI.TTF", BaseFont.IDENTITY_H,
            BaseFont.NOT_EMBEDDED);
        // 标题字体风格
        Font ThemeFont = new Font(macintosh, 18, Font.BOLD);
        // 设置标题字体的颜色
        ThemeFont.setColor(Color.RED);
        // 正文字体风格
        Font bodyFont = new Font(macintosh, 14, Font.NORMAL);
        // 设置正文字体的颜色
        bodyFont.setColor(100, 200,150);
        // 创建主题的Phrases对象
        Paragraph theme = new Paragraph("常见问题");
        // 将设置好的字体添加到主题的短句上
        theme.setAlignment(Element.ALIGN_CENTER);
        theme.setFont(ThemeFont);
        // 将Phrases添加到document文档中
        document.add(theme);
        // 设置word正文中的内容
        String bodyText = "报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容报告正文内容";
        // 创建正文的Phrases对象
        Paragraph context = new Paragraph(bodyText);
        // 正文格式左对齐
        context.setAlignment(Element.ALIGN_LEFT);
        // 设置正文字体的颜色
        context.setFont(bodyFont);
        // 离上一段落(标题)空的行数
        context.setSpacingBefore(3);
        // 设置第一行空的列数
        context.setFirstLineIndent(20);
        // // 将Phrases添加到document文档中
        document.add(context);
        // 利用类FontFactory结合Font和Color可以设置各种各样字体样式
        Paragraph line = new Paragraph("下划线的实现", FontFactory.getFont(
            FontFactory.HELVETICA_BOLDOBLIQUE, 18, Font.UNDERLINE, new Color(255,
                0, 255)));
        document.add(line);

        // 创建Table表格
        Table table = new Table(5);// 创建该表格的列数,在本程序中设为5列
        int width[] = { 20, 20, 20, 20, 20 };// 每列的单元格的宽度
        table.setWidths(width);// 设置每列所占比例
        table.setWidth(100); // 占页面宽度 100%,相当于html中width属性
        table.setAlignment(Element.ALIGN_CENTER);// 设置该表格中的元素水平方向居中显示
        table.setAlignment(Element.ALIGN_MIDDLE);// 设置该表格中的元素垂直方向纵向居中显示
        table.setAutoFillEmptyCells(true); // 自动填满
        table.setBorderWidth(1); // 边框宽度
        table.setBorderColor(new Color(160, 32, 240)); // 边框颜色
        table.setPadding(2);// 单元格内部的空白距离,相当于html中的cellpadding属性
        table.setSpacing(3);// 单元格之间的间距,相当于html中的cellspacing
        table.setBorder(2);// 边框的宽度
        // 设置表头
        Cell haderCell = new Cell("用iText创建的表格-表头");// 创建单元格
        haderCell.setBackgroundColor(Color.blue);// 设置此单元格的背景色
        haderCell.setHeader(true);// 设置为表头
        haderCell.setColspan(5);// 合并列的列数
        haderCell.setHorizontalAlignment(haderCell.ALIGN_CENTER);// 水平显示的位置
        table.addCell(haderCell);// 将单元格添加到表格中
        table.endHeaders();// 结束表头的设置
        Font fontChinese = new Font(macintosh, 15, Font.NORMAL, Color.red);// 设置字体风格
        Cell cell = new Cell(new Phrase("这是一个3行1列合并的表格", fontChinese));// 创建单元格
        cell.setVerticalAlignment(Element.ALIGN_TOP);
        cell.setBorderColor(new Color(255, 215, 0));
        cell.setRowspan(3);// 设置合并的行数
        // 添加单元格
        table.addCell(cell);
        table.addCell(new Cell("第一行第一列"));
        table.addCell(new Cell("第一行第二列"));
        table.addCell(new Cell("第一行第三列"));
        table.addCell(new Cell("第一行第四列"));
        table.addCell(new Cell("第二行第一列"));
        table.addCell(new Cell("第二行第二列"));
        table.addCell(new Cell("第二行第三列"));
        table.addCell(new Cell("第二行第四列"));
        table.addCell(new Cell("第三行第一列"));
        table.addCell(new Cell("第三行第二列"));
        table.addCell(new Cell("第三行第三列"));
        table.addCell(new Cell("第三行第四列"));
        // 创建一个合并5列的单元格
        Cell cell3 = new Cell(new Phrase("一行5列合并的表格", fontChinese));
        cell3.setColspan(5);
        cell3.setVerticalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell3);
        document.add(table);
        // 添加图片
        Image img = Image.getInstance("E:\\Blue hills.jpg");
        // img.setAbsolutePosition(0, 0);//设置图片的绝对位置
        img.setAlignment(Image.MIDDLE);// 设置图片显示位置
        //img.scaleAbsolute(20, 20);// 直接设定显示尺寸
        img.scalePercent(100);// 表示显示的大小为原尺寸的20%
        // img.scalePercent(60, 60);// 图像宽高的显示比例
        // img.setRotation(30);// 图像旋转一定角度
        document.add(img);
        document.close();

    }

    public static void main(String[] args)
    {
        CreateDoc word = new CreateDoc();
        String file = "E://华为.doc";
        try
        {
            File file1 = new File("E://华为.doc");
            if (!file1.exists())
            {
                word.createDocFile(file);
                System.out.println("生成文件'华为.doc'成功");
            }
            else
            {
                file1.delete();
                word.createDocFile(file);
                System.out.println("生成文件'华为.doc'成功");
            }

        }
        catch (DocumentException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值