Java在Word中添加表格、嵌套表格-創(chuàng)新互聯(lián)

表格作為一種可視化交流模式及組織、整理數(shù)據(jù)的方法,在各種場合及文檔中應(yīng)用廣泛。常見的表格可包含文字、圖片等元素,我們操作表格時可以插入圖片、嵌套表格、寫入文字及格式化表格樣式等。下面,將通過Java編程在Word文檔中創(chuàng)建表格并實(shí)現(xiàn)格式化操作,包括設(shè)置字體、字號、字體顏色、字體粗細(xì)等,設(shè)置單元格對齊方式、單元格背景色、單元格合并/拆分、表格邊框樣式、插入圖片等等。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、成都小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了湖州免費(fèi)建站歡迎大家使用!

使用工具:Free Spire.Doc for Java (免費(fèi)版)
Jar文件獲取及導(dǎo)入:
方法1:通過官網(wǎng)下載文件包。下載后,解壓文件,并將lib文件夾下的Spire.Doc.jar文件導(dǎo)入java程序。
方法2:通過maven倉庫安裝導(dǎo)入,可參考導(dǎo)入方法。


Java代碼示例

【示例1】添加表格

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;
import java.awt.*;

public class CreateTable {
    public static void main(String[] args){
        //創(chuàng)建Document對象
        Document doc = new Document();
        Section sec = doc.addSection();

        //聲明數(shù)組內(nèi)容
        String[] header = {"班級","姓名","性別", "學(xué)號", "專業(yè)成績"};
        String[][] data =
                {
                        new String[]{"一班","王麗", "女", "Y1256486", "138"},
                        new String[]{"一班","洪菲菲", "女", "Y5425875", "134"},
                        new String[]{"二班","劉洋", "男", "B1546258", "141"},
                        new String[]{"三班","馮剛", "男", "B1542367", "136"},
                        new String[]{"三班","劉思源", "男", "Z1263547", "133"},
                };

        //添加表格
        Table table = sec.addTable(true);

        //設(shè)置表格的行數(shù)和列數(shù)
        table.resetCells(data.length + 1, header.length);

        //設(shè)置表格第一行作為表頭,寫入表頭數(shù)組內(nèi)容,并格式化表頭數(shù)據(jù)
        TableRow row = table.getRows().get(0);
        row.isHeader(true);
        row.setHeight(20);
        row.setHeightType(TableRowHeightType.Exactly);
        row.getRowFormat().setBackColor(Color.ORANGE);
        for (int i = 0; i < header.length; 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[i]);
            range1.getCharacterFormat().setFontName("Arial");
            range1.getCharacterFormat().setFontSize(12f);
            range1.getCharacterFormat().setBold(true);
            range1.getCharacterFormat().setTextColor(Color.white);
        }

        //寫入剩余組內(nèi)容到表格,并格式化數(shù)據(jù)
        for (int r = 0; r < data.length; r++) {
            TableRow dataRow = table.getRows().get(r + 1);
            dataRow.setHeight(25);
            dataRow.setHeightType(TableRowHeightType.Exactly);
            dataRow.getRowFormat().setBackColor(Color.white);
            for (int c = 0; c < data[r].length; c++) {
                dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
                range2.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
                range2.getCharacterFormat().setFontName("Arial");
                range2.getCharacterFormat().setFontSize(10f);
            }
        }

        //縱向合并指定單元格
        table.applyVerticalMerge(0,1,2);
        table.applyVerticalMerge(0,4,5);

        //插入圖片到指定單元格
        DocPicture dp = table.getRows().get(1).getCells().get(0).addParagraph().appendPicture("1.png");
        dp.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //設(shè)置單元格背景顏色
        for (int j = 1; j < table.getRows().getCount(); j++) {
            if (j % 2 == 0) {
                TableRow row2 = table.getRows().get(j);
                for (int f = 1; f < row2.getCells().getCount(); f++) {
                    row2.getCells().get(f).getCellFormat().setBackColor(new Color(144,238,144));
                }
            }
        }

        //設(shè)置表格邊框樣式
        table.getTableFormat().getBorders().setBorderType(BorderStyle.Thick_Thin_Large_Gap);

        //保存文檔
        doc.saveToFile("CreateTable.docx", FileFormat.Docx_2013);
    }
}

表格添加效果:

Java 在Word中添加表格、嵌套表格

【示例2】添加嵌套表格

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;

public class NestedTable {
    public static void main(String[]args){
        //加載測試文檔
        Document doc = new Document("sample.docx");

        //獲取指定表格中的單元格,并設(shè)置行高、列寬
Section sec = doc.getSections().get(0);
        Table table = sec.getTables().get(0);
        table.getRows().get(0).setHeight(120f);
        table.getRows().get(0).getCells().get(0).setWidth(380);

        //添加嵌套表格到指定單元格
        Table nestedtable = table.get(0,0).addTable(true);
        nestedtable.getTableFormat().setHorizontalAlignment(RowAlignment.Center);//設(shè)置嵌套表格在單元格中的對齊方式
        nestedtable.resetCells(4,4);//指定嵌套表格行數(shù)、列數(shù)
        nestedtable.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);//設(shè)置嵌套表格內(nèi)容自適應(yīng)方法
        //聲明表格數(shù)組內(nèi)容
        String[][] data ={
                new String[]{"編號","產(chǎn)區(qū)","最新型號","生產(chǎn)日期",},
                new String[]{"1","A","V2.2.0","2019-06-21"},
                new String[]{"2","B","V2.6.1","2019-06-18"},
                new String[]{"3","C","V2.6.2","2019-06-14"},
        };

        //填充數(shù)組內(nèi)容到嵌套表格
        for (int i = 0; i < data.length; i++) {
            TableRow dataRow = nestedtable.getRows().get(i);
            dataRow.getCells().get(i).setWidth(160);
            dataRow.setHeight(25);
            dataRow.setHeightType(TableRowHeightType.Exactly);
            for (int j = 0; j < data[i].length; j++) {
                dataRow.getCells().get(j).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                TextRange range = dataRow.getCells().get(j).addParagraph().appendText(data[i][j]);
                range.getCharacterFormat().setFontName("楷體");
                range.getCharacterFormat().setFontSize(11f);
                range.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            }
        }

        //保存文檔
        doc.saveToFile("nesedtable1.docx",FileFormat.Docx_2010);
    }
}

嵌套表格添加效果:
Java 在Word中添加表格、嵌套表格

(本文完)

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

分享標(biāo)題:Java在Word中添加表格、嵌套表格-創(chuàng)新互聯(lián)
分享路徑:http://bm7419.com/article14/didoge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google網(wǎng)站設(shè)計(jì)、網(wǎng)站營銷、虛擬主機(jī)外貿(mào)建站、微信公眾號

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)