Created
August 8, 2019 08:13
-
-
Save tocalai/35a14f57a0f74e316cef173bc32c0964 to your computer and use it in GitHub Desktop.
Demonstrate how to export data to excel file through ClosedXML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ClosedXML.Excel; | |
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
namespace Demo.Excel.Export | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// generate fake data from the help of Bogus | |
var users = GenerateFakeMembers(1000); | |
// generate file: contact.xlxs | |
GenerateContactSheet(users); | |
} | |
private List<Member> GenerateFakeMembers(int amount) | |
{ | |
var faker = new Faker<Member>("en"); | |
// rule for create field data | |
// ... | |
var users = faker.Generate(amount); | |
return users; | |
} | |
private static void GenerateContactSheet(List<Member> users) | |
{ | |
var wb = new XLWorkbook(); | |
var saveAsPath = Path.Combine($@"{AppDomain.CurrentDomain.BaseDirectory}\excels\", @"contact.xlsx"); | |
// set sheet name | |
var worksheet = wb.Worksheets.Add("Contact List"); | |
// insert for the header from beginning | |
users.Insert(0, new Member | |
{ | |
FullName = nameof(Member.FullName), | |
Sex = nameof(Member.Sex), | |
Birthday = nameof(Member.Birthday), | |
Phone = nameof(Member.Phone), | |
Type = nameof(Member.Type) | |
}); | |
// insert all data to sheet | |
worksheet.Cell(1, 1).InsertData(users.Select(x => new | |
{ | |
x.FullName, | |
x.Sex, | |
x.Birthday, | |
x.Phone, | |
x.Type | |
})); | |
// stying the worksheet | |
// stying header | |
worksheet.RangeUsed().FirstRowUsed().Style.Font.FontColor = XLColor.White; | |
worksheet.RangeUsed().FirstRowUsed().Style.Fill.BackgroundColor = XLColor.FromArgb(79, 129, 189); // Blue, Accent 1 | |
worksheet.RangeUsed().FirstRowUsed().Style.Font.Bold = true; | |
// stying font | |
worksheet.RangeUsed().Style.Font.FontName = "Calibri"; | |
// stying alternate row colors except header row | |
worksheet.RangeUsed().AddConditionalFormat() | |
.WhenIsTrue("=MOD((ROW() + 1),2)=1").Fill.BackgroundColor = XLColor.FromArgb(217, 228, 240); // Blue, Accent 1, Lighter 80% | |
// set filter | |
worksheet.RangeUsed().SetAutoFilter(); | |
// adjust contents width and height | |
worksheet.Rows().AdjustToContents(); | |
worksheet.Columns().AdjustToContents(); | |
// save to file | |
wb.SaveAs(saveAsPath); | |
} | |
} | |
public class Member | |
{ | |
public Guid UserId { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public DateTime? DateOfBirth { get; set; } = null; | |
public string Phone { get; set; } | |
public DataSets.Name.Gender? Gender { get; set; } = null; | |
public string Type { get; set; } | |
// FirstName + "" LastName | |
public string FullName { get; set; } | |
public string Sex | |
{ | |
get => Gender.HasValue ? Gender.Value.ToString() : nameof(this.Sex); | |
set { } | |
} | |
public string Birthday | |
{ | |
get => DateOfBirth.HasValue ? DateOfBirth.Value.ToString("yyyy-MM-dd") : nameof(this.Birthday); | |
set { } | |
} | |
} | |
public enum ContactType | |
{ | |
Personal, | |
Business, | |
Faimly, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment