1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > C# 如何将Excel表格复制到Word中并保留格式

C# 如何将Excel表格复制到Word中并保留格式

时间:2018-08-17 04:15:54

相关推荐

C# 如何将Excel表格复制到Word中并保留格式

C# 如何将Excel表格复制到Word中并保留格式

在以前的文章中,我分享了如何使用免费控件将word表格中的数据导入到excel中,在本篇文章中我将介绍如何将Excel表格复制到Word表格中,并同时保留原Excel表格的格式。

这里我们需要使用到一个组件,叫做Spire.Office。它是一个企业级的Office组件,包含了Spire.Doc, Spire XLS, Spire.Spreadsheet, Spire.Presentation, Spire.PDF, Spire.DataExport, Spire.OfficeViewer, Spire.PDFViewer, Spire.DocViewer和Spire.BarCode等组件,使用它我们可以实现在.NET应用程序中查看、操作、转换及打印Office文档,PDF文档,创建Barcode以及数据导入导出等工作。这里我们需要使用到的是其中的Spire.Doc和Spire.XLS组件。

首先,下载并安装Spire.Office,然后打开Visual Studio,创建项目并引用Spire.Doc.dll和Spire.Xls.dll。以下是我们需要用到的命名空间:

using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using Spire.Xls;

详细步骤如下:

步骤1:从系统加载Excel文件并获取它的第一个工作表。

Workbook workbook = new Workbook();workbook.LoadFromFile("DatatableSample.xlsx");Worksheet sheet = workbook.Worksheets[0];

步骤2:创建一个新的Word文档,添加一个表格并设置它的行列数与Excel表格中的行列数一致。

Document doc = new Document();//添加表格Table table = doc.AddSection().AddTable(true);//设置行列数table.ResetCells(sheet.LastRow, sheet.LastColumn);

步骤3:将Excel表格中的数据写入到word表格里,并调用自定义的方法CopyStyle()将Excel表格中的格式复制到word表格。

for (int r = 1; r <=sheet.LastRow; r++){for (int c = 1; c <= sheet.LastColumn; c++){CellRange xCell =sheet.Range[r, c];TableCell wCell =table.Rows[r - 1].Cells[c - 1];//将数据写入到word表格TextRange textRange =wCell.AddParagraph().AppendText(xCell.NumberText);//复制格式到word表格CopyStyle(textRange,xCell, wCell);}}

自定义的方法CopyStyle(TextRange wTextRange,CellRangexCell,TableCell wCell)如下:

参数:

wTextRange:Word表格中的文本

xCell:Excel表格中的单元格

wCell:Word表格中的单元格

private static void CopyStyle(TextRangewTextRange, 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;}}

步骤4:保存文档。

doc.SaveToFile("result.docx",Spire.Doc.FileFormat.Docx);

运行结果:

完整代码:

using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using Spire.Xls;namespace 复制Excel表格到Word并保留格式{class Program{static void Main(string[] args){//加载Excel文档Workbook workbook = newWorkbook();workbook.LoadFromFile("DatatableSample.xlsx");Worksheet sheet = workbook.Worksheets[0];//创建一个新的word文档Document doc = newDocument();//添加一个表格到word文档Table table = doc.AddSection().AddTable(true);//设置word表格的行列数table.ResetCells(sheet.LastRow, sheet.LastColumn);for (int r = 1; r<= sheet.LastRow; r++){for (intc = 1; c <= sheet.LastColumn; c++){CellRange xCell = sheet.Range[r, c];TableCell wCell = table.Rows[r -1].Cells[c - 1];//将Excel表格中的数据写入到word表格TextRangetextRange = wCell.AddParagraph().AppendText(xCell.NumberText);//将Excel表格中的格式复制到word表格CopyStyle(textRange, xCell, wCell);}}//设置word表格的列宽for (int i = 0; i< table.Rows.Count; i++){for (intj = 0; j < table.Rows[i].Cells.Count; j++){table.Rows[i].Cells[j].Width = 60f;}}//保存文档并打开doc.SaveToFile("result.docx",Spire.Doc.FileFormat.Docx);System.Diagnostics.Process.Start("result.docx");}private static void CopyStyle(TextRangewTextRange, CellRange xCell, TableCell wCell){//复制Excel单元格的字体格式到word表格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;//复制Excel单元格的背景颜色到word表格wCell.CellFormat.BackColor =xCell.Style.Color;//复制Excel单元格的字体排列方式到word表格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;}}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。