Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Thu May 10, 2012 9:22 am

Hallo,

i want to try copy rows. At first i insert a new row with
Code: Select all
worksheet->InsertRow(range->row + 1 , range->LastRow);

Can I copy all Datas from range into new Row?

Thanks in advance and Have a nice day !

Lars

lameb
 
Posts: 10
Joined: Mon Feb 20, 2012 2:39 pm

Fri May 11, 2012 7:44 am

Hi lameb,

We have solved your problem of rows copying. Below are the steps and codes to solve it, please have a try.
1, Locate the source rows which need to be copyed.
2, Insert same number of target rows in a new position.
3, Copy data from the source rows to the target row.

Code: Select all
using Spire.Xls;

namespace XlsCopyRows
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\..\MiscDataTable.xls");

            Worksheet worksheet = workbook.Worksheets[0];
            int firstColumn = worksheet.FirstColumn;
            int lastColumn = worksheet.LastRow;
            int firstRow = worksheet.FirstRow;
            int lastRow = worksheet.LastRow;
            //rows count
            int copyRows = lastRow - firstRow + 1;
            //insert rows count
            worksheet.InsertRow(lastRow + 1, copyRows);
            CellRange originDataRang = worksheet.Range[firstRow, firstColumn, lastRow, lastColumn];
            CellRange targetDataRang = worksheet.Range[lastRow + 1, firstColumn, lastRow + 1, lastColumn];
            worksheet.Copy(originDataRang, targetDataRang, true);

            workbook.SaveToFile("Sample.xls");
            try
            {
                System.Diagnostics.Process.Start(workbook.FileName);
            }
            catch
            {
            }
        }
    }
}

It works fine on my side. But if there is something not smooth on your side, Please do not hesitate to contact us for anything at any time.

Regards
Eric
e-iceblue support
Attachments
MiscDataTable.zip
(2.5 KiB) Downloaded 541 times

Eric
 
Posts: 17
Joined: Tue Apr 24, 2012 4:53 am

Wed May 16, 2012 8:23 am

Hi Eric,

Thank you for your suggested solution.
The coding works.

Best Regards,
Lars

lameb
 
Posts: 10
Joined: Mon Feb 20, 2012 2:39 pm

Thu Jun 28, 2012 3:41 am

Please feel free to contact us for anything at any time. Have a great day!
e-iceblue support
User avatar

iceblue support
 
Posts: 240
Joined: Tue Dec 21, 2010 2:56 am

Mon Sep 05, 2022 6:11 pm

Hi, I did follow the instruction and the copy / paste working but it doesn't copy the Drawing Shaped althought i already use the Copy with Shape
Code: Select all
ws.Copy(originDataRang, targetDataRang, true, true, true, true);


Any suggestion on how to copy & paste with the shaped as well ?

Below is my code :

Code: Select all
 string path = "c:/CODE/test.xlsx";
            Workbook workbook = new();
            workbook.LoadFromFile(path/*, ExcelVersion.Version2010*/);

            var ws = workbook.Worksheets[0];

            int firstColumn = ws.FirstColumn;
            int lastColumn = ws.LastColumn;
            int firstRow = ws.FirstRow;
            int lastRow = ws.LastRow;
            //rows count
            int copyRows = lastRow - firstRow + 1;
            //insert rows count
            ws.InsertRow(lastRow + 1, copyRows);
            CellRange originDataRang = ws.Range[firstRow, firstColumn, lastRow, lastColumn];
            CellRange targetDataRang = ws.Range[lastRow + 1, firstColumn, lastRow + 1, lastColumn];
            ws.Copy(originDataRang, targetDataRang, true, true, true, true);

            workbook.SaveToFile("c:/CODE/test2.xlsx");
Attachments
test2.rar
test copy drawing
(9.16 KiB) Downloaded 71 times

vulamdien
 
Posts: 1
Joined: Mon Sep 05, 2022 6:04 pm

Tue Sep 06, 2022 9:47 am

Hello vulamdien,

Thanks for your inquiry.
Your issue is caused that the ws.FirstColumn returned 2 but not 1. This return issue has been logged into our bug tracking system with the ticket SPIREXLS-4111. Our Dev team will further investigate it. We will keep you posted if there is any updates.
Meanwhile, you can use the following code to achieve your demand.
Code: Select all
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"test2.xlsx", ExcelVersion.Version2010);
            var ws = workbook.Worksheets[0];
            int lastColumn = ws.LastColumn;
            int firstRow = ws.FirstRow;
            int lastRow = ws.LastRow;
            //rows count
            int copyRows = lastRow - firstRow + 1;
            //insert rows count
            ws.InsertRow(lastRow + 1, copyRows);
            CellRange originDataRang = (CellRange)ws.MaxDisplayRange;
            CellRange targetDataRang = ws.Range[lastRow + 1, 1, lastRow + 1, lastColumn];
            ws.Copy(originDataRang, targetDataRang, true, true, true, true);

            workbook.SaveToFile(@"resut.xlsx");


Sincerely,
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Fri Oct 21, 2022 1:35 am

Hello,

Thanks for your patience!
Glad to inform you that we just released Spire.XLS Pack Version:12.10 which fixes the issue with SPIREXLS-4111. Please download the new version from the following links to test.
Website download link: https://www.e-iceblue.com/Download/down ... t-now.html
Nuget download link: https://www.nuget.org/packages/Spire.XLS/12.10.1

Sincerely,
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Return to Spire.XLS