Written some function for coping row from Source cell to Destination cell. For that tried with Cells.CopyRow method
My requirement is copy 6th row from CellSource into CellDest file 1st row. MyCopyRowHack(cellsSource, 6, cellsDest, 1);
private void MyCopyRowHack(Cells cellsSource, int nSourceRow, Cells cellsDest, int nDestRow) { //Copy the row -- this is not working??? cellsDest.CopyRow(cellsSource, nSourceRow, nDestRow); }
Below function is working without using 'CopyRow', but its lengthy method. private void MyCopyRowHack(Cells cellsSource, int nSourceRow, Cells cellsDest, int nDestRow) {
// copy 1 column at a time A through C cellsDest["A" + nDestRow.ToString()].Value = cellsSource["A" + nSourceRow.ToString()].Value; cellsDest["B" + nDestRow.ToString()].Value = cellsSource["B" + nSourceRow.ToString()].Value; cellsDest["C" + nDestRow.ToString()].Value = cellsSource["C" + nSourceRow.ToString()].Value; }
Through the below source code, I have copied the 2nd row from Sheet1 of Book1.xlsx to 2nd row of Sheet2 in same workbook. Moreover, I have copied 2 rows [2nd and 3rd] from Sheet1 of Book1.xlsx to another Workbook. My input and output files are attached in an archive for your reference.
C# Source Code:
//Create a new Workbook. Open the existing excel file.
Workbook excelWorkbook = new Workbook(@“C:\temp\Book1.xlsx”);
//Get the first worksheet
Worksheet sheet1 = excelWorkbook.Worksheets[0];
//Get the second worksheet
Worksheet sheet2 = excelWorkbook.Worksheets[1];
//Copy the second row with data, formattings, images and drawing objects
//to the second row in the sheet2.
sheet2.Cells.CopyRow(sheet1.Cells, 1, 1);
//Create another workbook
Workbook newWorkbook = new Workbook();
//Get first workssheet
Worksheet newsheet1 = newWorkbook.Worksheets[0];
//Copy the second and third row to new workbook’s first worksheet
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.