ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java 复制Excel表格插入到Word

2021-06-22 11:53:50  阅读:200  来源: 互联网

标签:Java spire Excel jar doc import Word com


本篇文章介绍在Java程序中将Excel表格复制然后插入到Word文档中的方法。可插入到Word中的表格包含Excel原表格的所有表格样式,如单元格背景、字符样式、单元格合并拆分、单元格对齐方式等等。

程序运行环境

编译工具:IDEA

JDK版本:1.8.0

工具Jar包:free spire.office.jar 3.9.0

测试文档格式:.xlsx/.docx 2013

具体步骤

1.在Java程序中引入jar.

这里是通过手动导入本地的jar(需事先下载jar包到本地,然后解压文件,spire.office.jar文件在文件包的lib文件夹下);另外也可以通过Maven仓库下载导入,需在pom.xml文件中配置如下内容:

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.office.free</artifactId>
        <version>3.9.0</version>
    </dependency>
</dependencies>

配置完成后导入。

注:这里导入jar为spire.office.jar,这个包是个集合包,在程序中需同时操作excel和word文件,须导入此jar;不能将单个的spire.doc.jar和spire.xls.jar引入Java程序,会报错。

Java代码

import com.spire.doc.Document;
import com.spire.doc.Table;
import com.spire.doc.TableCell;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.VerticalAlignment;
import com.spire.doc.fields.TextRange;
import com.spire.xls.CellRange;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class CopyExcelTableToWord {
    public static void main(String[] args) {
        //加载Excel 示例文档
        Workbook workbook = new Workbook();
        workbook.loadFromFile("test.xlsx");
        //获取第一个工作表
        Worksheet sheet = workbook.getWorksheets().get(0);

        //复制到Word文档
        copyToWord(sheet.getAllocatedRange(), "result.docx");
    }
    public static void copyToWord(CellRange cell, String fPath) {
        //添加表格
        Document doc = new Document();
        Table table = doc.addSection().addTable(true);
        table.resetCells(cell.getRowCount(), cell.getColumnCount());
        //复制表格内容
        for (int r = 1; r <= cell.getRowCount(); r++) {
            for (int c = 1; c <= cell.getColumnCount(); c++) {
                CellRange xCell = cell.get(r, c);
                CellRange mergeArea = xCell.getMergeArea();
                //合并单元格
                if (mergeArea != null && mergeArea.getRow() == r && mergeArea.getColumn() == c) {
                    int rowIndex = mergeArea.getRow();
                    int columnIndex = mergeArea.getColumn();
                    int rowCount = mergeArea.getRowCount();
                    int columnCount = mergeArea.getColumnCount();

                    for (int m = 0; m < rowCount; m++) {
                        table.applyHorizontalMerge(rowIndex - 1 + m, columnIndex - 1, columnIndex + columnCount - 2);
                    }
                    table.applyVerticalMerge(columnIndex - 1, rowIndex - 1, rowIndex + rowCount - 2);

                }
                //复制内容
                TableCell wCell = table.getRows().get(r - 1).getCells().get(c - 1);
                if (!xCell.getDisplayedText().isEmpty()) {
                    TextRange textRange = wCell.addParagraph().appendText(xCell.getDisplayedText());
                    copyStyle(textRange, xCell, wCell);
                } else {
                    wCell.getCellFormat().setBackColor(xCell.getStyle().getColor());
                }
            }
        }
        doc.saveToFile(fPath,com.spire.doc.FileFormat.Docx);
}
    private static void copyStyle(TextRange wTextRange, CellRange xCell, TableCell wCell) {
        //复制字体样式
        wTextRange.getCharacterFormat().setTextColor(xCell.getStyle().getFont().getColor());
        wTextRange.getCharacterFormat().setFontSize((float) xCell.getStyle().getFont().getSize());
        wTextRange.getCharacterFormat().setFontName(xCell.getStyle().getFont().getFontName());
        wTextRange.getCharacterFormat().setBold(xCell.getStyle().getFont().isBold());
        wTextRange.getCharacterFormat().setItalic(xCell.getStyle().getFont().isItalic());
        //复制背景色
        wCell.getCellFormat().setBackColor(xCell.getStyle().getColor());
        //复制排列方式
        switch (xCell.getHorizontalAlignment()) {
            case Left:
                wTextRange.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
                break;
            case Center:
                wTextRange.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
                break;
            case Right:
                wTextRange.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
                break;
            default:
                break;
        }
        switch (xCell.getVerticalAlignment()) {
            case Bottom:
                wCell.getCellFormat().setVerticalAlignment(VerticalAlignment.Bottom);
                break;
            case Center:
                wCell.getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                break;
            case Top:
                wCell.getCellFormat().setVerticalAlignment(VerticalAlignment.Top);
                break;
            default:
                break;
        }
    }
}

 

执行程序后,生成Word结果文档。以上代码中的文件路径为IDEA项目程序运行路径,如本次路径F:\IDEAproject\CopyExcelTableToWord_Office\result.docx,文件路径可另行自定义。

测试的Excel源文档如图:

 

Word结果文档如图效果:

 

—End—

标签:Java,spire,Excel,jar,doc,import,Word,com
来源: https://blog.51cto.com/miayo/2936224

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有