How to evaluate an Excel format string programmatically?
I am programmatically creating Excel cells based on data in a certain format, e.g xml in
some of the the cells; there are format strings that need to be applied to them.
e.g “[Red] #,##0.00” for 987564.63635
before filling into the cell, I want to check (basically only colors) for the cell or text.
In case it is won’t display well to the user, e.g light yellow is very bad, a user can hardly see this color.
So it comes to before creating the cell, I need to know the color from the format string
e.g “[Red] #,##0.00” or “[Color 32] dd/mm/yyyy” or an even more complex Excel format string.
I want to know. Is there some kind of library that can do this job?
or does .net have this kind of function already?
or i have to do it by myself?
Sample
e.g
if format string is “[Red] #,##0,00”,
i want to know the corresponding Sysmte.draw.Color for the [Red] base on the string.
or the corresponding web color, ff0000, for [Red] base on the string
or corresponding RGB values
You can use the Excel range object to access all the cell’s configuration setup.
for example:
r.Value2 holds the underline value of the cell (the formula).
r.Text holds the visible (user) value of the cell (the result).
r.NumberFormat holds the format string – in your example it will hold the “[Red] #,##0.00”.
From there you’ll need to use any string manipulation techniques on the NumberFormat member (i recommend a regular expression) to find the color you are looking for or any other thing.
EDIT, here is some usage of regExp and TypeCovertor to accomplish the string to Color object conversion. (with a continue to the above source)
The regular expression object is compiled to the assembly for performance. I assume using the
Color resultColorobject is something you can figure yourself (it has ARGB and other members to examine).And last comment, you can easily reduce the number of lines in this example, i just wanted to emphasize each step on the way.