Question

How can i copy one worksheet values and paste an another worksheet on within a workbook in aspose.cell using asp.net with c#?

Thanks and regard, Parthiban K.

Was it helpful?

Solution

I work as Social Media developer at Aspose. Aspose.Cells offer multiple options to achieve your desired results. You can Export the data from one worksheet to another and then import the data to the destination worksheet using . You can also copy all the data as range to the destination worksheet. Check the following samples:

Copy the Range from First Worksheet to destination worksheet

//Open the workbook
Workbook workbook = new Workbook("book1.xlsx");

//Select source worksheet
Worksheet worksheet = workbook.Worksheets[0];


//Select Destination Worksheet
Worksheet destSheet = workbook.Worksheets[1]; 

//Get the range of cells with all the data from source worksheet
Aspose.Cells.Range sourceRange = worksheet.Cells.MaxDisplayRange;

//Create a range with same row and column count as source worksheet
Aspose.Cells.Range destRange = destSheet.Cells.CreateRange(sourceRange.FirstRow, sourceRange.FirstColumn, sourceRange.RowCount, sourceRange.ColumnCount);

//Select Paste Options
PasteOptions options = new PasteOptions();

options.PasteType = PasteType.All;

//Copy the range from source worksheet to destination.
destRange.Copy(sourceRange, options);

//Save the updated worksheet
workbook.Save("book1.xlsx");

Export data from one worksheet and import to another

//Open the workbook
Workbook workbook = new Workbook("book1.xlsx");

//Select source worksheet
Worksheet worksheet = workbook.Worksheets[0];

//Exporting the of worksheet to DataTable
DataTable dataTable = worksheet.Cells.ExportDataTableAsString(0, 0, worksheet.Cells.MaxRow, worksheet.Cells.MaxColumn, true);

//Select Destination Worksheet
Worksheet destSheet = workbook.Worksheets[1];

//Import data to destination worksheet
destSheet.Cells.ImportDataTable(dataTable, true, "A1");

//Save the updated worksheet
workbook.Save("book1.xlsx");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top