We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copy cells between Worksheets


I’m trying to move or copy a range of cells between worksheets, but I
can’t seem to find a suitable method. I’ve searched the forum, but
can’t seem to find an answer. Is there an easy way to do this?

Thanks in advance,


Hi Cruz,

Thanks for considering Aspose.

Well, There are no. of Options. You may use Range.Copy(rangeObject) method to copy the range with formattings. And If you want to copy only data from a range of cells you may export a range's data to fill a datatable using Range.ExportDataTableAsString() method and in other worksheet you may get the data using Cells.ImportDataTable() method. And Alternatively, you may also use Cells.ExportDataTableAsString() method to export the data to a data table.

May the following code help you for your need.

// Create a Workbook object
Workbook workbook = new Workbook();
// Open the Excel file
// Get the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];

//Define a style object adding a new style
//to the collection list.
Style stl2 = workbook.Styles[workbook.Styles.Add()];
//Set the custom cell shading color.
stl2.ForegroundColor = Color.Yellow;
//Set the solid background pattern for fillment color.
stl2.Pattern = BackgroundType.Solid;
//Set the font.
stl2.Font.Name = "Trebuchet MS";
//Set the font color.
stl2.Font.Color = Color.Maroon;
//Set the font size.
stl2.Font.Size = 10;
//Set the style flag struct.
StyleFlag flag = new StyleFlag();
//Apply cell shading.
flag.CellShading = true;
//Apply the font.
flag.FontName = true;
//Apply the font color.
flag.FontColor = true;
//Apply the font size.
flag.FontSize = true;

// Create a named range of the Cells
Range range = worksheet.Cells.CreateRange("A1", "C9");
// Set the name of the named range
range.Name = "TestRange1";
// Add a new Worksheet
int index =workbook.Worksheets.Add();
// Access the Worksheet
Worksheet testsheet = workbook.Worksheets[index];
// Create a DataTable
DataTable dt = new DataTable("testtable");
// Export data to the DataTable
dt =range.ExportDataTableAsString();
// Fill the Cells of the new sheet from the DataTable, only data will be obtained.
for (int i = 0;i< dt.Rows.Count; i++)
for (int j=0;j<dt.Columns.Count; j++ )

// Fill the Cells of the new sheet from the DataTable, only data will be obtained.

// Adjust the rows and columns of the sheet.
// Create another Range based on A21..C29 Cells.
Range range2 = worksheet.Cells.CreateRange("A21", "C29");
// Name the range.
range2.Name = "TestRange2";
// Get the total no. of columns in the range.
int cols = range2.ColumnCount;
// Get the total no. of rows in the range.
int rows = range2.RowCount;
// Get the first row index.
int frow = range2.FirstRow;
// Get the first column index
int fcol = range2.FirstColumn;
//Copy the range with data and formattings
// Save the modified Excel file in default format

Thank you.

Ah, I get it. Many thanks :wink: