java通過poi操作ppt


Java POI導出ppt簡單實現


Java使用poi組件導出ppt報表幻燈片,poi導出pptx表格可以合並單元格,輸出老版本的ppt不支持合並單元格,
下面介紹poi導出pptx的一些常用功能, 采用的是poi-3.8-20120326.jar,poi-ooxml-3.8-20120326.jar,poi-scratchpad-3.8-20120326.jar。

創建幻燈片

poi輸出pptx首先需要創建幻燈片,可以創建多個幻燈片,然后幻燈片中可以加入表格、圖片、文本等元素,如下通過ppt.createSlide()創建一個幻燈片,幻燈片中加入TextBox文本,需要指定TextBox坐標位置,長和寬可以設置為0,自動適應文本大小, 
如果不通過setAnchor()方法指定坐標,則幻燈片中不會顯示該文本元素。

          

1
2
3
4
5
6
7
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide(); //創建幻燈片
XSLFTextBox textBox = slide.createTextBox();
textBox.setAnchor( new Rectangle2D.Double( 10 , 10 , 0 , 0 ));
textBox.addNewTextParagraph().addNewTextRun().setText( "創建幻燈片" );
 
ppt.write( new FileOutputStream( "/Users/mike/ppt1.pptx" ));

幻燈片插入表格

幻燈片插入表格通過slide.createTable()方法創建表格,同樣table需要指定坐標位置,幻燈片的元素都需要指定坐標位置。

          

1
2
3
4
5
6
7
8
9
10
11
12
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]));
    }
}

設置表格樣式

單元格可以設置居左、居中、居右、上下居中、設置邊框、設置邊框顏色、設置單元格背景顏色, 設置文本居中是使用XSLFTextParagraph p 段落對象設置居中。

          

1
2
3
4
5
6
7
8
9
10
11
12
tableCell.setFillColor(Color.getColor( "0xdd7e6b" ));
p.setTextAlign(TextAlign.CENTER);
tableCell.setVerticalAlignment(VerticalAlignment.MIDDLE);
 
tableCell.setBorderBottom( 1 );
tableCell.setBorderLeft( 1 );
tableCell.setBorderTop( 1 );
tableCell.setBorderRight( 1 );
tableCell.setBorderBottomColor(Color.BLACK);
tableCell.setBorderLeftColor(Color.BLACK);
tableCell.setBorderTopColor(Color.BLACK);
tableCell.setBorderRightColor(Color.BLACK);

表格設置行高、列寬

有時幻燈片中表格文本比較多,需要設置表格的列寬度,在創建每行時設置高度,在創建表格之后設置表格每列寬度

          

1
2
3
4
tableRow.setHeight( 30 );
table.setColumnWidth( 0 , 150 );
table.setColumnWidth( 1 , 150 );
table.setColumnWidth( 2 , 250 );

文本設置字體樣式

單元格文本可設置字體大小、顏色、斜體、粗體、下划線等, 設置字體樣式時通過XSLFTextRun tr 對象設置。

          

1
2
3
4
5
6
tr.setFontSize( 16 );
tr.setBold( true );
tr.setItalic( true );
tr.setUnderline( true );
tr.setFontFamily( "\u5b8b\u4f53" );
tr.setFontColor(Color.RED);

合並單元格

合並單元格需要在表格創建完之后(行與列全部創建完之后),mergeCells()方法有四個參數,第一個參數:開始行,第二個參數:合並結束行,第三個參數:開始列,第四個參數:合並結束列。

          

1
2
//合並單元格
table.mergeCells( 0 , 0 , 0 , 2 );

幻燈片插入圖片

幻燈片中插入圖片首選在ppt對象中加入圖片生成一個idx圖片對應下標值,幻燈片對象slide創建圖片傳人下標值, 設置圖片在幻燈片中的絕對位置,圖片元素必須設置大小,否則不顯示。

          Java poi導出ppt

1
2
3
4
5
//插入圖片
byte [] bt = FileUtils.readFileToByteArray( new File( "/Users/mike/pie.png" ));
int idx = ppt.addPicture(bt, XSLFPictureData.PICTURE_TYPE_PNG);
XSLFPictureShape pic = slide.createPicture(idx);
pic.setAnchor( new Rectangle( 10 , 200 , 339 , 197 ));

創建一個鏈接

文本鏈接通過XSLFTextRun對象的createHyperlink()方法創建

          Java poi導出ppt

1
2
3
4
5
6
7
//創建一個文本鏈接
XSLFTextBox linkText = slide.createTextBox();
linkText.setAnchor( new Rectangle2D.Double( 430 , 310 , 0 , 0 ));
XSLFTextRun r = linkText.addNewTextParagraph().addNewTextRun();
r.setText( "Apache POI" );
XSLFHyperlink link = r.createHyperlink();
link.setAddress( "http://poi.apache.org" );

完整例子

poi導出pptx例子源碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileOutputStream;
 
import org.apache.commons.io.FileUtils;
import org.apache.poi.xslf.usermodel.TextAlign;
import org.apache.poi.xslf.usermodel.VerticalAlignment;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFHyperlink;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTable;
import org.apache.poi.xslf.usermodel.XSLFTableCell;
import org.apache.poi.xslf.usermodel.XSLFTableRow;
import org.apache.poi.xslf.usermodel.XSLFTextBox;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
 
public class TestExportPptx {
 
     public static void main(String[] args) throws Exception {
 
         XMLSlideShow ppt = new XMLSlideShow();
         XSLFSlide slide = ppt.createSlide(); //創建幻燈片
         XSLFTextBox textBox = slide.createTextBox();
         textBox.setAnchor( new Rectangle2D.Double( 10 , 10 , 0 , 0 ));
         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(TextAlign.CENTER);
                tableCell.setVerticalAlignment(VerticalAlignment.MIDDLE);
                
                if (i == datas.length - 1 && j == 3 - 1 ) {
                    tr.setFontSize( 16 );
                    tr.setBold( true );
                    tr.setItalic( true );
                    tr.setUnderline( true );
                    tr.setFontFamily( "\u5b8b\u4f53" );
                    tr.setFontColor(Color.RED);
                }
                
                tableCell.setBorderBottom( 1 );
                tableCell.setBorderLeft( 1 );
                tableCell.setBorderTop( 1 );
                tableCell.setBorderRight( 1 );
                tableCell.setBorderBottomColor(Color.BLACK);
                tableCell.setBorderLeftColor(Color.BLACK);
                tableCell.setBorderTopColor(Color.BLACK);
                tableCell.setBorderRightColor(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( "/Users/mike/pie.png" ));
        int idx = ppt.addPicture(bt, XSLFPictureData.PICTURE_TYPE_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 , 0 , 0 ));
        XSLFTextRun r = linkText.addNewTextParagraph().addNewTextRun();
        r.setText( "Apache POI" );
        XSLFHyperlink link = r.createHyperlink();
        link.setAddress( "http://poi.apache.org" );
                
         ppt.write( new FileOutputStream( "/Users/mike/ppt8.pptx" ));
     }
 
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM