From 2acc901da8bf27b38d4b3df54de1e6cc113816fd Mon Sep 17 00:00:00 2001 From: xiaochanghai Date: Thu, 8 Aug 2024 13:24:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Excel=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tiobon.Core.Common/Helper/NPOIHelper.cs | 133 ++++++++++++++++++++---- 1 file changed, 114 insertions(+), 19 deletions(-) diff --git a/Tiobon.Core.Common/Helper/NPOIHelper.cs b/Tiobon.Core.Common/Helper/NPOIHelper.cs index 73987eb1..8fdfe239 100644 --- a/Tiobon.Core.Common/Helper/NPOIHelper.cs +++ b/Tiobon.Core.Common/Helper/NPOIHelper.cs @@ -3,8 +3,10 @@ using System.IO; using System.Text; using NPOI.HPSF; using NPOI.HSSF.UserModel; +using NPOI.OpenXmlFormats.Spreadsheet; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; +using RestSharp; namespace Tiobon.Core.Common.Helper; @@ -92,17 +94,39 @@ public class NPOIHelper { if (!string.IsNullOrWhiteSpace(strHeaderText) && strHeaderText.Length > 0) { + + #region 标题单元格样式 + + ICellStyle titleStyle = workbook.CreateCellStyle(); + titleStyle.Alignment = HorizontalAlignment.Center; //居中 + titleStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中 + titleStyle.WrapText = true;//自动换行 + // 边框 + titleStyle.BorderBottom = BorderStyle.Thin; + titleStyle.BorderLeft = BorderStyle.None; + titleStyle.BorderRight = BorderStyle.None; + titleStyle.BorderTop = BorderStyle.None; + + IFont font = workbook.CreateFont(); + font.FontHeightInPoints = (short)14; + font.FontName = "宋体"; + font.IsBold = true; + titleStyle.SetFont(font); + #endregion + IRow headerRow = sheet.CreateRow(intTop); intTop += 1; headerRow.HeightInPoints = 25; headerRow.CreateCell(0).SetCellValue(strHeaderText); - ICellStyle headStyle = workbook.CreateCellStyle(); - headStyle.Alignment = HorizontalAlignment.Center; - IFont font = workbook.CreateFont(); - font.FontHeightInPoints = 20; - font.Boldweight = 700; - headStyle.SetFont(font); - headerRow.GetCell(0).CellStyle = headStyle; + //ICellStyle headStyle = workbook.CreateCellStyle(); + //headStyle.Alignment = HorizontalAlignment.Center; + //IFont font = workbook.CreateFont(); + //font.FontHeightInPoints = 20; + //font.IsBold = true; + //headStyle.SetFont(font); + headerRow.GetCell(0).CellStyle = titleStyle; + // 设置行高 + headerRow.HeightInPoints = 45; sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dtSource.Columns.Count - 1)); } @@ -111,15 +135,33 @@ public class NPOIHelper #region 列头及样式 { IRow headerRow = sheet.CreateRow(intTop); + headerRow.HeightInPoints = 40; intTop += 1; + ICellStyle headStyle = workbook.CreateCellStyle(); - headStyle.Alignment = HorizontalAlignment.Center; - headStyle.BorderBottom = BorderStyle.Medium; - headStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.LightGreen.Index; - headStyle.FillPattern = FillPattern.NoFill; + headStyle.Alignment = HorizontalAlignment.Center; //居中 + headStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中 + headStyle.WrapText = true;//自动换行 + // 边框 + headStyle.BorderBottom = BorderStyle.Thin; + headStyle.BorderLeft = BorderStyle.Thin; + headStyle.BorderRight = BorderStyle.Thin; + headStyle.BorderTop = BorderStyle.Thin; + // 字体 IFont font = workbook.CreateFont(); - font.Boldweight = 700; + font.FontHeightInPoints = (short)10; + font.IsBold = true; + font.FontName = "宋体"; headStyle.SetFont(font); + + //ICellStyle headStyle = workbook.CreateCellStyle(); + //headStyle.Alignment = HorizontalAlignment.Center; + //headStyle.BorderBottom = BorderStyle.Medium; + //headStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.LightGreen.Index; + //headStyle.FillPattern = FillPattern.NoFill; + //IFont font = workbook.CreateFont(); + //font.Boldweight = 700; + //headStyle.SetFont(font); foreach (DataColumn column in dtSource.Columns) { headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); @@ -144,9 +186,28 @@ public class NPOIHelper #endregion #region 填充内容 IRow dataRow = sheet.CreateRow(rowIndex); + dataRow.HeightInPoints = 25; + + ICellStyle style = workbook.CreateCellStyle(); + style.Alignment = HorizontalAlignment.Center; //居中 + style.VerticalAlignment = VerticalAlignment.Center;//垂直居中 + style.WrapText = true;//自动换行 + // 边框 + style.BorderBottom = BorderStyle.Thin; + style.BorderLeft = BorderStyle.Thin; + style.BorderRight = BorderStyle.Thin; + style.BorderTop = BorderStyle.Thin; + // 字体 + var font1 = workbook.CreateFont(); + font1.FontHeightInPoints = (short)10; + font1.FontName = "宋体"; + style.SetFont(font1); + foreach (DataColumn column in dtSource.Columns) { ICell newCell = dataRow.CreateCell(column.Ordinal); + + newCell.CellStyle = style; string drValue = row[column].ToString(); switch (column.DataType.ToString()) { @@ -162,15 +223,15 @@ public class NPOIHelper newCell.SetCellValue(dateV); if (column.Caption == "renderDateTime") { - newCell.CellStyle = datetimeStyle;//格式化显示到分钟 + newCell.CellStyle.DataFormat = format.GetFormat("yyyy-mm-dd hh:mm"); } else if (column.Caption == "renderDate") - { - newCell.CellStyle = dateStyle;//格式化显示到天 + { + newCell.CellStyle.DataFormat = format.GetFormat("yyyy-mm-dd"); } else - { - newCell.CellStyle = datetimesStyle;//格式化显示到秒 + { + newCell.CellStyle.DataFormat = format.GetFormat("yyyy-mm-dd hh:mm:ss"); } } break; @@ -202,6 +263,40 @@ public class NPOIHelper } } #endregion + + //获取当前列的宽度,然后对比本列的长度,取最大值 + if (true) + for (int columnNum = 0; columnNum <= dtSource.Columns.Count; columnNum++) + { + sheet.AutoSizeColumn(columnNum);//先来个常规自适应 + + var columnWidth = sheet.GetColumnWidth(columnNum) / 256; + for (int rowNum = 1; rowNum <= sheet.LastRowNum; rowNum++) + { + IRow currentRow; + //当前行未被使用过 + if (sheet.GetRow(rowNum) == null) + { + currentRow = sheet.CreateRow(rowNum); + } + else + { + currentRow = sheet.GetRow(rowNum); + } + if (currentRow.GetCell(columnNum) != null) + { + ICell currentCell = currentRow.GetCell(columnNum); + int length = Encoding.Default.GetBytes(currentCell.ToString()).Length; + if (columnWidth < length) + { + columnWidth = length; + if (columnWidth > 30) columnWidth = 30; + } + } + } + sheet.SetColumnWidth(columnNum, columnWidth * 300); // 256 + } + rowIndex++; } using (MemoryStream ms = new MemoryStream()) @@ -286,7 +381,7 @@ public class NPOIHelper { IRow row = sheet.GetRow(i); - if(row is null) + if (row is null) continue; //if (row.GetCell(row.FirstCellNum) != null && row.GetCell(row.FirstCellNum).ToString().Length > 0) //if (row.GetCell(row.FirstCellNum) != null) @@ -294,7 +389,7 @@ public class NPOIHelper DataRow dataRow = dt.NewRow(); for (int j = row.FirstCellNum; j < cellCount; j++) { - if(j<0) + if (j < 0) continue; if (row.GetCell(j) != null) {