public class Staff
{
[EpplusTableColumn(Order = 1, Header = "員工編號")]
[Required]
public string Id { get; set; }
[Column(1)]
[EpplusTableColumn(Order = 2, Header = "姓名")]
public string Name { get; set; }
//日期的話要設定 NumberFormat 才會顯示正確
[EpplusTableColumn(Order = 3, Header = "到值日", NumberFormat = "yyyy/MM/dd")]
public DateTime Create { get; set; }
[EpplusTableColumn(Order = 4, Header = "電話")]
public string Mobile { get; set; }
[EpplusTableColumn(Order = 5, Header = "薪水")]
public decimal Salary { get; set; }
[EpplusTableColumn(Hidden =true)]
public string Memo { get; set; }
}
接下來就是輸出 Excel 檔案的程式碼
var staffs = new List
{
new Staff{ Id = "S1", Name="DONMA", Mobile="0912456789", Salary=999_999m, Create = new DateTime(1950, 3, 15) },
new Staff{Id = "S2", Name="JOHN", Mobile="0922456789", Salary=199_999m, Create = new DateTime(1962, 6, 10)},
new Staff{ Id = "S1", Name="DELTA", Mobile="0912456799", Salary=299_999m, Create = new DateTime(1971, 10, 2)}
};
ExportListDataToExcel(staffs, "SHEET1", AppDomain.CurrentDomain.BaseDirectory + "sample2.xlsx");
private static void ExportListDataToExcel(List data,string sheetName,string filePath)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var package = new ExcelPackage(filePath))
{
// using the Actor class above
var sheet = package.Workbook.Worksheets.Add(sheetName);
sheet.Cells["A1"].LoadFromCollection(data,true);
package.Save();
}
}
Result:
有沒有異常的簡單,而且非常舒服不用在那邊數格子,如果有搞過輸出 Excel 一定知道我在說什麼
--
The bug existed in all possible states.
Until I ran the code.