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.

Fri Oct 11, 2019 12:40 am

Hello,

I want to copy range from one sheet to another:

var wk=workbook.CreateEmptySheet();
workbook.Worksheets[0].Copy(workbook.Worksheets[0].Range[1, 1, x - 1, y - 1], wk.Range[1, 1, x - 1, y - 1],true, true,true,true);

The content was copied correctly. However the height of rows and the width of columns were not copied. Is there a way to copy range with column width and row height?

Thanks

Lei

zhenlei1970
 
Posts: 9
Joined: Thu Oct 10, 2019 8:49 am

Fri Oct 11, 2019 2:15 am

Hi,

Thanks for your inquiry.
Sorry to tell you that there is no direct way to copy the cell range with column width and row height. But you could get the column width and row height of the cell range first, then assign them to the new cell range. Please refer to the code below.

Code: Select all
            var wk = workbook.CreateEmptySheet();

            //copy the content of the cell range
            workbook.Worksheets[0].Copy(sheet.Range[1, 1, x-1, y-1], wk.Range[1, 1, x-1, y-1], true, true, true, true);

            //copy the column width and row height of the cell range
            for (int i = 0; i < x-1 ;i++ )
            {
                wk.Rows[i].RowHeight = sheet.Rows[i].RowHeight;               
            }
            for (int j = 0; j < y-1 ;j++ )
            {
                wk.Columns[j].ColumnWidth = sheet.Columns[j].ColumnWidth;
            }


If the code couldn't solve your issue, to help us investigate it accurately, please offer us your input Excel file and your desired result file. You could upload them here or send us(support@e-iceblue.com) via email.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Tue Oct 29, 2019 1:48 am

Hi,

Greetings from E-iceblue.
Have you tried the code I offered you last time? Does it solve your issue?
Could you please give us some feedback at your convenience?

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Return to Spire.XLS