C# 使用NPOI 设置保存的表格样式

您所在的位置:网站首页 图片生成表格的软件下载 C# 使用NPOI 设置保存的表格样式

C# 使用NPOI 设置保存的表格样式

2023-06-02 18:35| 来源: 网络整理| 查看: 265

       通常我们直接通过NPOI实现保存功能,不设置任何样式,那么通常是版型非常普通,没有字体设置、 颜色设置 、单元格 等设置。 使保存出来的表格显示的非常普通。不具有视觉冲击感。也不能突出显示我们想要提示的信息。如下图 我们设置 保存的表格的Style 效果为 表头背景颜色填充为绿色。字体加粗 居中显示。 表单的内容全部居中 表头长的进行设置相应的列宽。

文章建议:阅读下前篇点击此处https://blog.csdn.net/qq_71897293/article/details/129859589?spm=1001.2014.3001.5502

实现效果 如下图 

 

设置   内容 1   表头的填充颜色

           内容 2   表头的首行白字 加粗 居中

           内容3   表单的居中地与出生地的宽度设置

           内容4   表单的内容居中

样式一

/// /// 表格标题样式 /// /// /// private static ICellStyle Tableheaderstyle(IWorkbook workbook) { ICellStyle style = workbook.CreateCellStyle(); style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; // 设置水平居中显示 style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; // 设置垂直居中显示 style.FillForegroundColor = IndexedColors.White.Index; // 设置字体颜色为白色 style.FillPattern = (FillPattern)1; // 设置填充模式 style.FillForegroundColor = IndexedColors.Green.Index;//设置填充颜色 style.WrapText = true; // 自动换行 // 创建字体 IFont font = workbook.CreateFont(); font.Color = IndexedColors.White.Index; // 字体颜色为白色 font.FontHeightInPoints = 10; // 字体大小 font.IsBold = true; style.SetFont(font); return style; }

示例解释 :上述代码实现效果 :填充颜色、字体 白字 加粗 居中

样式二

/// /// 表格内容居中 /// /// /// private static ICellStyle Thetablecontentcentered(IWorkbook workbook) { ICellStyle cellStyle = workbook.CreateCellStyle(); cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; return cellStyle; }

示例解释: 上述代码实现效果 :字体  居中

扩展 设置列宽 是通过 表单 然后 设置的方法 SetColumnWidth 其中参数1是 设置的第几列 后面的是设置多宽!!!

sheet.SetColumnWidth(i, 11 * 256);

如果前面这一行没看懂 那我粘贴出整个代码 

/// /// 写入成Excel数据 /// /// 数据源 /// 储存文件地址 public static void ExportToExcel(DataTable dt, string fileName, T T1) where T : struct { if (fileName == null) { Paramecium.App.ViewModels.Communals.MessageBoxHelper.ShowDialog_StringResult("提示", "保存位置为空"); } IWorkbook workbook = new XSSFWorkbook(); string fileExt = Path.GetExtension(fileName).ToLower(); //XSSFWorkbook 适用XLSX格式,HSSFWorkbook 适用XLS格式 ISheet sheet = workbook.CreateSheet("数据"); // 创建样式 ICellStyle style = Tableheaderstyle(workbook); ICellStyle cellStyle = Thetablecontentcentered(workbook); IRow row = sheet.CreateRow(0); //表头 if (T1 is Temp) { for (int i = 0; i < dt.Columns.Count; i++) { if (i >= 8) break; sheet.SetColumnWidth(i, i * 25 * 100); ICell cell = row.CreateCell(i); cell.CellStyle = style; cell.SetCellValue(((Temp)i).ToString()); } } else if (T1 is weighcounter) { for (int i = 0; i < dt.Columns.Count; i++) { if (i >= 8) break; ICell cell = row.CreateCell(i); cell.CellStyle = style; cell.SetCellValue(((weighcounter)i).ToString()); } } else if (T1 is weighcounter2) { for (int i = 0; i < dt.Columns.Count; i++) { //if (i >= 8) break; ICell cell = row.CreateCell(i); cell.CellStyle = style; cell.SetCellValue(((weighcounter2)i).ToString()); } } else if (T1 is Liquidpumpprospecting) { for (int i = 0; i < dt.Columns.Count; i++) { ICell cell = row.CreateCell(i); cell.CellStyle = style; cell.SetCellValue(((Liquidpumpprospecting)i).ToString()); } } for (int i = 0; i < dt.Rows.Count; i++) { IRow row1 = sheet.CreateRow(i + 1); for (int j = 0; j < dt.Columns.Count; j++) { ICell cell = row1.CreateCell(j); cell.CellStyle = cellStyle; cell.SetCellValue(System.Convert.ToDouble(dt.Rows[i][j]).ToString("F2")); } } //保存为Excel文件 using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { workbook.Write(fs); } }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3