Interop Excel and conditional formatting

omega123

New member
Joined
Oct 8, 2024
Messages
1
Programming Experience
Beginner
Hello.

How can I use Interop Excel to perform conditional formatting on excel cells?

In the cells from A1 to A100, I have a drop-down list where the user can choose one of the currencies "USD, EUR, CNY". Initially, the cells are empty—the user selects the currency only after opening the file.

How can I apply conditional formatting to the cells from B1 to B100 in the format "$ #,##0.00" "€ #,##0.00" "¥ #,##0.00", so that there is correspondence between A1 and B1, A2 and B2, and so on? At the same time, the user should be allowed to enter only numbers, and if they try to enter text or spaces between digits, an error should be displayed with validation.ErrorMessage = "Please enter a numeric value."

I couldn’t manage to set the formatting based on the value in another cell. The issue is that the cells A1 to A100 are initially empty, and the user manually selects the currency in Excel.

There is no possibility to use macros in Interop for this task.

Here is the beginning of my code. Please advise how to proceed.


C#:
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = false;
Excel.Workbook workbook = excelApp.Workbooks.Add();
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets.Add();
Excel.Range currencyRange = worksheet.Range["A1", "A100"];
currencyRange.Validation.Delete();
currencyRange.Validation.Add(
Excel.XlDVType.xlValidateList,
Excel.XlDVAlertStyle.xlValidAlertStop,
Excel.XlFormatConditionType.xlUniqueValues,
"USD, EUR, CNY",
Type.Missing
);
 
Can you do the formatting manually using just Excel? If not, then there is no magic that Excel Interop is going to give you since all the Interop does is do what you would have clicked on or typed in.

I would recommend just having a premade template with macros and/or scripting in it than trying to do this via Interop.
 
Back
Top Bottom