.Net?Core?NPOI?導(dǎo)出多級表頭的實現(xiàn)代碼
更新時間:2024年11月11日 09:19:51 作者:我想火火火
這篇文章介紹了如何使用.NetCore和NPOI庫導(dǎo)出多級表頭的表格數(shù)據(jù),并附上了源碼,感興趣的朋友一起看看吧
想要導(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("編號");
dt.Columns.Add("收入-線上采購-數(shù)量");
dt.Columns.Add("收入-線上采購-金額");
dt.Columns.Add("收入-線下采購-數(shù)量");
dt.Columns.Add("收入-線下采購-金額");
dt.Columns.Add("回收-數(shù)量");
dt.Columns.Add("回收-金額");
dt.Columns.Add("支出-測試01-數(shù)量");
dt.Columns.Add("支出-測試01-金額");
dt.Columns.Add("支出-測試02-數(shù)量");
dt.Columns.Add("支出-測試02-金額");
dt.Columns.Add("其它-數(shù)量");
dt.Columns.Add("其它-金額");
dt.Columns.Add("備注");
for (int i = 1; i <= 100; i++)
{
var row = dt.NewRow();
row["編號"] = "編號" + i;
row["收入-線上采購-數(shù)量"] = i;
row["收入-線上采購-金額"] = i;
row["收入-線下采購-數(shù)量"] = i;
row["收入-線下采購-金額"] = i;
row["回收-數(shù)量"] = i;
row["回收-金額"] = i;
row["支出-測試01-數(shù)量"] = i;
row["支出-測試01-金額"] = i;
row["支出-測試02-數(shù)量"] = i;
row["支出-測試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)出多級表頭的文章就介紹到這了,更多相關(guān).Net Core NPOI 導(dǎo)出多級表頭內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Asp.net靜態(tài)方法之Grid轉(zhuǎn)DataTable方法實現(xiàn)步驟
GridView綁定DataTable后,如何獲取GridView綁定后顯示的值,在項目需求的背景下寫了一個靜態(tài)方法,經(jīng)過在項目中的使用,bug的修復(fù),較為穩(wěn)定2013-04-04
將FreeTextBox做成控件添加到工具箱中的具體操作方法
以下是對將FreeTextBox做成控件添加到工具箱中的具體操作方法進行了詳細的介紹,需要的朋友可以過來參考下2013-09-09
asp.net(c#)限制用戶輸入規(guī)定的字符和數(shù)字的代碼
這幾天在看到一個網(wǎng)站的注冊的時候,就只允許輸入規(guī)定的字符和數(shù)字。我就好奇的寫了一個校驗的代碼。呵呵 不知道對大家有沒有用。如果有用的話可以保存。沒有用就當(dāng)是看看以下了。2010-10-10
viewstate和datatable動態(tài)錄入數(shù)據(jù)示例
這篇文章主要介紹了viewstate和datatable動態(tài)錄入數(shù)據(jù)示例,需要的朋友可以參考下2014-02-02

