|
|
|
@ -4,6 +4,7 @@ using NPOI.HPSF; |
|
|
|
|
using NPOI.HSSF.UserModel; |
|
|
|
|
using NPOI.SS.UserModel; |
|
|
|
|
using NPOI.XSSF.UserModel; |
|
|
|
|
using OfficeOpenXml.Drawing; |
|
|
|
|
|
|
|
|
|
namespace Tiobon.Core.Common.Helper; |
|
|
|
|
|
|
|
|
@ -77,6 +78,7 @@ public class NPOIHelper |
|
|
|
|
} |
|
|
|
|
int rowIndex = 0; |
|
|
|
|
int intTop = 0; |
|
|
|
|
int HeightInPoints = 40; |
|
|
|
|
foreach (DataRow row in dtSource.Rows) |
|
|
|
|
{ |
|
|
|
|
#region 新建表、填充表头、填充列头,样式 |
|
|
|
@ -87,6 +89,7 @@ public class NPOIHelper |
|
|
|
|
sheet = workbook.CreateSheet(); |
|
|
|
|
} |
|
|
|
|
intTop = 0; |
|
|
|
|
|
|
|
|
|
#region 表头及样式 |
|
|
|
|
{ |
|
|
|
|
if (!string.IsNullOrWhiteSpace(strHeaderText) && strHeaderText.Length > 0) |
|
|
|
@ -123,16 +126,71 @@ public class NPOIHelper |
|
|
|
|
//headStyle.SetFont(font); |
|
|
|
|
headerRow.GetCell(0).CellStyle = titleStyle; |
|
|
|
|
// 设置行高 |
|
|
|
|
headerRow.HeightInPoints = 45; |
|
|
|
|
headerRow.HeightInPoints = 30; |
|
|
|
|
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dtSource.Columns.Count - 1)); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region 年度人力配置 |
|
|
|
|
{ |
|
|
|
|
if (sheetName == "年度人力配置") |
|
|
|
|
{ |
|
|
|
|
HeightInPoints = 15; |
|
|
|
|
#region 标题单元格样式 |
|
|
|
|
|
|
|
|
|
ICellStyle titleStyle = workbook.CreateCellStyle(); |
|
|
|
|
titleStyle.Alignment = HorizontalAlignment.Center; //居中 |
|
|
|
|
titleStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中 |
|
|
|
|
titleStyle.WrapText = true;//自动换行 |
|
|
|
|
// 边框 |
|
|
|
|
titleStyle.BorderBottom = BorderStyle.Thin; |
|
|
|
|
titleStyle.BorderLeft = BorderStyle.Thin; |
|
|
|
|
titleStyle.BorderRight = BorderStyle.Thin; |
|
|
|
|
titleStyle.BorderTop = BorderStyle.Thin; |
|
|
|
|
|
|
|
|
|
IFont font = workbook.CreateFont(); |
|
|
|
|
font.FontHeightInPoints = (short)10; |
|
|
|
|
font.FontName = "宋体"; |
|
|
|
|
font.IsBold = true; |
|
|
|
|
titleStyle.SetFont(font); |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
IRow headerRow = sheet.CreateRow(intTop); |
|
|
|
|
headerRow.HeightInPoints = HeightInPoints; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 5; i++) |
|
|
|
|
{ |
|
|
|
|
headerRow.CreateCell(i).SetCellValue(dtSource.Columns[i].ColumnName); |
|
|
|
|
headerRow.GetCell(i).CellStyle = titleStyle; |
|
|
|
|
} |
|
|
|
|
for (int i = 5; i < 17; i++) |
|
|
|
|
{ |
|
|
|
|
headerRow.CreateCell(i).SetCellValue("预算编制数"); |
|
|
|
|
headerRow.GetCell(i).CellStyle = titleStyle; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//headerRow.CreateCell(5).SetCellValue("预算编制数"); |
|
|
|
|
//headerRow.GetCell(5).CellStyle = titleStyle; |
|
|
|
|
headerRow.CreateCell(17).SetCellValue("备注"); |
|
|
|
|
headerRow.GetCell(17).CellStyle = titleStyle; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 5; i++) |
|
|
|
|
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0 + intTop, 1 + intTop, i, i)); |
|
|
|
|
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0 + intTop, 1 + intTop, 17, 17)); |
|
|
|
|
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0 + intTop, 0 + intTop, 5, 16)); |
|
|
|
|
|
|
|
|
|
intTop += 1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
#region 列头及样式 |
|
|
|
|
{ |
|
|
|
|
IRow headerRow = sheet.CreateRow(intTop); |
|
|
|
|
headerRow.HeightInPoints = 40; |
|
|
|
|
headerRow.HeightInPoints = HeightInPoints; |
|
|
|
|
intTop += 1; |
|
|
|
|
|
|
|
|
|
ICellStyle headStyle = workbook.CreateCellStyle(); |
|
|
|
@ -270,7 +328,19 @@ public class NPOIHelper |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (DataColumn column in dtSource.Columns) |
|
|
|
|
{ |
|
|
|
|
//设置列宽 |
|
|
|
|
//sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256); |
|
|
|
|
if (arrColWidth[column.Ordinal] > 255) |
|
|
|
|
{ |
|
|
|
|
arrColWidth[column.Ordinal] = 254; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
rowIndex++; |
|
|
|
|
} |
|
|
|
|