How to Copy Excel Cells
It is a basic function to copy Excel cells in Microsoft Excel program. Most of us will use the copy function in our daily Excel develop jobs and that’s really convenient especially for a large content. In this article you will find a solution to copy Excel cell content including styles with C#.
Tools We Need
- Visual Studio
- Spire.XLS
Tool Introduction
Spire.XLS is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document for .NET and Silverlight.
Download Spire.XLS Here
How to Copy Excel Cells with Spire.XLS by Using C#
Make sure Spire.XLS and Visual Studio are correctly installed on system. Follow the simple instruction below to copy Excel cells.
Step 1 Create a Project
Create a C# project in Visual Studio and Add Spire.Pdf.dll as reference. The default setting of Spire.Pdf.dll is placed under "C:\Program Files\e-iceblue\Spire.Pdf\Bin”. Select assembly Spire.Pdf.dll and click OK to add it to the project.
using Spire.Xls;
namespace columnchart
{
class Program
{
static void Main(string[] args)
{
}
}
}
Step 2 Load Excel Files
Put the Excel file which has the cells we need copy into the project folder and use the code below to load it into the project.
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"..\..\..\..\Data\parts.xls",ExcelVersion.Version97to2003);
Worksheet worksheet = workbook.Worksheets[0];
Step 3 Copy Excel Cells
Select the cell range which we need copy and use the simple one sentence code below to copy the content and style into another area.
worksheet.Copy(worksheet.Range["A3:E5"],worksheet.Range["A19:E21"],true);
Step 4 Save and Preview
The following code can save the Excel file and preview the effect after launch the program.
workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
System.Diagnostics.Process.Start(workbook.FileName);
Effective Screenshot:
Tools We Need
- Visual Studio
- Spire.XLS
Tool Introduction
Spire.XLS is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document for .NET and Silverlight.
Download Spire.XLS Here
How to Copy Excel Cells with Spire.XLS by Using C#
Make sure Spire.XLS and Visual Studio are correctly installed on system. Follow the simple instruction below to copy Excel cells.
Step 1 Create a Project
Create a C# project in Visual Studio and Add Spire.Pdf.dll as reference. The default setting of Spire.Pdf.dll is placed under "C:\Program Files\e-iceblue\Spire.Pdf\Bin”. Select assembly Spire.Pdf.dll and click OK to add it to the project.
using Spire.Xls;
namespace columnchart
{
class Program
{
static void Main(string[] args)
{
}
}
}
Step 2 Load Excel Files
Put the Excel file which has the cells we need copy into the project folder and use the code below to load it into the project.
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"..\..\..\..\Data\parts.xls",ExcelVersion.Version97to2003);
Worksheet worksheet = workbook.Worksheets[0];
Step 3 Copy Excel Cells
Select the cell range which we need copy and use the simple one sentence code below to copy the content and style into another area.
worksheet.Copy(worksheet.Range["A3:E5"],worksheet.Range["A19:E21"],true);
Step 4 Save and Preview
The following code can save the Excel file and preview the effect after launch the program.
workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
System.Diagnostics.Process.Start(workbook.FileName);
Effective Screenshot:
More about Spire.XLS
Spire.XLS is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight. Click to learn more...
Spire.XLS is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight. Click to learn more...