locked
C# Excel Custom TableStyle RRS feed

  • Question

  • Question 1: How do you create a custom Excel.TableStyle in C#? I have been trying to find the answer for a while and I have been completely unsuccessful in my attempts.

    Question 2: When I try to change the border color of an Existing TableStyleElement, I am presented with an InvalidArg Error. The strange thing is that the following works: ->

    namedRange.Borders[Excel.XlBordersIndex.xlEdgeTop].Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(210, 30, 16));


    while this next segment causes the InvalidArg error: ->

    Excel.TableStyle style = (Excel.TableStyle)namedList.TableStyle;
    Excel.Border border = style.TableStyleElements[Excel.XlTableStyleElementType.xlHeaderRow].Borders[Excel.XlBordersIndex.xlEdgeBottom];
    border.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(210, 30, 16));


    I cannot for the life of me figure these two mysteries out.

    ______________________________________________________________

    EDIT: Okay. I figured out the first one. Tablestyles are associated to a workbook. And can be created from scratch with: ->

    namedWorkBook.TableStyles.Add(string TableStyleName);

    Also: As I have now determined, It is perfectly possible to work with the border colors on a custom TableStyle. So I would suspect, that you are simply not meant to make changes to the predefined Tablestyles in the Excel application. Not that the vague error message was any help in coming to that conclusion <.<

    Friday, January 18, 2013 12:26 PM

Answers

  • Okay. I figured out the first one. Tablestyles are associated to a workbook. And can be created from scratch with: ->
    namedWorkBook.TableStyles.Add(string TableStyleName);

    Also: As I have now determined, It is perfectly possible to work with the border colors on a custom TableStyle. So I would suspect, that you are simply not meant to make changes to the predefined Tablestyles in the Excel application. Not that the vague error message was any help in coming to that conclusion <.<

    Friday, January 18, 2013 1:09 PM

All replies

  • Okay. I figured out the first one. Tablestyles are associated to a workbook. And can be created from scratch with: ->
    namedWorkBook.TableStyles.Add(string TableStyleName);

    Also: As I have now determined, It is perfectly possible to work with the border colors on a custom TableStyle. So I would suspect, that you are simply not meant to make changes to the predefined Tablestyles in the Excel application. Not that the vague error message was any help in coming to that conclusion <.<

    Friday, January 18, 2013 1:09 PM
  • Good job, Jonatan!

    Glad to know you have solved this problem and thanks for sharing the solution. This is helpful for other developers in the forum who have interests in this problem.

    By the way, for Excel Development related question, please refer to:Excel for Developers Forum.

    Regards,



    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Lisa Zhu Monday, January 21, 2013 5:45 AM
    Monday, January 21, 2013 5:43 AM