java 使用 aspose 插入文本、图片、表格

  • Post author:
  • Post category:java


1、java 使用 aspose 插入表格

    /**
     * 插入表格
     *
     * @throws Exception
     */
    @Test
    public void testSaveWordTable() throws Exception {

        // Create a Document object
        Document doc = new Document();
        // Create a DocumentBuilder object
        DocumentBuilder builder = new DocumentBuilder( doc );
        // Create table
        Table table = builder.startTable();
        // Insert a cell
        builder.insertCell();
        table.autoFit( AutoFitBehavior.AUTO_FIT_TO_CONTENTS );
        builder.getCellFormat().setVerticalAlignment( CellVerticalAlignment.CENTER );
        builder.write( "评卷人" );
        builder.insertCell();
        builder.write( "得分" );
        // End row
        builder.endRow();
        // start a next row and set its properties
        builder.getRowFormat().setHeight( 20 );
        builder.getRowFormat().setHeightRule( HeightRule.EXACTLY );
        builder.insertCell();
        builder.write( "" );
        builder.insertCell();
        builder.write( "" );
        builder.endRow();
        // End table
        builder.endTable();
        // Save the document
        doc.save( "D:\\temp\\Rich Word Document5.docx" );

    }

插入表格2

  /**
     * Aspose.Words 是一款功能强大的word文档处理控件,在不需要安装word的条件下,可进行word 的创建、修改、转换等操作
     * Aspose.words 可以简单使用该产品提供的DocumentBuilder类库进行word 表格的插入
     * DocumentBuilder.StartTable 开始创建一个新的表格
     * DocumentBuilder.InsertCell 插入新的行和单元格到表格
     * DocumentBuilder.Writeln 为当前单元格写入文本
     * DocumentBuilder.EndRow 用于指示结束当前行,并且开始行的一行
     * DocumentBuilder.EndTable 表示表格构建完成
     */
    @Test
    public void testInsertTable() throws Exception {
        //下面的代码,展示了如何插入一个简单无格式的表格到word
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder( doc );


        builder.startTable();
        builder.insertCell();
        builder.write( "Row 1 ,Cell 1 Content " );

        // Build the second cell
        builder.insertCell();
        builder.write( "Row 1 ,Cell 2 Content " );
        //Call the following method to end the row and start a new row
        builder.endRow();

        // build the second cell
        builder.insertCell();
        builder.write( "Row 2 ,Cell 1 Content " );

        // Build the second cell
        builder.insertCell();
        builder.write( "Row 2 ,Cell 2 Content " );
        //Call the following method to end the row and start a new row
        builder.endRow();
        //Signal that we have finished building the table
        builder.endTable();

        doc.save( "F:\\www\\wwwroot\\temp\\table1.doc" );

    }

3插入表格,表格居中

  //表格居中显示
    @Test
    public void testInsertTable3() throws Exception {
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder( doc );
        Table table = builder.startTable();

        // Insert a cell
        builder.insertCell();
        //AutoFitBehavior.AUTO_FIT_TO_CONTENTS 根据内容调整表格
        // AUTO_FIT_TO_WINDOW 根据窗口调整表格
        // FIXED_COLUMN_WIDTHS  固定列宽
        table.autoFit( AutoFitBehavior.FIXED_COLUMN_WIDTHS );
        builder.getCellFormat().setVerticalAlignment( CellVerticalAlignment.CENTER );
        builder.getParagraphFormat().setAlignment( ParagraphAlignment.CENTER );
        builder.getCellFormat().setWidth( 50 );//设置列宽度
        builder.write( "题号" );
        for (int i = 0; i < 3; i++) {
            builder.insertCell();
            builder.write( NumberUtil.int2chineseNum( i + 1 ) );
        }
        builder.insertCell();
        builder.write( "总分" );
        // End row
        builder.endRow();
        // start a next row and set its properties
        builder.getRowFormat().setHeight( 20 );
        builder.getRowFormat().setHeightRule( HeightRule.EXACTLY );
        builder.insertCell();
        builder.write( "得分" );
        for (int i = 0; i < 3; i++) {
            builder.insertCell();
            builder.write( "" );
        }
        builder.insertCell();
        builder.write( "" );
        builder.endRow();
        // End table
        builder.endTable();
        //表格画完后,表格居中
        table.setAlignment( TableAlignment.CENTER );//表格整体居中

        doc.save( "F:\\www\\wwwroot\\temp\\table3.doc" );

    }

