要将Excel表格显示在Word文档中,我们可以将Excel文档作为OLE对象插入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);
}
/// <summary>
/// 复制单元格格式
/// </summary>
/// <param name="wTextRange">Word表格:单元格数据</param>
/// <param name="xCell">Excel表格:单元格</param>
/// <param name="wCell">Word表格:单元格</param>
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
'<summary>
'复制单元格格式
'</summary>
'<param name="wTextRange">Word表格:单元格数据</param>
'<param name="xCell">Excel表格:单元格</param>
'<param name="wCell">Word表格:单元格</param>
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