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;
        }

 

posted @ 2022-04-07 17:35  baileyer  阅读(307)  评论(0编辑  收藏  举报