要将excel表格显示在word文档中,我们可以将,也可以将excel表格复制到word中。本文介绍如何将excel表格复制到word并保留excel中的表格样式。
此方案需要在项目中同时引用spire.xls.dll和spire.doc.dll,请下载spire.office并使用其中的dll文件。
c#
static void main(string[] args)
{
//创建workbook对象
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile(@"c:\users\administrator\desktop\sample.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//创建document对象,即word文档
document doc = new document();
//在word中添加表格
table table = doc.addsection().addtable(true);
//根据excel表格数据所占的行数和列数设置表格的行和列
table.resetcells(sheet.lastrow, sheet.lastcolumn);
//遍历excel表格的行
for (int r = 1; r <= sheet.lastrow; r )
{
//遍历excel表格的列
for (int c = 1; c <= sheet.lastcolumn; c )
{
//获取excel表格的单元格
cellrange xcell = sheet.range[r, c];
//获取word表格的单元格
tablecell wcell = table.rows[r - 1].cells[c - 1];
//将excel单元格数据填充到对应的word单元格
textrange textrange = wcell.addparagraph().appendtext(xcell.numbertext);
//复制单元格格式
copystyle(textrange, xcell, wcell);
}
}
//保存文档
doc.savetofile("result.docx", spire.doc.fileformat.docx);
}
///
/// 复制单元格格式
///
/// word表格:单元格数据
/// excel表格:单元格
/// word表格:单元格
private static void copystyle(textrange wtextrange, cellrange xcell, tablecell wcell)
{
//复制字体样式
wtextrange.characterformat.textcolor = xcell.style.font.color;
wtextrange.characterformat.fontsize = (float)xcell.style.font.size;
wtextrange.characterformat.fontname = xcell.style.font.fontname;
wtextrange.characterformat.bold = xcell.style.font.isbold;
wtextrange.characterformat.italic = xcell.style.font.isitalic;
//复制单元格背景色
wcell.cellformat.backcolor = xcell.style.color;
//复制文字对齐方式
switch (xcell.horizontalalignment)
{
case horizontalaligntype.left:
wtextrange.ownerparagraph.format.horizontalalignment = horizontalalignment.left;
break;
case horizontalaligntype.center:
wtextrange.ownerparagraph.format.horizontalalignment = horizontalalignment.center;
break;
case horizontalaligntype.right:
wtextrange.ownerparagraph.format.horizontalalignment = horizontalalignment.right;
break;
}
}
vb.net
private shared sub main(byval args() as string)
'创建workbook对象
dim workbook as workbook = new workbook
'加载excel文档
workbook.loadfromfile("c:\users\administrator\desktop\sample.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'创建document对象,即word文档
dim doc as document = new document
'在word中添加表格
dim table as table = doc.addsection.addtable(true)
'根据excel表格数据所占的行数和列数设置表格的行和列
table.resetcells(sheet.lastrow, sheet.lastcolumn)
'遍历excel表格的行
dim r as integer = 1
do while (r <= sheet.lastrow)
'遍历excel表格的列
dim c as integer = 1
do while (c <= sheet.lastcolumn)
'获取excel表格的单元格
dim xcell as cellrange = sheet.range(r, c)
'获取word表格的单元格
dim wcell as tablecell = table.rows((r - 1)).cells((c - 1))
'将excel单元格数据填充到对应的word单元格
dim textrange as textrange = wcell.addparagraph.appendtext(xcell.numbertext)
'复制单元格格式
(c copystyle(textrange, xcell, wcell))
c = (c 1)
loop
r = (r 1)
loop
'保存文档
doc.savetofile("result.docx", spire.doc.fileformat.docx)
end sub
'
'复制单元格格式
'
'word表格:单元格数据
'excel表格:单元格
'word表格:单元格
private static void copystyle(textrange wtextrange, cellrange xcell, tablecell wcell)
private shared sub copystyle(byval wtextrange as textrange, byval xcell as cellrange, byval wcell as tablecell)
'复制字体样式
wtextrange.characterformat.textcolor = xcell.style.font.color
wtextrange.characterformat.fontsize = ctype(xcell.style.font.size,single)
wtextrange.characterformat.fontname = xcell.style.font.fontname
wtextrange.characterformat.bold = xcell.style.font.isbold
wtextrange.characterformat.italic = xcell.style.font.isitalic
'复制单元格背景色
wcell.cellformat.backcolor = xcell.style.color
'复制文字对齐方式
select case (xcell.horizontalalignment)
case horizontalaligntype.left
wtextrange.ownerparagraph.format.horizontalalignment = horizontalalignment.left
case horizontalaligntype.center
wtextrange.ownerparagraph.format.horizontalalignment = horizontalalignment.center
case horizontalaligntype.right
wtextrange.ownerparagraph.format.horizontalalignment = horizontalalignment.right
end select
end sub