zoukankan      html  css  js  c++  java
  • itext库产生word文档示例(.doc)

    itext库生产word文档示例

     1   // 设置中文字体------------------------------------------------
    2 BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
    3 "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    4 // 标题字体风格
    5 Font titleFont = new Font(bfChinese, 12, Font.BOLD);
    6 // 正文字体风格
    7 Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
    8
    9 // 新建标题,设置格式对齐方式------------------------
    10 Paragraph title = new Paragraph("标题");
    11 title.setAlignment(Element.ALIGN_CENTER);
    12 title.setFont(titleFont);
    13 document.add(title);
    14
    15 // 创建新段落,设置格式---------------------------
    16 Paragraph context = new Paragraph("测试:测试:测试:测试:测试:测试:");
    17 context.setAlignment(Element.ALIGN_LEFT);
    18 context.setFont(contextFont);
    19 context.setSpacingBefore(3);// 段间距
    20 context.setFirstLineIndent(20);// 设置第一行空的列数
    21 document.add(context);
    22
    23 // 创建一个三列的表格------------------------------
    24 Table table = new Table(3);
    25 int width[] = { 25, 25, 50 };// 设置每列宽度比例
    26 table.setWidths(width);
    27 table.setWidth(90);// 占页面宽度比例
    28 table.setAlignment(Element.ALIGN_CENTER);// 居中
    29 table.setAlignment(Element.ALIGN_MIDDLE);// 垂直居中
    30 table.setAutoFillEmptyCells(true);// 自动填满
    31 table.setBorderWidth(1);// 边框宽度
    32
    33 Cell haderCell = new Cell("表格表头");// 设置表头
    34 haderCell.setHeader(true);
    35 haderCell.setColspan(3);
    36 table.addCell(haderCell);
    37 table.endHeaders();
    38 Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);
    39 Cell cell = new Cell(new Paragraph("这是一个3*3测试表格数据", fontChinese));
    40 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    41 table.addCell(cell);
    42 table.addCell(new Cell("#1"));
    43 table.addCell(new Cell("#2"));
    44 table.addCell(new Cell("#3"));
    45
    46 document.add(table);
    47 document.close();
    48
    49 }


    (一)生产文件直接存储到本地

    1 RtfWriter2.getInstance(document, new FileOutputStream("D://fileName.doc"));

    (二)生产文件 存到数据库中

    ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
    RtfWriter2.getInstance(document, outputStream);

    文件信息形式转化:ByteArrayOutputStream->InputStream->Blob

    如:ByteArrayOutputStream outStream=new ByteArrayOutputStream();
    InputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
    Blob fileObject = Hibernate.createBlob(inStream);
    详见博文" 文档上传(通过网页上传到服务器)..."

    (三)生产文件 供下载

    OutputStream outputStream;
    RtfWriter2.getInstance(document, outputStream);
    详见博文" 文件下载(从数据库下载到本地)…"
  • 相关阅读:
    python 读取excel表格中的数据
    python 安装 pip 报错解决方案
    HDU-1150-MachineSchedule(二分图匹配)
    POJ-3177-RedundantPaths(边联通分量,缩点)
    POJ-3352-RoadConstruction(边双联通分量,缩点)
    F-三生三世
    D-温暖的签到题
    C-晾衣服
    A-坐飞机
    POJ-2777-CountColor(线段树,位运算)
  • 原文地址:https://www.cnblogs.com/windlaughing/p/2415623.html
Copyright © 2011-2022 走看看