Dynamic Column Excel export and Dynamic Column excel Export

Source: Internet
Author: User

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;

}

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.