This project has moved and is read-only. For the latest updates, please go here.

Expando Objects

Apr 8, 2013 at 11:52 AM
Hi

Could you please guide how we can use the expando object as source of the report. Secondly how we can control the header text to be picked from the resource file.

Regards
May 9, 2013 at 6:07 PM
Here's an example use case. This would write the results of a run-time specified sql query to an excel file. There are some declarations missing here but I think this should give you the general idea of how to use the expando object as your IReportSource.
            var expandoList = new List<ExpandoObject>();

            using (var conn = new SqlConnection(ConnectionString))
            using (var command = new SqlCommand(queryString, conn))
            {
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    dynamic expando = new ExpandoObject();
                    for (int i = 0; i < reader.FieldCount; ++i)
                    {
                        ((IDictionary<string, Object>)expando)[reader.GetName(i)] = reader[i];
                    }
                    expandoList.Add(expando);
                }
            }

            var report = new Report(expandoList .ToReportSource());
            report.TextFields.Title = "Test Report";
            var writer = new ExcelReportWriter();

            string path = @"C:\test.xls";
            using (FileStream fs = File.Create(path))
            {
                writer.WriteReport(report, fs);
            }