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