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.

Mon Sep 05, 2022 7:24 am

Hi.

I try to copy styles and set them in a different range.
If try to only one cell, it works, but not if used for multiple cells.

Code: Select all
CellRange[] ranges1 = worksheet.FindAllString("~FindText~", false, false);
CellStyle cellStyle = worksheet[ranges1[0].Row, ranges1[0].Column].Style;

worksheet[ranges1[0].Row+1 , ranges1[0].Column].Style = cellStyle;

this code is no problem.
but, below codes are some problem

1.
Code: Select all
CellRange[] ranges1 = worksheet.FindAllString("~FindText~", false, false);
CellStyle cellStyle = worksheet[ranges1[0].Row, ranges1[0].Column , ranges1[0].Row, worksheet.LastColumn].Style;

worksheet[ranges1[0].Row + 1, ranges1[0].Column, ranges1[0].Row + 1, worksheet.LastColumn].Style = cellStyle; // -> exception  System.NullReferenceException


2.
Code: Select all
CellRange[] ranges1 = worksheet.FindAllString("~FindText~", false, false);
CellStyle cellStyle = worksheet[ranges1[0].Row, ranges1[0].Column , ranges1[0].Row, worksheet.LastColumn].Style;

CellStyle tempStyle = worksheet[ranges1[0].Row + 1, ranges1[0].Column, ranges1[0].Row + 1, worksheet.LastColumn].Style;
tempStyle = cellStyle;
// No error occurs, but nothing works.


3.
Code: Select all
CellRange[] ranges1 = worksheet.FindAllString("~FindText~", false, false);
CellStyle cellStyle = worksheet[ranges1[0].Row, ranges1[0].Column , ranges1[0].Row, worksheet.LastColumn].Style;

CellStyle tempStyle = worksheet[ranges1[0].Row + 1, ranges1[0].Column, ranges1[0].Row + 1, worksheet.LastColumn].Style = cellStyle.clone();
// Exception  sprῆ: 'Font size is out of range.'.



I want to copy the style of the row.
Both CopyRow and Copy don't work either.
I want to copy only the style.
but, It copies the data, even if it gives an option.

Code: Select all
worksheet.CopyRow(worksheet[ranges1[0].Row, worksheet.LastColumn], worksheet, ranges1[0].Row + 1, CopyRangeOptions.CopyStyles) ;


Code: Select all
CellRange aaa = worksheet[ranges1[0].Row, ranges1[0].Column, ranges1[0].Row, worksheet.LastColumn];
CellRange bbb = worksheet[ranges1[0].Row+1, ranges1[0].Column, ranges1[0].Row+1, worksheet.LastColumn];
 worksheet.Copy(aaa, bbb,CopyRangeOptions.CopyStyles );



Is there a way to copy the style of the row, not one cell?

Sungkyu_min
 
Posts: 36
Joined: Fri Jan 07, 2022 5:53 am

Mon Sep 05, 2022 9:59 am

Hello,

Thanks for your inquiry.

I tried your code and found some possible problems in it. After modifiying, I got the correct result. Maybe you should just copy the styles from a single cell each time instead of multiple cells. The modified code is as follows, hope it will help you.
Code: Select all
            CellRange[] ranges1 = worksheet.FindAllString("TestString", false, false);
            //CellStyle cellStyle = worksheet[ranges1[0].Row, ranges1[0].Column, ranges1[0].Row, worksheet.LastColumn].Style;
            CellStyle cellStyle = ranges1[0].Style;

            worksheet[ranges1[0].Row + 2, ranges1[0].Column, ranges1[0].Row + 2, worksheet.LastColumn].Style = cellStyle;
            workbook.SaveToFile("output.xlsx",ExcelVersion.Version2013);

Screenshot of Result:
B67BBD3B-BFBD-48a9-B33B-9E60940A5E1F.png
B67BBD3B-BFBD-48a9-B33B-9E60940A5E1F.png (24.27 KiB) Viewed 741 times

If this is not what you want, please describe your needs and usage scenarios with an example (preferably if you can share an example file). Thanks in advance.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to Spire.XLS

cron