原創|使用教程|編輯:張瑩心|2021-10-29 13:29:46.117|閱讀 580 次
概述:當您處理 Excel 文檔時,您可能需要從主工作簿復制數據并粘貼到單獨的工作簿中,這是一項常見任務。您可以在不同的工作簿之間復制選定的單元格區域或整個工作表。本文演示了如何使用Spire.XLS for .NET將選定的單元格范圍從一個工作簿復制到另一個工作簿。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Spire.XLS for .NET 是一款專業的 .NET Excel 組件, 它可以用在各種.NET 框架中,包括 .NET Core、ASP.NET 和 Windows Forms 等相關的.NET 應用程序。
本文介紹使用Spire.XLS for .NET來壓縮Excel工作表中的圖片。
在不同工作簿之間復制單元格范圍
Spire.XLS 提供了Worksheet.Copy()方法來將數據從源范圍復制到目標范圍。目標區域可以是同一工作簿內的單元格區域,也可以是來自不同工作簿的單元格區域。以下是將單元格區域從工作簿復制到另一個工作簿的步驟。
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:\Users\Administrator\Desktop\source.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:\Users\Administrator\Desktop\destination.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.Xls Namespace 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:\Users\Administrator\Desktop\source.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:\Users\Administrator\Desktop\destination.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 Class End Namespace
慧都是E-iceblue官方友好合作伙伴,如果您對spire.xls,可以了解具體授權價格和使用機制
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn