Skip to content

Commit aa54365

Browse files
committed
fix: 优化扩展方法
1 parent ff3c4c1 commit aa54365

File tree

5 files changed

+137
-8
lines changed

5 files changed

+137
-8
lines changed

src/Bing.Offices.Npoi/Extensions/FontExtensions.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace Bing.Offices.Npoi.Extensions
77
/// </summary>
88
public static class FontExtensions
99
{
10+
#region SetFontHeightInPoints(设置字体大小)
11+
1012
/// <summary>
1113
/// 设置字体大小
1214
/// </summary>
@@ -18,6 +20,10 @@ public static IFont SetFontHeightInPoints(this IFont font, short fontSize)
1820
return font;
1921
}
2022

23+
#endregion
24+
25+
#region SetColor(设置字体颜色)
26+
2127
/// <summary>
2228
/// 设置字体颜色
2329
/// </summary>
@@ -29,15 +35,36 @@ public static IFont SetColor(this IFont font, short color)
2935
return font;
3036
}
3137

38+
#endregion
39+
40+
#region SetBoldWeight(设置粗体)
41+
3242
/// <summary>
3343
/// 设置粗体
3444
/// </summary>
3545
/// <param name="font">字体</param>
36-
/// <param name="boldweight">粗体大小</param>
37-
public static IFont SetBoldWeight(this IFont font, short boldweight)
46+
/// <param name="boldWeight">粗体大小</param>
47+
public static IFont SetBoldWeight(this IFont font, short boldWeight)
3848
{
39-
font.Boldweight = boldweight;
49+
font.Boldweight = boldWeight;
4050
return font;
4151
}
52+
53+
#endregion
54+
55+
#region DefaultFont(默认字体)
56+
57+
/// <summary>
58+
/// 默认字体
59+
/// </summary>
60+
/// <param name="font">字体</param>
61+
public static IFont DefaultFont(this IFont font)
62+
{
63+
font.FontName = "宋体";
64+
font.FontHeightInPoints = 9;
65+
return font;
66+
}
67+
68+
#endregion
4269
}
4370
}

src/Bing.Offices.Npoi/Extensions/RowExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,21 @@ public static IRow ClearContent(this IRow row)
4545
/// </summary>
4646
/// <param name="row">行</param>
4747
public static bool IsEmptyRow(this IRow row) => row == null || row.Cells.All(x => string.IsNullOrWhiteSpace(x?.GetStringValue()));
48+
49+
/// <summary>
50+
/// 设置单元格值
51+
/// </summary>
52+
/// <typeparam name="T">数据类型</typeparam>
53+
/// <param name="row">行</param>
54+
/// <param name="column">单元格索引</param>
55+
/// <param name="value">值</param>
56+
/// <param name="style">行样式</param>
57+
public static void Value<T>(this IRow row, int column, T value, ICellStyle style = null)
58+
{
59+
var cell = row.CreateCell(column);
60+
cell.SetValue(value);
61+
if (style != null)
62+
cell.CellStyle = style;
63+
}
4864
}
4965
}

src/Bing.Offices.Npoi/Extensions/SheetExtensions.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using NPOI.SS.UserModel;
1+
using System;
2+
using System.Collections.Generic;
3+
using NPOI.SS.UserModel;
24

35
namespace Bing.Offices.Npoi.Extensions
46
{
@@ -77,5 +79,24 @@ public static int GetHasDataRowNum(this ISheet sheet)
7779
}
7880
return -1;
7981
}
82+
83+
/// <summary>
84+
/// 添加行
85+
/// </summary>
86+
/// <typeparam name="T">实体类型</typeparam>
87+
/// <param name="sheet">工作表</param>
88+
/// <param name="data">数据</param>
89+
/// <param name="action">操作</param>
90+
public static void AddRow<T>(this ISheet sheet, IEnumerable<T> data, Action<IRow, T> action)
91+
{
92+
var index = 1;
93+
foreach (var item in data)
94+
{
95+
var row = sheet.CreateRow(index);
96+
row.Height = 20 * 20;
97+
action(row, item);
98+
index++;
99+
}
100+
}
80101
}
81102
}

src/Bing.Offices.Npoi/Extensions/WorkbookExtensions.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,73 @@ public static void SetAllSheetAutoCompute(this IWorkbook workbook)
9999
}
100100

101101
#endregion
102+
103+
#region AddSheet(添加工作表)
104+
105+
/// <summary>
106+
/// 添加工作表
107+
/// </summary>
108+
/// <param name="workbook">工作簿</param>
109+
/// <param name="name">工作表名称</param>
110+
/// <param name="heads">表头</param>
111+
public static ISheet AddSheet(this IWorkbook workbook,string name,List<string> heads)
112+
{
113+
var sheet = workbook.CreateSheet(name);
114+
var style = workbook.DefaultHeadStyle();
115+
var row = sheet.CreateRow(0);
116+
row.Height = 20 * 20;
117+
heads.ForEach(item => row.Value(heads.IndexOf(item), item, style));
118+
return sheet;
119+
}
120+
121+
#endregion
122+
123+
#region DefaultHeadStyle(默认表头样式)
124+
125+
/// <summary>
126+
/// 默认表头样式
127+
/// </summary>
128+
/// <param name="workbook">工作簿</param>
129+
public static ICellStyle DefaultHeadStyle(this IWorkbook workbook)
130+
{
131+
var style = workbook.CreateCellStyle();
132+
var font = workbook.CreateFont();
133+
font.Boldweight = short.MaxValue;// 加粗
134+
135+
style.FillForegroundColor = 13;// 13为黄色
136+
style.FillPattern = FillPattern.SolidForeground;
137+
style.BorderTop = BorderStyle.Thin;
138+
style.BorderBottom = BorderStyle.Thin;
139+
style.BorderLeft = BorderStyle.Thin;
140+
style.BorderRight = BorderStyle.Thin;
141+
style.Alignment = HorizontalAlignment.Center;
142+
style.VerticalAlignment = VerticalAlignment.Center;
143+
style.SetFont(font.DefaultFont());
144+
return style;
145+
}
146+
147+
#endregion
148+
149+
#region DefaultBodyStyle(默认正文样式)
150+
151+
/// <summary>
152+
/// 默认正文样式
153+
/// </summary>
154+
/// <param name="workbook">工作簿</param>
155+
public static ICellStyle DefaultBodyStyle(this IWorkbook workbook)
156+
{
157+
var style = workbook.CreateCellStyle();
158+
var font = workbook.CreateFont();
159+
style.Alignment = HorizontalAlignment.Center;
160+
style.VerticalAlignment = VerticalAlignment.Center;
161+
style.BorderTop = BorderStyle.Thin;
162+
style.BorderBottom = BorderStyle.Thin;
163+
style.BorderLeft = BorderStyle.Thin;
164+
style.BorderRight = BorderStyle.Thin;
165+
style.SetFont(font.DefaultFont());
166+
return style;
167+
}
168+
169+
#endregion
102170
}
103171
}

src/Bing.Offices.Npoi/Metadata/Excels/NpoiWorkbook.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ public class NpoiWorkbook : IWorkbook
3939
/// <summary>
4040
/// 初始化一个<see cref="NpoiWorkbook"/>类型的实例
4141
/// </summary>
42-
public NpoiWorkbook()
43-
{
44-
Sheets = new List<IWorkSheet>();
45-
}
42+
public NpoiWorkbook() => Sheets = new List<IWorkSheet>();
4643

4744
#endregion
4845

0 commit comments

Comments
 (0)