We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Alternate for CopyConditionalFormatting deprecated method?

Hi, before we used to copy Conditional Formatting using CopyConditionalFormatting method. Now since it is not available in newer version. How can we copy conditional formatting from one cell to multiple cells?

Old code:

Cell myCell = worksheet.GetRange("Some_Range")[0, 0];
for (int i = 1; i < ColumnCount; i++)
                worksheet.CopyConditionalFormatting(myCell .Row, myCell .Column, myCell .Row, myCell .Column + i);

@programmerboy,

Thanks for your query. We are working on this query and will share our feedback shortly.

@programmerboy,

Please use following sample code which copies conditional formatting from one cell to a complete range. Give it a try and share your feedback.

// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream("Book1.xlsx", FileMode.Open);

// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);

// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];

// Copying conditional format settings from cell "A1" to cell "B1"
//worksheet.CopyConditionalFormatting(0, 0, 0, 1);

int TotalRowCount = 0;

for (int i = 0; i < workbook.Worksheets.Count; i++)
{
    Worksheet sourceSheet = workbook.Worksheets[i];

    Range sourceRange = sourceSheet.Cells.MaxDisplayRange;

    //Range destRange = worksheet.Cells.CreateRange(sourceRange.FirstRow + TotalRowCount, sourceRange.FirstColumn,sourceRange.RowCount, sourceRange.ColumnCount);
    Range destRange = worksheet.Cells.CreateRange("Sheet1!A1:A20");

    destRange.Copy(sourceRange);

    TotalRowCount = sourceRange.RowCount + TotalRowCount;
}
            

// Saving the modified Excel file
workbook.Save("output3.xls");

// Closing the file stream to free all resources
fstream.Close();

Book1.zip (5.1 KB)
output3.zip (4.3 KB)

Thanks, Ahsan. However, I cannot download your files. I get this message.

β€œSorry, this file is private. Only visible to topic owner and staff members.”

Secondly, isn’t below line of code copy data as well, not just formatting? I only need to copy conditional formatting.

destRange.Copy(sourceRange);

@programmerboy,

You may download the sample book1.zip file here. If still you are not able to download this file, its just a sample file containing conditional formatting in Cell A1 of first sheet. You may please create sample file yourself using Excel.

Add following parameter to copy formatting only:
destRange.Copy(sourceRange, new PasteOptions() { PasteType = PasteType.Formats});

1 Like

Thanks, Ahsan. It works! :slightly_smiling_face:

@programmerboy,

Thank you for providing the feedback and you are welcome to ask more queries if required.