java poi导出PPT格式

本文介绍如何利用Apache POI库在Java中创建包含文本、表格、图片及超链接的PPTX文件。通过示例代码展示了创建幻灯片、设置表格样式、插入图片和超链接等操作。

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

1:maven 依赖:

采用的是4.1.2的版本导出

  <!--导出文件-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

2:代码源码

XMLSlideShow ppt = new XMLSlideShow();
        XSLFSlide slide = ppt.createSlide();//创建幻灯片
        XSLFTextBox textBox = slide.createTextBox();
        textBox.setAnchor(new Rectangle2D.Double(10,10, 200, 30));
        textBox.addNewTextParagraph().addNewTextRun().setText("创建幻灯片");

        Object[][] datas = {{"区域产品销售额","",""},{"区域", "总销售额(万元)", "总利润(万元)简单的表格"}, {"江苏省" , 9045,  2256}, {"广东省", 3000, 690}};
        XSLFTable table = slide.createTable();//创建表格
        table.setAnchor(new Rectangle2D.Double(10, 50, 0, 0));
        for(int i = 0; i < datas.length; i++) {
            XSLFTableRow tableRow = table.addRow(); //创建表格行
            for(int j = 0; j < datas[i].length; j++) {
                XSLFTableCell tableCell = tableRow.addCell();//创建表格单元格
                XSLFTextParagraph p = tableCell.addNewTextParagraph();
                XSLFTextRun tr = p.addNewTextRun();
                tr.setText(String.valueOf(datas[i][j]));

                tableCell.setFillColor(Color.getColor("0xdd7e6b"));
                p.setTextAlign(TextParagraph.TextAlign.CENTER);
                tableCell.setVerticalAlignment(VerticalAlignment.MIDDLE);

                if(i == datas.length - 1 && j == 3-1) {
                    tr.setFontSize(16d);
                    tr.setBold(true);
                    tr.setItalic(true);
                    tr.setUnderlined(true);
                    tr.setFontFamily("\u5b8b\u4f53");
                    tr.setFontColor(Color.RED);
                }
                tableCell.setBorderWidth(TableCell.BorderEdge.left,1);
                tableCell.setBorderWidth(TableCell.BorderEdge.right,1);
                tableCell.setBorderWidth(TableCell.BorderEdge.top,1);
                tableCell.setBorderWidth(TableCell.BorderEdge.bottom,1);
                tableCell.setBorderColor(TableCell.BorderEdge.bottom,Color.BLACK);
                tableCell.setBorderColor(TableCell.BorderEdge.left,Color.BLACK);
                tableCell.setBorderColor(TableCell.BorderEdge.right,Color.BLACK);
                tableCell.setBorderColor(TableCell.BorderEdge.top,Color.BLACK);
            }
            tableRow.setHeight(30);
        }

        //设置列宽
        table.setColumnWidth(0, 150);
        table.setColumnWidth(1, 150);
        table.setColumnWidth(2, 250);

        //合并单元格
        table.mergeCells(0, 0, 0, 2);

        //插入图片
        byte[] bt = FileUtils.readFileToByteArray(new File("D:\\Users\\ppt.png"));
        XSLFPictureData idx = ppt.addPicture(bt, PictureData.PictureType.PNG);
        XSLFPictureShape pic = slide.createPicture(idx);
        pic.setAnchor(new Rectangle2D.Double(10, 200, 339, 197));

        //创建一个文本链接
        XSLFTextBox linkText = slide.createTextBox();
        linkText.setAnchor(new Rectangle2D.Double(430, 310, 80, 30));
        XSLFTextRun r = linkText.addNewTextParagraph().addNewTextRun();
        r.setText("Apache POI");
        XSLFHyperlink link = r.createHyperlink();
        link.setAddress("http://poi.apache.org");

        ppt.write(new FileOutputStream("D:\\Users\\pptTest.pptx"));

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值