[英]Repeater data to excel using Epplus?
I am using Epplus to write datatable to excel file which works fine. 我正在使用Epplus将数据表写入excel文件,效果很好。 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());
}
Repeater class has a property Data
, so you have to assign your data table to it. Repeater类具有
Data
属性,因此必须为其分配数据表。 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? 关于为什么需要
DataBind
的信息,您可以阅读: 为什么需要DataBind()方法?
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? 如果是这样,为什么不使用
LoadFromCollection
? 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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.