关于java:Java-创建删除Word表格

4次阅读

共计 3502 个字符,预计需要花费 9 分钟才能阅读完成。

在 Word 文档中,咱们能够通过增加表格的形式来帮忙咱们更加清晰、直观地剖析和展现数据。本文将介绍如何应用 Free Spire.Doc for Java 组件来给 Word 文档创建表格,及删除文档中已有的表格和表格内容。

增加产品及其依赖

形式 1: 通过官网下载组件,解压后将 lib 文件夹下的 Spire.Doc.jar 手动导入 IDEA 中。具体导入步骤参见下图。


形式 2: 通过 Maven 仓库装置产品及相干依赖。具体操作步骤参见此教程。

代码演示

创建表格

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 = {"Name", "Capital", "Continent", "Area"};
        String[][] data =
                {new String[]{"Indonesia", "Jakarta", "Southeast Asia", "1913579"},
                        new String[]{"Bangladesh", "Dhaka", "South Asia", "147570"},
                        new String[]{"Mexican", "Mexico City", "North America", "1964375"},
                        new String[]{"Kenya", "Nairobi", "East Africa", "582646"},
                        new String[]{"China", "Beijing", "East Asia", "9600000"},
                };

        // 增加表格
        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]);
                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("output/CreateTable.docx", FileFormat.Docx_2013);
    }
}

创立成果:

删除表格

* 删除整个表格
import com.spire.doc.*;
import com.spire.doc.interfaces.ITable;
public class DeleteTable1 {public static void main(String[] args) {
        // 创立实例
        Document doc = new Document();
        // 加载 Word 文档
        doc.loadFromFile("C:\\Users\\Test1\\Desktop\\CreateTable.docx");

        // 获取 Section
        Section section = doc.getSections().get(0);

        // 获取表格
        ITable table = section.getTables().get(0);

        // 删除表格
        section.getTables().remove(table);

        // 保存文档
        doc.saveToFile("output/DeleteTable.docx",FileFormat.Docx_2013);
        doc.dispose();}
}

删除成果:

* 仅删除表格中的内容
import com.spire.doc.*;
public class DeleteTable2 {public static void main(String[] args) {
        // 创立实例,加载测试文档
        Document doc = new Document();
        doc.loadFromFile("C:\\Users\\Test1\\Desktop\\CreateTable.docx");

        // 获取 Section
        Section section = doc.getSections().get(0);

        // 获取表格
        Table table =section.getTables().get(0);
        // 遍历表格每行
        for (int i = 0; i < table.getRows().getCount(); i++) {
            // 获取表格行
            TableRow row = table.getRows().get(i);
            // 遍历每行中的每个单元格
            for(int j = 0; j < row.getCells().getCount();j++)
            {
                // 获取单元格
                TableCell cell =  row.getCells().get(j);
                cell.getChildObjects().clear();// 革除所有子对象
                //cell.getCellFormat().clearFormatting();// 革除单元格格局
                //cell.getParagraphs().removeAt(0);// 删除单元格中的段落
            }
        }
        // 保存文档
        doc.saveToFile("output/DeleteContent.docx",FileFormat.Docx_2013);
        doc.dispose();}
}

删除成果:

(本文完)

正文完
 0