Dynamic Column Excel export and Dynamic Column excel Export
/// <Summary>
/// The general attributes of exported data must be marked in the attributes
/// </Summary>
[AttributeUsage (AttributeTargets. Class | AttributeTargets. Parameter | AttributeTargets. Property)]
Public class ExportToExcelAttribute: Attribute
{
Private string _ columnName;
Private int _ width = 19;
Public ExportToExcelAttribute (string columnName)
{
This. _ columnName = columnName;
}
Public ExportToExcelAttribute (string columnName, int columnWidth)
{
This. _ columnName = columnName;
This. _ width = columnWidth;
}
Public string ColumnName {get {return _ columnName ;}}
Public int ColumnWidth {get {return _ width ;}}
}
Public class ClassA
{
Int id = 0;
String name = string. Empty;
[ExportToExcel ("ID", 25)]
Public int Id
{
Get {return Id ;}
Set {Id = value ;}
}
[ExportToExcel ("Name", 25)]
Public int Id
{
Get {return Id ;}
Set {Id = value ;}
}
}
Public class ExcelHelper
{
Public void CreateReportToExcel ()
{
Var data = new List <ClassA> ()
{
New ClassA () {Id = 1, Name = "Zhang San"}, new ClassA () {Id = 2, Name = "Zhang Si "}
};
Var expressList = new List <Expression <Func <ClassA, object >>() {m => m. Id}, {m => m. Name }};
Aspose. Cells. Workbook workbook = GetExportWorkbook <ClassA> (expressList, data, "ExcelSheet1 ");
Response. BinaryWrite (workbook. SaveToStream (). ToArray ());
Response. appendHeader ("Content-Disposition", "attachment; filename = \" "+ HttpUtility. urlEncode ("" + DateTime. now. toString ("yyyyMMddHHmmss") + ". xls ", System. text. encoding. UTF8) + "\"");
Response. ContentType = "application/ms-excel ";
}
/// <Summary>
/// General method for exporting data to excel
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "expressList"> </param>
/// <Param name = "datas"> </param>
/// <Param name = "WorksheetName"> </param>
/// <Returns> </returns>
Public static Workbook GetExportWorkbook <T> (List <Expression <Func <T, object> expressList, List <T> datas, string WorksheetName)
{
List <PropertyInfo> proList = new List <PropertyInfo> (); // a set of field attributes
PropertyInfo pro;
ExportToExcelAttribute att;
Workbook work = new Workbook ();
Work. Worksheets. Clear ();
Worksheet sheet = null;
If (datas. Count> 0)
{
Work. Worksheets. Add (WorksheetName );
Sheet = work. Worksheets [0];
Sheet. Cells. SetRowHeight (0, 30 );
Int I = 0;
Foreach (var item in expressList)
{
Pro = GetProperty (item );
ProList. Add (pro );
Att = pro. getcustomattriatt <ExportToExcelAttribute> ();
Sheet. Cells [0, I ++]. PutValue (att. ColumnName );
Sheet. Cells. SetColumnWidth (I-1, att. ColumnWidth );
}
// Cell
Cells cells = sheet. Cells;
Style style1 = work. Styles [work. Styles. Add ()];
Style1.HorizontalAlignment = TextAlignmentType. Center;
Style1.Font. Name = "";
Style1.Font. Size = 11;
Style1.IsLocked = true;
Style1.Font. IsBold = true;
Int n = 1;
Foreach (T item in datas)
{
Int j = 0;
Foreach (var p in proList)
{
Sheet. Cells [n, j ++]. PutValue (p. GetValue (item ));
}
N + = 1;
}
}
Return work;
}
}