Excel .NET组件Spire.XLS教程:在不同工作簿之间复制单元格范围

*这么优秀的国产工具怎能错过呢!在线下单专享“一口价”,立即购买!想要获取更多福利的朋友可以咨询在线客服哦~


在不同工作簿之间复制单元格范围

Spire.XLS 提供了Worksheet.Copy()方法来将数据从源范围复制到目标范围。目标区域可以是同一工作簿内的单元格区域,也可以是来自不同工作簿的单元格区域。以下是将单元格区域从工作簿复制到另一个工作簿的步骤。

  • 创建一个Workbook对象以加载源 Excel 文档。
  • 分别使用Workbook.Worksheets属性和Worksheet.Range属性获取源工作表和源单元格范围。
  • 创建另一个Workbook对象以加载目标 Excel 文档。
  • 获取目标工作表和单元格范围。
  • 使用Worksheet.Copy(CellRange source, CellRange destRange)将数据从源范围复制到目标范围。
  • 将列宽从源范围复制到目标范围,以便数据可以在目标工作簿中正确显示。
  • 使用Workbook.SaveToFile()方法将目标工作簿保存到 Excel 文件。

C#

using Spire.Xls;namespace CopyCellRange{    class Program    {        static void Main(string[] args)        {            //Create a Workbook object            Workbook sourceBook = new Workbook();            //Load the source workbook            sourceBook.LoadFromFile(@"C:UsersAdministratorDesktopsource.xlsx");            //Get the source worksheet            Worksheet sourceSheet = sourceBook.Worksheets[0];            //Get the source cell range            CellRange sourceRange = sourceSheet.Range["A1:E4"];            //Create another Workbook objecy            Workbook destBook = new Workbook();            //Load the destination workbook            destBook.LoadFromFile(@"C:UsersAdministratorDesktopdestination.xlsx");            //Get the destination worksheet            Worksheet destSheet = destBook.Worksheets[0];            //Get the destination cell range            CellRange destRange = destSheet.Range["B2:F5"];            //Copy data from the source range to the destination range            sourceSheet.Copy(sourceRange, destRange);            //Loop through the columns in the source range            for (int i = 0; i < sourceRange.Columns.Length; i++)            {                //Copy the column widths also from the source range to destination range                destRange.Columns[i].ColumnWidth = sourceRange.Columns[i].ColumnWidth;            }                //Save the destination workbook to an Excel file            destBook.SaveToFile("CopyRange.xlsx");        }    }}

VB.NET

Imports Spire.XlsNamespace CopyCellRange    Class Program        Shared  Sub Main(ByVal args() As String)            'Create a Workbook object            Dim sourceBook As Workbook =  New Workbook()            'Load the source workbook            sourceBook.LoadFromFile("C:UsersAdministratorDesktopsource.xlsx")            'Get the source worksheet            Dim sourceSheet As Worksheet =  sourceBook.Worksheets(0)            'Get the source cell range            Dim sourceRange As CellRange =  sourceSheet.Range("A1:E4")            'Create another Workbook objecy            Dim destBook As Workbook =  New Workbook()            'Load the destination workbook            destBook.LoadFromFile("C:UsersAdministratorDesktopdestination.xlsx")            'Get the destination worksheet            Dim destSheet As Worksheet =  destBook.Worksheets(0)            'Get the destination cell range            Dim destRange As CellRange =  destSheet.Range("B2:F5")            'Copy data from the source range to the destination range            sourceSheet.Copy(sourceRange, destRange)            'Loop through the columns in the source range            Dim i As Integer            For  i = 0 To  sourceRange.Columns.Length- 1  Step  i + 1                'Copy the column widths also from the source range to destination range                destRange.Columns(i).ColumnWidth = sourceRange.Columns(i).ColumnWidth            Next            'Save the destination workbook to an Excel file            destBook.SaveToFile("CopyRange.xlsx")        End Sub    End ClassEnd Namespace

Excel .NET组件Spire.XLS教程:在不同工作簿之间复制单元格范围

是E-iceblue官方友好合作伙伴,如果您对spire.xls,可以联系在线客服了解具体授权价格和使用机制

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年9月26日
下一篇 2021年9月26日

相关推荐

发表回复

登录后才能评论