该文将介绍如何同时使用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;
}
}
}
效果图: