java 生成 word 文档

java 生成  word 文档

引用 jar 包

<repositories>
		<repository>
			<id>public</id>
			<name>aliyun nexus</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
		</repository>
		<repository>
			<id>com.e-iceblue</id>
			<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
		</repository>
</repositories>
		<dependency>
			<groupId>e-iceblue</groupId>
			<artifactId>spire.doc.free</artifactId>
			<version>2.7.3</version>
		</dependency>

样例一 


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.replace("Jois", "NewText", false, true);

        //保存文档
        document.saveToFile("D:\workspace\idea\report\src\main\resources\CreateTable.docx", FileFormat.Docx_2013);
    }

 

样例二

import com.spire.doc.*;



 Document doc = new Document();
        doc.loadFromFile("D:\workspace\idea\report\src\main\resources\export_word.docx");

        //获取表格
        Section section = doc.getSections().get(0);
        Table table = section.getTables().get(0);

        table.addRow();//默认在表格最下方插入一行
        //table.getRows().insert(2,table.addRow());//在表格中第3行插入一行
        //table.addRow(4);//默认在表格最下方添加4个单元格
        //table.addRow(true,2);//带格式在最后一行添加2个单元格
        //table.addRow(false,2);//不带格式在最后一行添加2个单元格

//保存文档
        doc.saveToFile("D:\workspace\idea\report\src\main\resources\addrow.docx",FileFormat.Docx_2013);
        doc.dispose();

 

https://www.e-iceblue.cn/Downloads/Spire-Doc-JAVA.html

 

 

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » java 生成 word 文档