From 7c0a3155eadbae973f7f061ab7146a57dec42be2 Mon Sep 17 00:00:00 2001 From: xiaochanghai Date: Tue, 17 Dec 2024 15:09:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B9=B4=E5=BA=A6=E4=BA=BA=E5=8A=9B=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=B0=E5=A2=9E=20Excel=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tiobon.Core.Common/Helper/NPOIHelper.cs | 76 ++++++++++++++++++- .../Ghrh/Ghrh_YearHumanSettingsServices.cs | 6 +- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/Tiobon.Core.Common/Helper/NPOIHelper.cs b/Tiobon.Core.Common/Helper/NPOIHelper.cs index 3198ea43..bf96ab47 100644 --- a/Tiobon.Core.Common/Helper/NPOIHelper.cs +++ b/Tiobon.Core.Common/Helper/NPOIHelper.cs @@ -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++; } diff --git a/Tiobon.Core.Services/Ghrh/Ghrh_YearHumanSettingsServices.cs b/Tiobon.Core.Services/Ghrh/Ghrh_YearHumanSettingsServices.cs index 1029e16c..38f3f05b 100644 --- a/Tiobon.Core.Services/Ghrh/Ghrh_YearHumanSettingsServices.cs +++ b/Tiobon.Core.Services/Ghrh/Ghrh_YearHumanSettingsServices.cs @@ -551,15 +551,15 @@ public class Ghrh_YearHumanSettingsServices : BaseServices.OprateSuccess("导出成功", result); }