Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active January 30, 2022 18:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/badef5dcc0845289eb79e3fed242c6cd to your computer and use it in GitHub Desktop.
Save aspose-com-gists/badef5dcc0845289eb79e3fed242c6cd to your computer and use it in GitHub Desktop.
Copy Rows and Columns in Excel using C#
// Load Excel file
Workbook excelWorkbook1 = new Workbook("book1.xlsx");
// Get reference of the first worksheet
Worksheet ws1 = excelWorkbook1.Worksheets[0];
// Copy the first column from the first worksheet of the workbook
ws1.Cells.CopyColumn(ws1.Cells, ws1.Cells.Columns[0].Index, ws1.Cells.Columns[2].Index);
// Autofit the column
ws1.AutoFitColumn(2);
// Save the Excel file
excelWorkbook1.Save("output.xlsx");
// Load Excel file
Workbook excelWorkbook1 = new Workbook("book1.xlsx");
// Get reference of the first worksheet
Worksheet ws1 = excelWorkbook1.Worksheets[0];
// Copy the second row with data, formattings, images and drawing objects to the 16th row in the worksheet
ws1.Cells.CopyRow(ws1.Cells, 1, 15);
// Save the Excel file
excelWorkbook1.Save("output.xlsx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment