Java 在Word中創建表格


Word文檔中,表格能使文本內容更加簡潔明了,同時也能使數據展示更加清晰直觀。 本文將介紹如何使Java代碼Word文檔中創建表格設置其單元格的背景顏色

 

Jar文件導入方法

方法一:

下載免費Free Spire.Doc for Java包並解壓縮然后從lib文件夾下,Spire.Doc.jar包導入到你的Java應用程序中。導入成功如下圖所示

 

 

方法二:

通過Maven倉庫安裝導入詳細的操作步驟請參考鏈接:

https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html

 

Java代碼示例

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

import java.awt.*;

public class CreateTable {
    public static void main(String[] args) {
        //創建Word文檔
        Document document = new Document();
        //添加一個section
        Section section = document.addSection();

        //數據
        String[] header = {"姓名", "性別", "部門", "工號"};
        String[][] data =
                {
                        new String[]{"Winny", "女", "綜合", "0109"},
                        new String[]{"Lois", "女", "綜合", "0111"},
                        new String[]{"Jois", "男", "技術", "0110"},
                        new String[]{"Moon", "女", "銷售", "0112"},
                        new String[]{"Vinit", "女", "后勤", "0113"},
                };

        //添加表格
        Table table = section.addTable(true);
        //設置表格的行數和列數
        table.resetCells(data.length + 1, header.length);

        //設置第一行作為表格的表頭並添加數據
        TableRow row = table.getRows().get(0);
        row.isHeader(true);
        row.setHeight(20);
        row.setHeightType(TableRowHeightType.Exactly);
        row.getRowFormat().setBackColor(Color.gray);
        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);
        }

        //添加數據到剩余行
        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.getCharacterFormat().setFontName("Arial");
                range2.getCharacterFormat().setFontSize(10f);
            }
        }

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

        //保存文檔
        document.saveToFile("創建表格.docx", FileFormat.Docx_2013);
    }
}

 

創建表格效果圖:

 

 

 

 


免責聲明!

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



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