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

61 篇文章 3 订阅
4 篇文章 0 订阅

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;
            }
        }
    }
}


 

 

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值