Npoi操作Excel单元格合并,字体样式相关
public async Task<byte[]> ExportNewReportByQuotationId(Guid quotationId) { //string sql = @$"select ""Id"" as ModelId, (q.qq).key as key,(q.qq).value as myValue from(with a as( select ""Id"" ,""Customer"" ,""Family"" ,""ModelPN"" from ""Eme_ProductModelMain"" epmm limit 3) select ""Id"",jsonb_each(row_to_json(a)::jsonb-'qqq'::varchar)as qq from a )q "; // eqpm.""QuotationId"" ,eqpm.""ProductId"" , string sql = @$"select epmm.""ModelPN"" from ""qq_GoodsProductModels"" eqpm left join ""qq_ProductModelMain"" epmm on eqpm.""ProductId"" =epmm.""Id"" where eqpm.""QuotationId""='{quotationId}'"; var listProductPns = await DbScoped.SugarScope.Ado.SqlQueryAsync<string>(sql); int modelCount = listProductPns.Count; IWorkbook wookbook = new XSSFWorkbook(); ISheet sheet = wookbook.CreateSheet("IL"); int rowIndex = 0;//第几行 IRow headRow1 = sheet.CreateRow(rowIndex); headRow1.Height = 350; ICellStyle cellStyleBgColor = wookbook.CreateCellStyle(); cellStyleBgColor.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; cellStyleBgColor.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; cellStyleBgColor.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; cellStyleBgColor.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; ICell cellhead1 = headRow1.CreateCell(0); cellhead1.SetCellValue(""); cellhead1.CellStyle = cellStyleBgColor; for (int mp = 0; mp < modelCount; mp++) { int mindex = mp + 1; ICell cellhead2 = headRow1.CreateCell(mindex); cellhead2.CellStyle = cellStyleBgColor; cellhead2.SetCellValue("HC Required(HC Qty)");//first input } sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 1, modelCount)); sheet.SetColumnWidth(1, Encoding.UTF8.GetBytes("HC Required(HC Qty)").Length * 256 / 2 + 100); sheet.SetColumnWidth(2, Encoding.UTF8.GetBytes("HC Required(HC Qty)").Length * 256 / 2 + 100); ICell cellhead_b2 = headRow1.CreateCell(modelCount + 1); cellhead_b2.SetCellValue("Rate"); cellhead_b2.CellStyle = cellStyleBgColor; ICell cellhead_b1 = headRow1.CreateCell(modelCount + 2); cellhead_b1.SetCellValue("Remark"); cellhead_b1.CellStyle = cellStyleBgColor; rowIndex += 1; IRow headRow2 = sheet.CreateRow(rowIndex); ICell cell1 = headRow2.CreateCell(0); cell1.SetCellValue("HC List"); cell1.CellStyle = cellStyleBgColor; sheet.SetColumnWidth(0, Encoding.UTF8.GetBytes("Materials Supervisors Mfg Tier 1").Length * 256 + 100); for (int p = 0; p < modelCount; p++) { int pindex = p + 1; ICell cell2 = headRow2.CreateCell(pindex); cell2.SetCellValue(listProductPns[p]); cell2.CellStyle = cellStyleBgColor; } int backIndex = modelCount + 1; ICell cellb2 = headRow2.CreateCell(backIndex); cellb2.SetCellValue("Rate"); cellb2.CellStyle = cellStyleBgColor; ICell cellb1 = headRow2.CreateCell(backIndex + 1); cellb1.SetCellValue("Remark"); cellb1.CellStyle = cellStyleBgColor; sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 1, modelCount + 1, modelCount + 1)); sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 1, modelCount + 2, modelCount + 2)); AssmblyExcelModel(modelCount, sheet, cellStyleBgColor); string cpath = Directory.GetCurrentDirectory(); Console.WriteLine("=====================" + cpath); string fulpath = Path.Combine(cpath, "wwwroot/myfiles/1.xlsx"); using var file = File.Create(fulpath); wookbook.Write(file); MemoryStream stream = new MemoryStream(); FileStream fileStream = new FileStream(fulpath, FileMode.OpenOrCreate, FileAccess.ReadWrite); fileStream.CopyTo(stream); byte[] by = stream.ToArray(); stream.Dispose(); fileStream.Dispose(); return by; } private void AssmblyExcelModel(int modelCount, ISheet sheet, ICellStyle cellStyleBgColor) { //到时可以配置出去 var templateStr = ConnectionJsonDto.ExportILLeftNames.Split(",", StringSplitOptions.RemoveEmptyEntries).ToList(); for (int R = 0; R < templateStr.Count; R++) { int dataRow = R + 2; IRow row = sheet.CreateRow(dataRow); ICell cell = row.CreateCell(0); cell.SetCellValue(templateStr[R]); cell.CellStyle = cellStyleBgColor; for (int p = 0; p < modelCount; p++) { int pmodelIndex = p + 1; ICell cell2 = row.CreateCell(pmodelIndex); cell2.SetCellValue(""); cell2.CellStyle = cellStyleBgColor; } ICell cell_back2 = row.CreateCell(modelCount + 1); cell_back2.SetCellValue(""); cell_back2.CellStyle = cellStyleBgColor; ICell cell_back1 = row.CreateCell(modelCount + 2); cell_back1.SetCellValue(""); cell_back1.CellStyle = cellStyleBgColor; } }
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 1, modelCount));
(开始行,最后一行,开始列,最后一列)
ICellStyle cellStyle = workbook.CreateCellStyle(); cellStyle.BorderBottom = BorderStyle.Thin; cellStyle.BorderLeft = BorderStyle.Thin; cellStyle.BorderRight = BorderStyle.Thin; cellStyle.BorderTop = BorderStyle.Thin; cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; ICellStyle cellStyleTitle = workbook.CreateCellStyle(); cellStyleTitle.BorderBottom = BorderStyle.Thin; cellStyleTitle.BorderLeft = BorderStyle.Thin; cellStyleTitle.BorderRight = BorderStyle.Thin; cellStyleTitle.BorderTop = BorderStyle.Thin; cellStyleTitle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; cellStyleTitle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; IFont font = workbook.CreateFont(); font.IsBold = false; font.FontHeightInPoints = 12; cellStyleTitle.SetFont(font); HSSFPalette palette = ((HSSFWorkbook)workbook).GetCustomPalette(); palette.SetColorAtIndex((short)9, 98, 185, 106); var color = palette.FindColor(98, 185, 106); //下面两行设置单元格背景色 cellStyleTitle.FillPattern = FillPattern.SolidForeground; cellStyleTitle.FillForegroundColor = color.Indexed; ICellStyle cellStyleTitle2 = workbook.CreateCellStyle(); cellStyleTitle2.CloneStyleFrom(cellStyleTitle); font = workbook.CreateFont(); font.IsBold = false; font.FontHeightInPoints = 14; cellStyleTitle2.SetFont(font);
部分code截图
如有疑问或者错误的地方,请跟帖,本人会第一时间答复以及相互学习,谢谢!个人会不断的上传自己的学习心得!
好了今天就先到这里,下次有时间再更新,如果存在不合理的地方,欢迎大家多多指教留言!!!
分类:
其它/测试等
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具