|
| 1 | +--- |
| 2 | +title: Copy used range in Excel | Syncfusion |
| 3 | +description: Learn how to copy the used range from one Excel workbook to another using the Syncfusion .NET Excel library (XlsIO). |
| 4 | +platform: document-processing |
| 5 | +control: XlsIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# How to copy the used range from one Excel workbook to another? |
| 10 | + |
| 11 | +The [UsedRange](https://help.syncfusion.com/cr/document-processing/Syncfusion.XlsIO.IWorksheet.html#Syncfusion_XlsIO_IWorksheet_UsedRange) of [IWorksheet](https://help.syncfusion.com/cr/document-processing/Syncfusion.XlsIO.IWorksheet.html) returns the contiguous range that contains data in a worksheet. By default, Excel also considers cells that have only formatting applied as part of the used range. |
| 12 | + |
| 13 | +The following code examples show how to copy the used range from a source workbook to a destination workbook in C# (cross-platform and Windows-specific) and VB.NET. |
| 14 | + |
| 15 | +{% tabs %} |
| 16 | +{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/FAQ/Copy%20Used%20Range/.NET/CopyUsedRange/CopyUsedRange/Program.cs,180" %} |
| 17 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 18 | +{ |
| 19 | + IApplication application = excelEngine.Excel; |
| 20 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 21 | + IWorkbook sourceWorkbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Source.xlsx")); |
| 22 | + IWorkbook destinationWorkbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Destination.xlsx")); |
| 23 | + |
| 24 | + IWorksheet sourceWorksheet = sourceWorkbook.Worksheets["Sheet1"]; |
| 25 | + IWorksheet destinationWorksheet = destinationWorkbook.Worksheets["Sheet1"]; |
| 26 | + |
| 27 | + //Get the actual used range from source sheet |
| 28 | + IRange sourceRange = sourceWorksheet.UsedRange; |
| 29 | + |
| 30 | + //Copy the entire used range from source sheet to destination sheet |
| 31 | + sourceRange.CopyTo(destinationWorksheet.Range[sourceRange.Row, sourceRange.Column]); |
| 32 | + |
| 33 | + //Save the destination workbook |
| 34 | + destinationWorkbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx")); |
| 35 | +} |
| 36 | +{% endhighlight %} |
| 37 | + |
| 38 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 39 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 40 | +{ |
| 41 | + IApplication application = excelEngine.Excel; |
| 42 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 43 | + IWorkbook sourceWorkbook = application.Workbooks.Open("Source.xlsx"); |
| 44 | + IWorkbook destinationWorkbook = application.Workbooks.Open("Destination.xlsx"); |
| 45 | + |
| 46 | + IWorksheet sourceWorksheet = sourceWorkbook.Worksheets["Sheet1"]; |
| 47 | + IWorksheet destinationWorksheet = destinationWorkbook.Worksheets["Sheet1"]; |
| 48 | + |
| 49 | + //Get the actual used range from source sheet |
| 50 | + IRange sourceRange = sourceWorksheet.UsedRange; |
| 51 | + |
| 52 | + //Copy the entire used range from source sheet to destination sheet |
| 53 | + sourceRange.CopyTo(destinationWorksheet.Range[sourceRange.Row, sourceRange.Column]); |
| 54 | + |
| 55 | + //Save the destination workbook |
| 56 | + destinationWorkbook.SaveAs("Output.xlsx"); |
| 57 | +} |
| 58 | +{% endhighlight %} |
| 59 | + |
| 60 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 61 | +Using excelEngine As New ExcelEngine() |
| 62 | + Dim application As IApplication = excelEngine.Excel |
| 63 | + application.DefaultVersion = ExcelVersion.Xlsx |
| 64 | + Dim sourceWorkbook As IWorkbook = application.Workbooks.Open("Source.xlsx") |
| 65 | + Dim destinationWorkbook As IWorkbook = application.Workbooks.Open("Destination.xlsx") |
| 66 | + |
| 67 | + Dim sourceWorksheet As IWorksheet = sourceWorkbook.Worksheets("Sheet1") |
| 68 | + Dim destinationWorksheet As IWorksheet = destinationWorkbook.Worksheets("Sheet1") |
| 69 | + |
| 70 | + 'Get the actual used range from source sheet |
| 71 | + Dim sourceRange As IRange = sourceWorksheet.UsedRange |
| 72 | + |
| 73 | + 'Copy the entire used range from source sheet to destination sheet |
| 74 | + sourceRange.CopyTo(destinationWorksheet.Range(sourceRange.Row, sourceRange.Column)) |
| 75 | + |
| 76 | + 'Save the destination workbook |
| 77 | + destinationWorkbook.SaveAs("Output.xlsx") |
| 78 | +End Using |
| 79 | +{% endhighlight %} |
| 80 | +{% endtabs %} |
| 81 | + |
| 82 | +A complete working example to copy the used range from a source workbook to a destination workbook using C# is present on <a href="https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/FAQ/Copy%20Used%20Range/.NET/CopyUsedRange">this GitHub page</a>. |
0 commit comments