.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>
/// 開(kāi)始行
/// </summary>
public int StartRow { get; set; }
/// <summary>
/// 合并行
/// </summary>
public int MergeRowCount { get; set; }
/// <summary>
/// 開(kāi)始列
/// </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 Execl的添加,更新操作實(shí)現(xiàn)代碼
asp.net Execl的添加、修改等實(shí)現(xiàn)代碼。2009-02-02
asp.net連接數(shù)據(jù)庫(kù)讀取數(shù)據(jù)示例分享
這篇文章主要介紹了asp.net連接數(shù)據(jù)庫(kù)讀取數(shù)據(jù)示例,大家參考使用吧2014-01-01
未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例 (System.NullReferenceException)
System.NullReferenceException:未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例,這是一個(gè)新鳥(niǎo),中鳥(niǎo),老鳥(niǎo)都避不開(kāi)的錯(cuò)誤2012-03-03
如何為asp.net網(wǎng)站項(xiàng)目添加子項(xiàng)目
最近要給公司的電子商務(wù)網(wǎng)站添加個(gè)圈子的功能.網(wǎng)站功能本來(lái)就包含有新聞發(fā)布,會(huì)員管理,商品購(gòu)物,后臺(tái)管理等,現(xiàn)在又要再加上圈子的功能,網(wǎng)站項(xiàng)目越來(lái)越復(fù)雜,每次編譯生成的dll超過(guò)100k.這樣每次修改任何一個(gè)功能中的任何一個(gè)小問(wèn)題都要上傳整個(gè)dll,并導(dǎo)致整個(gè)website的首次訪問(wèn)的重新編譯.2008-10-10

