Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.57/5 (4 votes)
See more:
Please take a look at the below code

C#
public ActionResult SampleDownload1()
        {
            var records = _getSampleInfoQuery.Execute();
                        var columns = new[]
                              {
                                  Column(x => x.College),
                                  Column(x => x.Amount, ExcelFormat.Money),
                                  Column(x => x.CreatedDate, ExcelFormat.Date, "Date"),
                                  Column(x => x.CreatedBy),
                                  Column(x => x.CreatedDate, ExcelFormat.Time, "Time"),
                                  Column(x => x.PercentageExample, ExcelFormat.Percent),
                              };

            return new ExcelFileResult<SampleInfo>(records) {ColumnDefinitions = columns};
        }

        private static ExcelColumnDefinition Column(Expression<Func<SampleInfo, object>> member, string format = null, string header = null)
        {
            return ExcelColumnDefinition.Create(member, format, header);
        }



My requirement is i want to assign values to Var columns dynamically, meaning i would like the user to specify the list of columns to be added to
C#
var columns;


I would probably be passing array of IDs of the chosen column like College,Amount,PercentageExample... My system has to add only those columns.

Note sure whether i am putting my question in a right way. Let me know for any clarifications.

Thanks for your precious time :)
Vishnu
Posted
Updated 8-Jan-14 8:13am
v4
Comments
Dave Kreskowiak 8-Jan-14 14:13pm    
Considering everyone here volunteers their time and you're not paying for support, nobody cares how "urgent" you think the problem is.

1 solution

Well, at the moment you are picking up your columns by directly allocating them as array arguments. If you are wanting to pick individual items, you're going to need to do something a little bit different - here's an example that you could adapt as you see fit:
C#
public ActionResult SampleDownload1(List<string> columnIds)
{
  var records = _getSampleInfoQuery.Execute();
  List<excelcolumndefinition> columns = new List<excelcolumndefinitions>();
  foreach (string columnName in columnIds)
  {
    switch (columnName)
    {
      case "College":
        columns.Add(Column(x => x.College));
        break;
      case "Amount":
        columns.Add(Column(x => x.Amount, ExcelFormat.Money));
        break;
        // And so on....
    }
  }
  return new ExcelFileResult<sampleinfo>(records) {ColumnDefinitions = columns.ToArray()};
}
Obviously this is a fairly naive, simplistic approach, but it should serve to give you some ideas.
 
Share this answer
 
v2
Comments
Maciej Los 8-Jan-14 15:00pm    
+5!
vishnutejcj 8-Jan-14 21:38pm    
Thanks a lot :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900