1

I am using Epplus to write datatable to excel file which works fine. How can i do this for repeater ?

        using (ExcelPackage pck = new ExcelPackage())
        {
            ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");
            ws.Cells["A1"].LoadFromDataTable(dt, true);
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;  filename=ExcelDemo.xlsx");
            Response.BinaryWrite(pck.GetAsByteArray());
        }
siddhipur
  • 121
  • 1
  • 13

2 Answers2

1

Repeater class has a property Data, so you have to assign your data table to it. The code could look like following

    repeater.DataSource = dt; //dt variable is same as in your example
    repeater.DataBind();

About why DataBind is necessary you can read in: Why is the DataBind() method necessary?

Community
  • 1
  • 1
Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
  • Acutally, i am getting datatable and then manipulating it to a different format by storing them into class object and then binding that item to the repeater which is why i am not able to use "LoadFromDatatable" option of Epplus. – siddhipur Apr 30 '15 at 04:09
  • @siddhipur so why example in your question use this function? – Piotr Stapp Apr 30 '15 at 04:11
  • Anyway if it class of objects look here http://stackoverflow.com/questions/674204/binding-a-generic-list-to-a-repeater-asp-net – Piotr Stapp Apr 30 '15 at 04:12
1

When you say into "class object" I assume you mean a collection of objects? Which would make sense if you are binding to a repeater. If so, why not use LoadFromCollection instead? Like this:

[TestMethod]
public void LoadFromCollection_Test()
{
    //http://stackoverflow.com/questions/29958463/repeater-data-to-excel-using-epplus
    //Throw in some data
    var dataSet = new List<RowObject>();
    for (var a = 0; a < 20; a++)
        dataSet.Add(new RowObject
        {
            ColumnString = "Row " + a,
            ColumnDateTime = DateTime.Now.AddHours(a)
        });

    //Clear the file
    var newFile = new FileInfo(@"C:\Temp\Temp.xlsx");
    if (newFile.Exists)
        newFile.Delete();

    var i = 0;
    using (var package = new ExcelPackage(newFile))
    {
        var ws = package.Workbook.Worksheets.Add("Sheet1");
        ws.Cells["A1"].LoadFromCollection(dataSet, true);

        package.Save();
    }
}

public class RowObject
{
    public string ColumnString { get; set; }
    public DateTime ColumnDateTime { get; set; }
}
Ernie S
  • 13,902
  • 4
  • 52
  • 79