Java in PPT add, modify tables

This article describes the method in the form of PPT operation through the Java programming. Comprising adding to PPT slide table, and set the table style, cell alignment, cell background color, border style, font, size, and other merged cell; Meanwhile, by loading existing slide table may be implemented to form add, delete rows or columns, set the table borders and row height / column width, etc..

 

Use tools : Free Spire.Presentation for Java (free version)

 

Jar file acquisition:

Method 1 : through the official website to download jar files. Download and unzip the file to import lib folder Spire.Presentation.jar file to the Java program.

Method 2 : By maven install the program into the warehouse.

 

Java code sample (for reference)

 

[Example 1 ] Adding table

* com.spire.presentation Import;. 
Import in java.awt *;. 

public class AddTable { 
    public static void main (String [] args) throws Exception { 
        // instantiate a Presentation Object 
        Presentation Presentation Presentation new new = (); 

        // set the number of rows in the table and the number of columns, the row height and the column width 
        Double [] WIDTHS = new new Double [] {100D, 100D, 100D, 100D, 100D}; 
        Double [] Heights = new new Double [] {15d, 15d, 15d, 15d, 15d, 15d}; 

        // Add a table 
        ITable table = presentation.getSlides () get ( 0) .getShapes () appendTable ((float) presentation.getSlideSize () getSize () getWidth () / 2.... - 275, 90, WIDTHS, Heights); 

        // set the table built-in styles 
        table.setStylePreset (TableStylePreset.LIGHT_STYLE_1_ACCENT_6);

        // declare a String array 
        String [] [] = dataStr new new String [] [] 
                { 
                        { "class", "name", "gender", "study", "Total"}, 
                        { "Class 1" "Li Longfei", "M", "Y010956", "658"}, 
                        { "class. 1", "funan", "M", "Y011561", "686"}, 
                        { "class. 5", "Chen Lin "," female "," Y011650 "," 654 "}, 
                        {" class 9 "," room Fen "," female "," Y011258 "," 638 "}, 
                        {" class 13 "," Zhang " "M", "Y011328", "645 "} 
                }; 

        // padding data to the table 
        for (int I = 0; I <. 6; I ++) 
        { 
            for (int J = 0; J <. 5; J ++) 
            { 
                table.get (J, I) .getTextFrame () .setText (dataStr [i] [j ]);
                . table.get (J, I) .getTextFrame () getParagraphs () GET (0) .setAlignment (TextAlignmentType.CENTER);. 
            } 
        } 

        // the merged cell 
        table.mergeCells (table.get (0, 2) , table .get (0,2), false); 

        // set the table border style 
        table.setTableBorder (TableBorderType.Inside_Horizontal, 1, Color.black); 

        // save the file 
        presentation.saveToFile ( "add a table .pptx", FileFormat.PPTX_2013 ); 
    } 
}

 

Form add effects:


 

 

[Example 2 ] edit, modify existing slide table

* com.spire.presentation Import;. 
Import in java.awt *;. 

public ModifyTable class { 
    public static void main (String [] args) throws Exception { 
        // test document loading 
        the Presentation the Presentation PPT = new new (); 
        ppt.loadFromFile ( "test.pptx"); 

        // get a table 
        .. ITable the table = (ITable) ppt.getSlides () gET (0) .getShapes () gET (0); 

        // add rows and columns (default inserted at the bottom of the table line) 
        table.getTableRows () the append (table.getTableRows (.) GET (0));. 
        table.getColumnsList () the Add (table.getColumnsList (.) GET (0));. 

        // insert rows or columns (in inserting a specified position or row / column) 
        table.getTableRows () iNSERT (0, table.getTableRows (.) GET (. 1)).;
        . table.getColumnsList () INSERT (0, table.getColumnsList () GET (. 1).);
 
        high // set the row, column width
        . table.getTableRows () GET (0) .setHeight (50); 
        table.getColumnsList () GET (0) .setWidth (100);. 

        // delete the specified row or column 
        . table.getTableRows () removeAt (2, false ); 
        . table.getColumnsList () removeAt (2, false); 

        // set the table border 
        table.setTableBorder (TableBorderType.All, 1, Color.black); 

        // save the document 
        ppt.saveToFile ( "Modified.pptx", FileFormat .PPTX_2013); 
    } 
}

 

After completing the code, run the program, the table can view the generated document editing effects.

 

(This article End)

 

Guess you like

Origin miaonly.iteye.com/blog/2442740