C# 自定义导出模板(NPOI)
private async Task<Stream> ExportOrderDtlTemp(string tempName = "订货订单明细导入模板") { IWorkbook workbook = new XSSFWorkbook(); //.xlsx ISheet sheet = workbook.CreateSheet("sheet1"); #region 创建表头 #region 尺码组 //第一行 IRow row = sheet.CreateRow(0); row.CreateCell(0).SetCellValue(""); row.CreateCell(1).SetCellValue(""); row.CreateCell(2).SetCellValue(""); row.CreateCell(3).SetCellValue("尺码组名称A"); row.CreateCell(4).SetCellValue("36"); row.CreateCell(5).SetCellValue("37"); row.CreateCell(6).SetCellValue("38"); row.CreateCell(7).SetCellValue("39"); row.CreateCell(8).SetCellValue("40"); //第二行 IRow row2 = sheet.CreateRow(1); row2.CreateCell(0).SetCellValue(""); row2.CreateCell(1).SetCellValue(""); row2.CreateCell(2).SetCellValue(""); row2.CreateCell(3).SetCellValue("尺码组名称B"); row2.CreateCell(4).SetCellValue("S"); row2.CreateCell(5).SetCellValue("M"); row2.CreateCell(6).SetCellValue("L"); row2.CreateCell(7).SetCellValue("XL"); row2.CreateCell(8).SetCellValue("XXL"); #endregion #region 第三行 IRow rowTitle = sheet.CreateRow(2); rowTitle.CreateCell(0).SetCellValue("商品编码"); rowTitle.CreateCell(1).SetCellValue("商品名称"); rowTitle.CreateCell(2).SetCellValue("颜色"); rowTitle.CreateCell(3).SetCellValue("店铺"); rowTitle.CreateCell(4).SetCellValue("尺码1"); rowTitle.CreateCell(5).SetCellValue("尺码2"); rowTitle.CreateCell(6).SetCellValue("尺码3"); rowTitle.CreateCell(7).SetCellValue("尺码4"); rowTitle.CreateCell(8).SetCellValue("尺码5"); #endregion #endregion //字体颜色 IFont font = workbook.CreateFont(); font.Color = IndexedColors.Red.Index; ICellStyle style = workbook.CreateCellStyle(); style.SetFont(font); rowTitle.GetCell(0).CellStyle = style; rowTitle.GetCell(2).CellStyle = style; rowTitle.GetCell(3).CellStyle = style; //转为字节数组 MemoryStream memoryStream = new MemoryStream(); workbook.Write(memoryStream); var buf = memoryStream.ToArray(); memoryStream = (MemoryStream)null; //返回待下载文件 Stream stream = new MemoryStream(buf); stream.Flush(); stream.Position = 0L; stream.Seek(0L, SeekOrigin.Begin); string str = ".xlsx"; string fileName = (string.IsNullOrEmpty(tempName) ? DateTime.Now.ToString("yyyyMMddHHmmssffff") : tempName) + str; this._httpContextAccessor.HttpContext.Response.ContentType = "application/vnd.ms-excel"; this._httpContextAccessor.HttpContext.Response.ContentLength = new long?(stream.Length); ContentDispositionHeaderValue dispositionHeaderValue = new ContentDispositionHeaderValue((StringSegment)"attachment"); dispositionHeaderValue.SetHttpFileName((StringSegment)fileName); this._httpContextAccessor.HttpContext.Response.Headers["Content-Disposition"] = (StringValues)((object)dispositionHeaderValue).ToString(); this._httpContextAccessor.HttpContext.Response.Headers["Accept-Ranges"] = (StringValues)"bytes"; Stream stream1 = (Stream)stream; stream = (NpoiMemoryStream)null; return stream1; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)