Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 576839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:05:23+00:00 2026-05-13T14:05:23+00:00

Scenario is to generate an excel report that has ~ 150 data columns. Now

  • 0

Scenario is to generate an excel report that has ~ 150 data columns. Now I need to manage the column properties like Width, BackgroundColor, Font etc.

The approach that I am using relies on reflection. I have a class that has ~ 150 constants for column header text. Another custom attribute class to store column properties. These attributes are applied to the constants.

During column creation using reflection I am accessing all the constants to create the header text(Constant ordering in class defines column ordering) and the attribute for column properties.

private void CreateHeader(Excel.Worksheet xl_WorkSheet, FieldInfo[] fi_Header)
    {
        ColumnProperties c;
        System.Attribute[] customAttributes;
        for (int i = 0; i < fi_Header.GetLength(0); i++)
        {
            xl_WorkSheet.get_Range(xl_WorkSheet.Cells[1, i+1], xl_WorkSheet.Cells[2, i+1]).Merge(false);

            //Set the header text.
            xl_WorkSheet.get_Range(xl_WorkSheet.Cells[1, i + 1], xl_WorkSheet.Cells[2, i + 1]).FormulaR1C1 = 
                fi_Header[i].GetValue(null).ToString();
            //Set cell border.
            xl_WorkSheet.get_Range(xl_WorkSheet.Cells[1, i + 1],
                xl_WorkSheet.Cells[2, i + 1]).BorderAround(Excel.XlLineStyle.xlContinuous,
                Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);

            //Get custom attribute ~ Column attribute.
            customAttributes = (System.Attribute[])fi_Header[i].GetCustomAttributes(typeof(ColumnProperties), false);
            if (customAttributes.Length > 0)
            {
                c = (ColumnProperties)customAttributes[0];
                //Set column properties.
                xl_WorkSheet.get_Range(xl_WorkSheet.Cells[1, i + 1],
                    xl_WorkSheet.Cells[2, i + 1]).Interior.Color =
                    System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromName(c.Color));

                xl_WorkSheet.get_Range(xl_WorkSheet.Cells[1, i + 1],
                    xl_WorkSheet.Cells[2, i + 1]).ColumnWidth = c.Width;
            }                
        }
    }

EDIT: Code to get constants

private FieldInfo[] GetHeaderConstants(System.Type type)
    {
        ArrayList constants = new ArrayList();
        FieldInfo[] fieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
        foreach (FieldInfo fi in fieldInfos)
        {
            if (fi.IsLiteral && !fi.IsInitOnly)
                constants.Add(fi);
        }
        return (FieldInfo[])constants.ToArray(typeof(FieldInfo));
    }   

Main objective is to make the excel file generation generic/less maintainable. Is the approach fine or there are any other better alternatives.

EDIT 2: Constants class

public class ExcelHeaders
{
    [ColumnProperties(Width=10, Color="LemonChiffon")]
    public const string S_NO = "S.No";

    [ColumnProperties(Width = 20, Color = "WhiteSmoke")]
    public const string COLUMN_HEADER = "Header Text";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:05:23+00:00Added an answer on May 13, 2026 at 2:05 pm

    One characteristic of your approach is that you will need to change your source if you want to change a column’s appearance. I would prefer storing the appearance data in some kind of XML configuration. You could load the configuration from an external configuration file if available, else from a default configuration that’s embedded in the executable as a resource. This gives you the flexibility to change the configuration at runtime simply by adding a configuration file.

    Your XML document might look like:

      <Appearance>
        <!-- Defaults to use for attributes not explicitly specified -->
        <Defaults HeaderText="" Width="10" Color="White" />
        <Columns>
          <Column HeaderText="S.No" Width="10" Color="LemonChiffon" />
          <Column HeaderText="Header Text" Width="20" Color="WhiteSmoke" />
        </Columns>
      </Appearance>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 282k
  • Answers 282k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To the point, you need to set the encoding of… May 13, 2026 at 4:02 pm
  • Editorial Team
    Editorial Team added an answer There's a very similar question here. It doesn't look like… May 13, 2026 at 4:02 pm
  • Editorial Team
    Editorial Team added an answer The answer is to create an attached property. See: Creating… May 13, 2026 at 4:02 pm

Related Questions

Here's the scenario: I'm an SSIS virgin, but I found an excuse to start
I'm using the standard visitor pattern to iterate through a LINQ expression tree in
This is a strange LINQ-to-SQL problem which can't evaluate as an Enumerable (in SQL)
I'm looking for sort of a 'best practice' way to tackle this common scenario.
lets begin with the scenario: I have an ItemsControl inside a UserControl. In this

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.