4、插入表格,表格进行不同设置

 //表格设置不同的宽度
    @Test
    public void testInsertTable2() throws Exception {
        //下面的代码,展示了如何插入一个简单无格式的表格到word
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder( doc );

        Table table = builder.startTable();

        //make the header row
        builder.insertCell();

        //Set the left indent for the table. Table wide formatting must be applied after
        // at least one row is present in the table
        table.setLeftIndent( 20 );

        //Set height and define the height rule for the header row
        builder.getRowFormat().setHeight( 40 );
        builder.getRowFormat().setHeightRule( HeightRule.AT_LEAST );

        //Some special features for the header row
        builder.getCellFormat().getShading().setBackgroundPatternColor( Color.white );
        builder.getParagraphFormat().setAlignment( ParagraphAlignment.CENTER );
        builder.getFont().setSize( 16 );
        builder.getFont().setBold( true );

        builder.getCellFormat().setWidth( 100 );
        builder.write( "Header row ,cell 1" );

        //we don't need to specify the width of this cell because it's inherited from the previous cell.
        builder.insertCell();
        builder.write( "Header row ,cell 2" );

        builder.insertCell();
        builder.getCellFormat().setWidth( 200 );
        builder.write( "Header row ,cell 3 " );
        //Call the following method to end the row and start a new row
        builder.endRow();


        //Set features for the other rows and cells
        builder.getCellFormat().getShading().setBackgroundPatternColor( Color.white );
        builder.getCellFormat().setWidth( 100 );
        builder.getCellFormat().setVerticalAlignment( CellVerticalAlignment.CENTER );

        //reset height and define a different height rule for table body
        builder.getRowFormat().setHeight( 30 );
        builder.getRowFormat().setHeightRule( HeightRule.AUTO );
        builder.insertCell();
        builder.getFont().setSize( 12 );
        builder.getFont().setBold( false );

        //build the other cells
        builder.write( "row 1 ,cell 1 content  " );
        builder.insertCell();
        builder.write( "row 1 ,cell 2 content  " );

        builder.insertCell();
        builder.getCellFormat().setWidth( 200 );
        builder.write( "row 1 ,cell 3 content  " );
        builder.endRow();

        builder.insertCell();
        builder.getCellFormat().setWidth( 100 );
        builder.write( "row 2 ,cell 1 content  " );

        builder.insertCell();
        builder.write( "row 2 ,cell 2 content  " );

        builder.insertCell();
        builder.getCellFormat().setWidth( 200 );
        builder.write( "row 2 ,cell 3 content  " );
        builder.endRow();
        builder.endTable();


        Cell nodes = table.getRows().get( 1 ).getCells().get( 2 );
        Cell nodes1 = table.getRows().get( 2 ).getCells().get( 2 );
        //合并单元格
        mergeCells( nodes, nodes1 );

        //设置边框的颜色
        table.setBorder( BorderType.RIGHT, LineStyle.DOUBLE, 1, Color.WHITE, false );
        table.setBorder( BorderType.LEFT, LineStyle.DOUBLE, 1, Color.WHITE, false );
        //所有的边框都以为白色
        table.setBorders( LineStyle.DOUBLE, 1, Color.WHITE );


        doc.save( "F:\\www\\wwwroot\\temp\\table2.doc" );

    }



版权声明:本文为xzj80927原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。