Sandy Gifford
Sandy Gifford

Reputation: 8136

Getting Excel Border Styles in C#

Excel border styles in C# are well explored across the internet (here on Stack as much as anywhere) but I've been totally unable to find any documentation covering getting style values; every question, page, and PDF I've found only discusses setting.

How does one get style data from the excel borders in a useful form? (Say, String)

The problem, I'm sure, has something to do with the fact that I'm an absolute C# novice. Dynamic types confuse and scare me (I miss Java) and this problem may very well center around that. I'm trying to retrieve various styles from ranges in Excel (the ranges may be cells, rows, or entire tables) and translate them into some other form (for demonstration purposes, I've rewritten my code to write HTML styles inline as a String).

using Excel = Microsoft.Office.Interop.Excel;

public static String rangeStyle2InlineHTMLStyle(Excel.Range range)
{
    String str = "";
    Excel.Style style = range.Style;

    Excel.Border border_top = style.Borders[Excel.XlBordersIndex.xlEdgeTop];
    Excel.Border border_left = style.Borders[Excel.XlBordersIndex.xlEdgeLeft];
    Excel.Border border_right = style.Borders[Excel.XlBordersIndex.xlEdgeRight];
    Excel.Border border_bottom = style.Borders[Excel.XlBordersIndex.xlEdgeBottom];

    Console.WriteLine(border_top.Color.ToString());

    str += "border-top-color:" + color2CSSRGB(border_top.Color) + "; ";
    str += "border-left-color:" + color2CSSRGB(border_left.Color) + "; ";
    str += "border-right-color:" + color2CSSRGB(border_right.Color) + "; ";
    str += "border-bottom-color:" + color2CSSRGB(border_bottom.Color) + "; ";

    return "style='" + str + "'";
}

public static String color2CSSRGB(Object c)
{
    return "rgb(" + c.R + "," + c.G + "," + c.B + ")";
}

Everything underneath Excel.Style seems to be of dynamic type, and I have no clue how to use that. MSDN documentation is wonderfully obtuse: There's no clear indication what the members of the Color property are, or what sort of object type I can expect it to return.

I guess this is intentional, that way any one of a number of different color types can be used and even returned, but it's not immediately useful when all I want to know is what's going on in border x... Then again, it's just as likely that this is all entirely the wrong approach.

Thanks

Upvotes: 0

Views: 3100

Answers (1)

chancea
chancea

Reputation: 5958

I will completely redo my answer based on the new information as I misunderstood the question earlier:

Although I do not understand the actual problem as to why you need to convert the styles to strings, still you can make multiple methods that each take in an Excel.Border and then return the corresponding string values you desire

For getting the color

you can have something like

    public static String getBorderColor(Excel.Border border)
    {
        String retval = "";
        retval += " rgb(";
        System.Drawing.Color color = Color.FromArgb((int)border.Color);
        retval += color.R + ",";
        retval += color.G + ",";
        retval += color.B + ")";
        return retval;
    }

For getting the weight since all line styles and weight styles are just basically integers and so they will not convert to strings as you want them. They would appear as their assigned numeric value as such you are going to have to use either if statements or switch case statements and traverse through all the styles... so something like:

    public static String getWeight(Excel.Border border)
    {
        String retval = "weight = ";
        int weight = border.Weight;

        const int xlThick = (int)Excel.XlBorderWeight.xlThick;

        switch (weight)
        {
            case xlThick:
                retval += "thick";
                break;
            //... continue for all border weights
        }

        return retval;

    }

you can repeat that for line styles and such... Hopefully that helps you

Upvotes: 1

Related Questions