该文将介绍如何同时使用Spire.Doc和Spire.XLS ,将Excel 工作表里面的数据,含字体,样式,背景色等复制到Word表格中。
首先,请查看Excel示例文档:

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 ExcelToWordTable {
    public static void main(String[] args) throws Exception {
        //加载Excel 示例文档
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample00.xlsx");
        //获取第一个工作表
        Worksheet sheet = workbook.getWorksheets().get(0);
        //复制到Word文档
        copyToWord(sheet.getAllocatedRange(), "output.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;
            }
        }
    }效果图:

 



 
					



