使用spire.doc for java 操作文档(添加段落,动态生成表格,给段落设置字体样式等)

31、使用spire.doc for java 操作文档记录

// 创建段落 
public static void createText(Section section,List<ApplyObject> wordList){
        //      创建一个段落,添加一些信息 一、查询需求:
        Paragraph paragraph = section.addParagraph();
        TextRange tr = paragraph.appendText("专线一键搜查询内容");
        tr.getCharacterFormat().setFontSize(20);
        String name = "";
        String sfzhm = "";
        String queryContent = "";
        for (ApplyObject applyObject : wordList){
            name+=applyObject.getSelName()+",";
            sfzhm+=applyObject.getIdNumber()+",";
            queryContent = applyObject.getSelTableStr();
        }
        String nameNew = name.substring(0,name.length()-1);
        String sfzhmNew = sfzhm.substring(0,sfzhm.length()-1);
//        创建一个段落添加查询需求里面的具体信息
        Paragraph paragraph3 = section.addParagraph();
        TextRange tr3 = paragraph3.appendText("外网专线查询对象为:"+nameNew+"("+sfzhmNew+")"+","+"查询数据项包括:"+queryContent);

        //      创建一个段落,添加一些信息 一、查询内容:
        Paragraph paragraph2 = section.addParagraph();
        TextRange tr2 = paragraph2.appendText("专线一键搜查询结果");
        tr2.getCharacterFormat().setFontSize(20);
    }


// 给设置的段落添加样式
// 给全文中的段落添加样式,如果你只有一个section的话,只需要执行里面的一段话就可以了
找到段落,给每一个段落添加样式
        ParagraphStyle style1 = new ParagraphStyle(document);
        style1.setName("style");
        style1.getCharacterFormat().setFontName("仿宋_GB2312");
        document.getStyles().add(style1);
       for(int i = 0; i < doc.getSections().getCount(); i++) {
            Section section = doc.getSections().get(i);
            for (int j = 0; j < section.getParagraphs().getCount(); j++) {
                Paragraph paragraph = section.getParagraphs().get(j);
                paragraph.applyStyle(style1.getName());
            }
        }

// 动态创建表格
   public static void createDoc(Section section, List<String> header, List<List<Object>> data, String title, List<ApplyObject> wordList) {
        //添加表格
        Table table = section.addTable(true);
        //设置表格的行数和列数
        table.resetCells(data.size() + 2, header.size());
        //设置第一行作为表格的表头并添加数据
        TableRow row = table.getRows().get(1);
        row.isHeader(true);
        row.setHeight(40);
        row.setHeightType(TableRowHeightType.Exactly);
        TableRow row3 = table.getRows().get(0);
        row3.isHeader(true);
        row3.setHeight(60);
        row3.setHeightType(TableRowHeightType.Exactly);
        row3.getCells().get(0).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
        table.applyHorizontalMerge(0, 0, header.size() - 1);
        Paragraph p1 = row3.getCells().get(0).addParagraph();
        p1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        TextRange range3 = p1.appendText(title);
        range3.getCharacterFormat().setFontName("仿宋_GB2312");
        range3.getCharacterFormat().setFontSize(12f);
        range3.getCharacterFormat().setTextColor(Color.black);
        range3.getCharacterFormat().setBold(true);

        for (int i = 0; i < header.size(); i++) {
            row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
            Paragraph p = row.getCells().get(i).addParagraph();
            p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            TextRange range1 = p.appendText(header.get(i));
            range1.getCharacterFormat().setTextColor(Color.black);
            range1.getCharacterFormat().setFontName("仿宋_GB2312");
            range1.getCharacterFormat().setFontSize(12f);
            range1.getCharacterFormat().setBold(true);
        }

        //添加数据到剩余行
        try{
            for (int r = 0; r < data.size(); r++) {
                TableRow dataRow = table.getRows().get(r + 2);
                dataRow.setHeight(25);
                dataRow.setHeightType(TableRowHeightType.Exactly);
                dataRow.getRowFormat().setBackColor(Color.white);
                for (int c = 0; c < data.get(r).size(); c++) {
                              dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                    String s = "";
                    if (data.get(r).get(c) instanceof Timestamp) {
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式,不显示毫秒
                        s = df.format((Timestamp) (data.get(r).get(c)));
                    } else if (data.get(r).get(c) instanceof Date) {
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式,不显示毫秒
                        s = df.format((Date) (data.get(r).get(c)));
                    } else if (data.get(r).get(c) == null) {
                        s = "";
                    } else {
                        s = data.get(r).get(c).toString();
                    }

                    TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(s);


                    range2.getCharacterFormat().setFontName("仿宋_GB2312");
                    range2.getCharacterFormat().setFontSize(10f);
                }
            }
        }catch (Exception e){
            e.getMessage();
            log.info("插入数据有异常");
        }
        section.addParagraph();
    }
    //生成页眉和页脚
  public static void insertHeaderAndFooter(Section section) {

        //分别获取section的页眉页脚
        HeaderFooter header = section.getHeadersFooters().getHeader();
        HeaderFooter footer = section.getHeadersFooters().getFooter();

        //添加段落到页眉
        Paragraph headerParagraph = header.addParagraph();

        //添加文字到页眉的段落
        TextRange text = headerParagraph.appendText("页眉测试");
        text.getCharacterFormat().setFontName("仿宋_GB2312");
        text.getCharacterFormat().setFontSize(10);
        text.getCharacterFormat().setItalic(true);
        headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);

        //设置页眉段落的底部边线样式
        headerParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
        headerParagraph.getFormat().getBorders().getBottom().setLineWidth(1f);

        //添加段落到页脚
        Paragraph footerParagraph = footer.addParagraph();

        //添加Field_Page和Field_Num_Pages域到页脚段落,用于显示当前页码和总页数
        footerParagraph.appendField("page number", FieldType.Field_Page);
        footerParagraph.appendText("/");
        footerParagraph.appendField("number of pages", FieldType.Field_Num_Pages);
        footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);

        //设置页脚段落的顶部边线样式
        footerParagraph.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
        footerParagraph.getFormat().getBorders().getTop().setLineWidth(1f);
    }

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值