Skip to content

Commit 1b41f14

Browse files
author
helin
committed
更新NPOI为2.7.0 稳定版
1 parent bbae289 commit 1b41f14

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

IThink.ExcelHelper/IThink.ExcelHelper.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</PropertyGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="NPOI" Version="2.7.0-rc1" />
26+
<PackageReference Include="NPOI" Version="2.7.0" />
2727
</ItemGroup>
2828

2929
<ItemGroup>

IThink.ExcelHelper/NExcelHelper.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,52 @@ public static byte[] Save(this IWorkbook workbook)
9999
}
100100
}
101101

102+
/// <summary>
103+
/// 执行导出
104+
/// </summary>
105+
/// <typeparam name="T"></typeparam>
106+
/// <param name="dataList"></param>
107+
/// <param name="dataRowIndex">数据行</param>
108+
/// <param name="templateFullPath">模板全路径</param>
109+
/// <param name="cellStyleFunc">T1:current workbook T2:row index T3:col index</param>
110+
/// <returns></returns>
111+
public static byte[] Export<T>(this List<T> dataList, string templateFullPath, int dataRowIndex, Func<IWorkbook, int, int, ICellStyle> cellStyleFunc) where T : IExportModel
112+
{
113+
var exportData = dataList.GetExportData(dataRowIndex);
114+
115+
using (var templatefs = new FileStream(templateFullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
116+
{
117+
var xssfWorkbook = new XSSFWorkbook(templatefs);
118+
119+
var workSheet = xssfWorkbook.GetSheetAt(0);
120+
121+
foreach (var rowData in exportData)
122+
{
123+
var row = workSheet.CreateRow(rowData.RowNo);
124+
var cols = rowData.Cols.OrderBy(s => s.ColNo);
125+
foreach (var colData in cols)
126+
{
127+
var cell = row.CreateCell(colData.ColNo);
128+
SetValue(cell, colData.Value);
129+
130+
if (cellStyleFunc != null)
131+
{
132+
var cellstyle = cellStyleFunc(xssfWorkbook, rowData.RowNo, colData.ColNo);
133+
cell.CellStyle = cellstyle;
134+
}
135+
}
136+
}
137+
138+
using (var memoryStream = new MemoryStream())
139+
{
140+
xssfWorkbook.Write(memoryStream);
141+
xssfWorkbook.Close();
142+
143+
return memoryStream.ToArray();
144+
}
145+
}
146+
}
147+
102148
/// <summary>
103149
/// 执行导出
104150
/// </summary>
@@ -145,6 +191,46 @@ public static byte[] Export<T>(this List<T> dataList, string templateFullPath, i
145191
}
146192
}
147193

194+
/// <summary>
195+
///
196+
/// </summary>
197+
/// <typeparam name="T"></typeparam>
198+
/// <param name="dataList"></param>
199+
/// <param name="templateFullPath"></param>
200+
/// <param name="dataRowIndex"></param>
201+
/// <param name="cellStyleFunc">T1:current workbook T2:row index T3:col index</param>
202+
/// <returns></returns>
203+
public static XSSFWorkbook ExportWorkbook<T>(this List<T> dataList, string templateFullPath, int dataRowIndex, Func<IWorkbook, int, int, ICellStyle> cellStyleFunc) where T : IExportModel
204+
{
205+
var exportData = dataList.GetExportData(dataRowIndex);
206+
207+
using (var templatefs = new FileStream(templateFullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
208+
{
209+
var xssfWorkbook = new XSSFWorkbook(templatefs);
210+
211+
var workSheet = xssfWorkbook.GetSheetAt(0);
212+
213+
foreach (var rowData in exportData)
214+
{
215+
var row = workSheet.CreateRow(rowData.RowNo);
216+
var cols = rowData.Cols.OrderBy(s => s.ColNo);
217+
foreach (var colData in cols)
218+
{
219+
var cell = row.CreateCell(colData.ColNo);
220+
SetValue(cell, colData.Value);
221+
222+
if (cellStyleFunc != null)
223+
{
224+
var cellstyle = cellStyleFunc(xssfWorkbook, rowData.RowNo, colData.ColNo);
225+
cell.CellStyle = cellstyle;
226+
}
227+
}
228+
}
229+
230+
return xssfWorkbook;
231+
}
232+
}
233+
148234
/// <summary>
149235
///
150236
/// </summary>
@@ -376,6 +462,7 @@ public static dynamic GetValue(this ICell cell)
376462
/// 获取NPOI的单元格的值
377463
/// </summary>
378464
/// <param name="cell"></param>
465+
/// <param name="autoTransferDateValue"></param>
379466
/// <returns></returns>
380467
internal static SheetDataColumn GetCellValue(this ICell cell, bool autoTransferDateValue = false)
381468
{

IThink.ExcelHelper/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
work with npoi and help to export or import easier.
33

44
how to use!
5+
nuget H.Npoi.ExcelHelper
6+
57
1. import
68

79
![image](https://user-images.githubusercontent.com/38428011/199665711-36a2c5ea-6aef-4881-bff3-1ab9f7d3e4ff.png)

0 commit comments

Comments
 (0)