.Net?Core?NPOI?導(dǎo)出多級(jí)表頭的實(shí)現(xiàn)代碼
想要導(dǎo)出這樣的表格
數(shù)據(jù)準(zhǔn)備格式
附上源碼
using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.SS.Util; using System.Data; using System.Text.RegularExpressions; namespace TestConsoleApp { /// <summary> /// 導(dǎo)出Excel /// </summary> public static class ExportHelper { public static void Export() { var dt = CreteTable(); var titles = GetExcelTitles(dt.Columns, out int maxTitleLevel); HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet("Sheet1"); var allRowCount = dt.Rows.Count + maxTitleLevel; //創(chuàng)建所有單元格 for (int i = 0; i < allRowCount; i++) { var row = sheet.CreateRow(i); for (int j = 0; j < dt.Columns.Count; j++) { row.CreateCell(j); } } //合并創(chuàng)建表頭 foreach (var tit in titles) { sheet.GetRow(tit.StartRow).GetCell(tit.StartColumn).SetCellValue(tit.Title); if (tit.MergeColumnCount + tit.MergeRowCount > 0) { sheet.AddMergedRegion(new CellRangeAddress(tit.StartRow, tit.StartRow + tit.MergeRowCount, tit.StartColumn, tit.StartColumn + tit.MergeColumnCount)); } } //生成數(shù)據(jù)行 for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { string cellValue = dt.Rows[i][j].ToString(); sheet.GetRow(maxTitleLevel + i).Cells[j].SetCellValue(cellValue); } } using FileStream stm = File.OpenWrite(@"D:\Drivers\Merge.xls"); workbook.Write(stm); } private static DataTable CreteTable() { DataTable dt = new DataTable(); dt.Columns.Add("編號(hào)"); dt.Columns.Add("收入-線上采購(gòu)-數(shù)量"); dt.Columns.Add("收入-線上采購(gòu)-金額"); dt.Columns.Add("收入-線下采購(gòu)-數(shù)量"); dt.Columns.Add("收入-線下采購(gòu)-金額"); dt.Columns.Add("回收-數(shù)量"); dt.Columns.Add("回收-金額"); dt.Columns.Add("支出-測(cè)試01-數(shù)量"); dt.Columns.Add("支出-測(cè)試01-金額"); dt.Columns.Add("支出-測(cè)試02-數(shù)量"); dt.Columns.Add("支出-測(cè)試02-金額"); dt.Columns.Add("其它-數(shù)量"); dt.Columns.Add("其它-金額"); dt.Columns.Add("備注"); for (int i = 1; i <= 100; i++) { var row = dt.NewRow(); row["編號(hào)"] = "編號(hào)" + i; row["收入-線上采購(gòu)-數(shù)量"] = i; row["收入-線上采購(gòu)-金額"] = i; row["收入-線下采購(gòu)-數(shù)量"] = i; row["收入-線下采購(gòu)-金額"] = i; row["回收-數(shù)量"] = i; row["回收-金額"] = i; row["支出-測(cè)試01-數(shù)量"] = i; row["支出-測(cè)試01-金額"] = i; row["支出-測(cè)試02-數(shù)量"] = i; row["支出-測(cè)試02-金額"] = i; row["其它-數(shù)量"] = i; row["其它-金額"] = i; row["備注"] = i; dt.Rows.Add(row); } return dt; } private static List<ExcelTitle> GetExcelTitles(DataColumnCollection columns, out int maxTitleLevel) { maxTitleLevel = 0; List<LevelExcelTitle> levelExcelTitles = new List<LevelExcelTitle>(); for (var index = 0; index < columns.Count; index++) { var column = columns[index].ToString(); var arr = column.Split("-"); if (maxTitleLevel < arr.Length) { maxTitleLevel = arr.Length; } for (int i = 0; i < arr.Length; i++) { levelExcelTitles.Add(new LevelExcelTitle() { Title = arr[i], LevelCode = string.Join("-", arr[..(i + 1)]), RowIndex = i, ColumnIndex = index, TotalLevel = arr.Length }); } } var titleLevel = maxTitleLevel; var excelTitles = levelExcelTitles .GroupBy(b => new { b.LevelCode, b.Title }) .Select(b => new ExcelTitle() { Title = b.Key.Title, StartRow = b.Min(c => c.RowIndex), MergeRowCount = b.Min(c => c.RowIndex) + 1 == b.Max(c => c.TotalLevel) ? titleLevel - b.Max(c => c.TotalLevel) : 0, StartColumn = b.Min(c => c.ColumnIndex), MergeColumnCount = b.Count() - 1,//排除自身 }).ToList(); return excelTitles; } } public class ExcelTitle { /// <summary> /// 標(biāo)題 /// </summary> public string Title { get; set; } /// <summary> /// 開始行 /// </summary> public int StartRow { get; set; } /// <summary> /// 合并行 /// </summary> public int MergeRowCount { get; set; } /// <summary> /// 開始列 /// </summary> public int StartColumn { get; set; } /// <summary> /// 合并列 /// </summary> public int MergeColumnCount { get; set; } } public class LevelExcelTitle { /// <summary> /// 標(biāo)題 /// </summary> public string Title { get; set; } public string LevelCode { get; set; } /// <summary> /// 第幾行 /// </summary> public int RowIndex { get; set; } /// <summary> /// 第幾列 /// </summary> public int ColumnIndex { get; set; } /// <summary> /// 總層 /// </summary> public int TotalLevel { get; set; } } }
到此這篇關(guān)于.Net Core NPOI 導(dǎo)出多級(jí)表頭的文章就介紹到這了,更多相關(guān).Net Core NPOI 導(dǎo)出多級(jí)表頭內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Asp.net靜態(tài)方法之Grid轉(zhuǎn)DataTable方法實(shí)現(xiàn)步驟
GridView綁定DataTable后,如何獲取GridView綁定后顯示的值,在項(xiàng)目需求的背景下寫了一個(gè)靜態(tài)方法,經(jīng)過在項(xiàng)目中的使用,bug的修復(fù),較為穩(wěn)定2013-04-04將FreeTextBox做成控件添加到工具箱中的具體操作方法
以下是對(duì)將FreeTextBox做成控件添加到工具箱中的具體操作方法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來(lái)參考下2013-09-09asp.net(c#)限制用戶輸入規(guī)定的字符和數(shù)字的代碼
這幾天在看到一個(gè)網(wǎng)站的注冊(cè)的時(shí)候,就只允許輸入規(guī)定的字符和數(shù)字。我就好奇的寫了一個(gè)校驗(yàn)的代碼。呵呵 不知道對(duì)大家有沒有用。如果有用的話可以保存。沒有用就當(dāng)是看看以下了。2010-10-10viewstate和datatable動(dòng)態(tài)錄入數(shù)據(jù)示例
這篇文章主要介紹了viewstate和datatable動(dòng)態(tài)錄入數(shù)據(jù)示例,需要的朋友可以參考下2014-02-02