前面一篇詳細講解了導入導出,本節演示混合結構的導出功能!同時提供代碼下載..
先看效果圖:這個一個混合的Excel,列表與自定義信息的混合!git
建立一個新的Asp.Net MVC5項目,並從Nuget包安裝ClosedXMLgithub
public class SchoolClass { public string Id { get; set; }//班級標示 public string Name { get; set; }//班級名稱 public string Manager { get; set; }//班主任姓名 public string Manager2 { get; set; }//副班主任姓名 public string PhoneNumber { get; set; }//班主任聯繫電話 public string PhoneNumber2 { get; set; }//副主任聯繫電話 public string Remark { get; set; } //班級說明 public List<Students> stuList { get; set; }//一個班級對應多個學生 } public class Students { public string Id { get; set; }//學號 public string Name { get; set; }//姓名 public string Sex { get; set; }//性別 public string Age { get; set; }//年齡 public string Point { get; set; }//年度得分 public string PhoneNumber { get; set; }//電話 }
注:從模型看出,這是一個班級信息表,一個班級對應多個學生的信息!數據庫
public ActionResult Index() { //模擬數據庫賦值,一個班級對應多個學生 SchoolClass model = new SchoolClass(); model.Id = "CLS0001"; model.Name = "三年二班"; model.Manager = "黃SIR"; model.PhoneNumber = "13800138000"; model.Manager2 = "李SIR"; model.PhoneNumber2 = "13888138666"; model.Remark = "這是一段有不少個字的班級說明,只有足夠長的字,才能證實這段文字很長,若是100個字還不夠長,那麼就再來100個字!"; model.stuList = new List<Students>(); model.stuList.Add(new Students() { Id = "STU0001", Name = "牛掰掰", Sex = "男", Age = "23",Point="80", PhoneNumber = "13545678547" }); model.stuList.Add(new Students() { Id = "STU0002", Name = "張三", Sex = "女", Age = "23", Point = "70", PhoneNumber = "13545654874" }); model.stuList.Add(new Students() { Id = "STU0003", Name = "李四", Sex = "女", Age = "25", Point = "50", PhoneNumber = "13545633552" }); model.stuList.Add(new Students() { Id = "STU0004", Name = "王五", Sex = "男", Age = "22", Point = "66", PhoneNumber = "13566885541" }); model.stuList.Add(new Students() { Id = "STU0005", Name = "林蛋大", Sex = "男", Age = "26", Point = "95", PhoneNumber = "13821298458" }); model.stuList.Add(new Students() { Id = "STU0006", Name = "劉麗麗", Sex = "女", Age = "19", Point = "95", PhoneNumber = "13821298458" }); }
紅框中表示表示表頭位置,咱們Excel你們都知道是按位置標示的,好比我點中的三年二班表示的是B3(左上角顯示),全部咱們填充表頭也是這麼作數組
var wb = new XLWorkbook(); var ws = wb.Worksheets.Add("班級"); ws.Cell("A1").Value = model.Name+"班級信息表"; //標題 ws.Cell("A2").Value = "班級代號"; ws.Cell("B2").Value = "班級名稱"; ws.Cell("C2").Value = "班主任"; ws.Cell("D2").Value = "聯繫電話"; ws.Cell("E2").Value = "副班主任"; ws.Cell("F2").Value = "聯繫電話"; //主表內容 ws.Cell("A3").Value = model.Id; ws.Cell("B3").Value = model.Name; ws.Cell("C3").Value = model.Manager; ws.Cell("D3").Value = model.PhoneNumber; ws.Cell("E3").Value = model.Manager2; ws.Cell("F3").Value = model.PhoneNumber2; ws.Cell("A4").Value = model.Remark;//說明 //明細表標題 ws.Cell("A5").Value = "學號"; ws.Cell("B5").Value = "姓名"; ws.Cell("C5").Value = "性別"; ws.Cell("D5").Value = "年齡"; ws.Cell("E5").Value = "得分"; ws.Cell("F5").Value = "電話號碼";
因此位置決定內容,填充表頭後。咱們再填充列表ide
for (int i = 0; i < model.stuList.Count(); i++) { ws.Cell(i + 6, 1).Value = model.stuList[i].Id; ws.Cell(i + 6, 2).Value = model.stuList[i].Name; ws.Cell(i + 6, 3).Value = model.stuList[i].Sex; ws.Cell(i + 6, 4).Value = model.stuList[i].Age; ws.Cell(i + 6, 5).Value = model.stuList[i].PhoneNumber; ws.Cell(i + 6, 6).Value = model.stuList[i].PhoneNumber; }
因爲咱們列表數據是動態並非固定的,工做簿也是支持二維數組位置填充!看代碼從第6行開始,一行一行填充!spa
var rngTable = ws.Range("A1:F" + (model.stuList.Count() + 5)); //合併表頭 var rngHeaders = rngTable.Range("A1:F1"); ws.Row(1).Height = 20; rngHeaders.FirstCell().Style .Font.SetBold() .Fill.SetBackgroundColor(XLColor.Buff) .Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center); rngHeaders.FirstRow().Merge(); //第二行表頭樣式 rngHeaders = rngTable.Range("A2:F2"); // The address is relative to rngTable (NOT the worksheet) rngHeaders.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; rngHeaders.Style.Font.Bold = true; //rngHeaders.Style.Font.FontColor = XLColor.DarkBlue; rngHeaders.Style.Fill.BackgroundColor = XLColor.Aqua; //合併說明 var rngRemark = rngTable.Range("A4:F4"); ws.Row(4).Height = 30; rngRemark.Style.Alignment.WrapText = true; rngRemark.FirstCell().Comment.Style.Size.SetAutomaticSize(); rngRemark.FirstRow().Merge(); rngTable = ws.Range("A5:F" + (model.stuList.Count() + 5)); var excelTable = rngTable.CreateTable(); ws.Columns().AdjustToContents();
最後咱們要作:填充顏色,合併單元格就好,好比說明:說明內容咱們是填充在A4,可是咱們要合併A1-F4的位置excel
public ActionResult Index() { //模擬數據庫賦值,一個班級對應多個學生 SchoolClass model = new SchoolClass(); model.Id = "CLS0001"; model.Name = "三年二班"; model.Manager = "黃SIR"; model.PhoneNumber = "13800138000"; model.Manager2 = "李SIR"; model.PhoneNumber2 = "13888138666"; model.Remark = "這是一段有不少個字的班級說明,只有足夠長的字,才能證實這段文字很長,若是100個字還不夠長,那麼就再來100個字!"; model.stuList = new List<Students>(); model.stuList.Add(new Students() { Id = "STU0001", Name = "牛掰掰", Sex = "男", Age = "23",Point="80", PhoneNumber = "13545678547" }); model.stuList.Add(new Students() { Id = "STU0002", Name = "張三", Sex = "女", Age = "23", Point = "70", PhoneNumber = "13545654874" }); model.stuList.Add(new Students() { Id = "STU0003", Name = "李四", Sex = "女", Age = "25", Point = "50", PhoneNumber = "13545633552" }); model.stuList.Add(new Students() { Id = "STU0004", Name = "王五", Sex = "男", Age = "22", Point = "66", PhoneNumber = "13566885541" }); model.stuList.Add(new Students() { Id = "STU0005", Name = "林蛋大", Sex = "男", Age = "26", Point = "95", PhoneNumber = "13821298458" }); model.stuList.Add(new Students() { Id = "STU0006", Name = "劉麗麗", Sex = "女", Age = "19", Point = "95", PhoneNumber = "13821298458" }); var wb = new XLWorkbook(); var ws = wb.Worksheets.Add("班級"); ws.Cell("A1").Value = model.Name+"班級信息表"; //標題 ws.Cell("A2").Value = "班級代號"; ws.Cell("B2").Value = "班級名稱"; ws.Cell("C2").Value = "班主任"; ws.Cell("D2").Value = "聯繫電話"; ws.Cell("E2").Value = "副班主任"; ws.Cell("F2").Value = "聯繫電話"; //主表內容 ws.Cell("A3").Value = model.Id; ws.Cell("B3").Value = model.Name; ws.Cell("C3").Value = model.Manager; ws.Cell("D3").Value = model.PhoneNumber; ws.Cell("E3").Value = model.Manager2; ws.Cell("F3").Value = model.PhoneNumber2; ws.Cell("A4").Value = model.Remark;//說明 //明細表標題 ws.Cell("A5").Value = "學號"; ws.Cell("B5").Value = "姓名"; ws.Cell("C5").Value = "性別"; ws.Cell("D5").Value = "年齡"; ws.Cell("E5").Value = "得分"; ws.Cell("F5").Value = "電話號碼"; for (int i = 0; i < model.stuList.Count(); i++) { ws.Cell(i + 6, 1).Value = model.stuList[i].Id; ws.Cell(i + 6, 2).Value = model.stuList[i].Name; ws.Cell(i + 6, 3).Value = model.stuList[i].Sex; ws.Cell(i + 6, 4).Value = model.stuList[i].Age; ws.Cell(i + 6, 5).Value = model.stuList[i].PhoneNumber; ws.Cell(i + 6, 6).Value = model.stuList[i].PhoneNumber; } var rngTable = ws.Range("A1:F" + (model.stuList.Count() + 5)); //合併表頭 var rngHeaders = rngTable.Range("A1:F1"); ws.Row(1).Height = 20; rngHeaders.FirstCell().Style .Font.SetBold() .Fill.SetBackgroundColor(XLColor.Buff) .Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center); rngHeaders.FirstRow().Merge(); //第二行表頭樣式 rngHeaders = rngTable.Range("A2:F2"); // The address is relative to rngTable (NOT the worksheet) rngHeaders.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; rngHeaders.Style.Font.Bold = true; //rngHeaders.Style.Font.FontColor = XLColor.DarkBlue; rngHeaders.Style.Fill.BackgroundColor = XLColor.Aqua; //合併說明 var rngRemark = rngTable.Range("A4:F4"); ws.Row(4).Height = 30; rngRemark.Style.Alignment.WrapText = true; rngRemark.FirstCell().Comment.Style.Size.SetAutomaticSize(); rngRemark.FirstRow().Merge(); rngTable = ws.Range("A5:F" + (model.stuList.Count() + 5)); var excelTable = rngTable.CreateTable(); ws.Columns().AdjustToContents(); var exportFileName = string.Concat( "ExcelSample", DateTime.Now.ToString("yyyyMMddHHmmss"), ".xlsx"); return new ExportExcelResult(wb) { SheetName = "人員列表", FileName = exportFileName, Workbook = wb }; }
代碼不難看懂,基本情景我都用到:好比合並、高度、自動換行、等等,是一篇值得參考的文章,同時也見證ClosedXML的強大code
參考代碼:https://github.com/closedxml/closedxmlxml