• 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;
            }
  • 相关阅读:
    file_get_contents抓取远程URL内容
    Xshell下VI打开文件中文乱码解决
    YII实现Memcache故障转移的配置办法
    Nginx实现多重IF判断的办法
    CentOS安装NodeJS v0.10.25 + Express
    一个小玩意 PHP实现微信红包金额拆分试玩
    Web Service测试利器 Postman
    CentOS安装Git
    PHP导出CSV UTF-8转GBK不乱码的解决办法
    configure: error: C++ compiler cannot create executables
  • 原文地址:https://www.cnblogs.com/bmyblogs/p/16113396.html
Copyright © 2020-2023  润新知