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

  • Home
  • SEARCH
  • 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

Related Questions

The scenario: we have a web system that automatically generates office 2003 excel files
I have a scenario that requires me to generate a printed page with encrypted
This is my scenario... I have a winForm tabControl that has a variety of
Scenario is such: I have a webapp that I'd like to run dynamically with
I need to generate a few small text files that will be used as
I would like to generate reports in pdf format with following scenario: people would
I am going to use SSRS to generate report for my website. Scenario: I
Scenario You have an Assembly for Data Transfer Objects containing 10 classes that exactly
I have the following scenario: My page has a dropdown list that allows user
Let's consider the following scenario: a function which can generate code colors from white

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.