asp.net api使用NPOI 导出xls

2021-06-08 20:05

阅读:587

标签:address   origin   ret   for   文件   attach   datetime   encode   ice   

 public HttpResponseMessage ExportSupportByIds(string ids)
        {
            Listint> idsIntList = new Listint>();
            var idsList = ids.Split(,).ToList();
            if (idsList == null)
                return CommonHelper.HttpResponseMessageJSON(new Response(ResponseCode.ReqInvalidParameter, "参数不能为空"));
            if (idsList.Count() 0)
                return CommonHelper.HttpResponseMessageJSON(new Response(ResponseCode.ReqInvalidParameter, "参数不能为空"));
            foreach (var item in idsList)
            {
                idsIntList.Add(Convert.ToInt32(item));
            }
            if (idsList == null)
                return CommonHelper.HttpResponseMessageJSON(new Response(ResponseCode.ReqInvalidParameter, "参数不能为空"));
            if (idsList.Count() 0)
                return CommonHelper.HttpResponseMessageJSON(new Response(ResponseCode.ReqInvalidParameter, "参数不能为空"));
            var list = new crowd_supportBLL().GetListModelByIds(idsIntList);
            if (list == null || list.Count() 0)
                return CommonHelper.HttpResponseMessageJSON(new Response(ResponseCode.RspEmpty, "数据为空"));
            var response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StreamContent(new crowd_supportBLL().ExportSupport(list));
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            var fileName = "聚好物项目支持列表" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = System.Web.HttpUtility.UrlEncode(fileName)
            };
            return response;
        }
 public MemoryStream ExportSupport(List list)
        {
            //创建Excel文件的对象
            NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
            //添加一个sheet
            NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");
            //获取list数据
            //List listRainInfo = m_BLL.GetSchoolListAATQ(schoolname);
            //给sheet1添加第一行的头部标题
            NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
            row1.CreateCell(0).SetCellValue("订单编号");
            row1.CreateCell(1).SetCellValue("支持人");
            row1.CreateCell(2).SetCellValue("支持档次");
            row1.CreateCell(3).SetCellValue("支持数量");

            row1.CreateCell(4).SetCellValue("支持金额(元)");
            row1.CreateCell(5).SetCellValue("支付时间");
            row1.CreateCell(6).SetCellValue("订单状态");

            row1.CreateCell(7).SetCellValue("回报发货时间");

            row1.CreateCell(8).SetCellValue("收货人");
            row1.CreateCell(9).SetCellValue("收货地址");
            row1.CreateCell(10).SetCellValue("发票信息");
            row1.CreateCell(11).SetCellValue("快递公司");
            row1.CreateCell(12).SetCellValue("物流单号");
            //将数据逐步写入sheet1各个行
            for (int i = 0; i )
            {
                NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
                rowtemp.CreateCell(0).SetCellValue(list[i].crowd_support_order); //订单编号
                rowtemp.CreateCell(1).SetCellValue(list[i].nickname); //支持人
                rowtemp.CreateCell(2).SetCellValue((double)list[i].level_amount); //支持档次
                rowtemp.CreateCell(3).SetCellValue(list[i].buy_num);//支持数量
                rowtemp.CreateCell(4).SetCellValue((double)list[i].payment_amount); //支持金额(元)
                rowtemp.CreateCell(5).SetCellValue(list[i].pay_time.ToString()); //支付时间
                switch (list[i].pay_status)
                {
                    case 0:
                        rowtemp.CreateCell(6).SetCellValue("未付款"); //订单状态
                        break;
                    case 1:
                        rowtemp.CreateCell(6).SetCellValue("已支付"); //订单状态
                        break;
                    case 2:
                        rowtemp.CreateCell(6).SetCellValue("已退款"); //订单状态
                        break;
                    case 3:
                        rowtemp.CreateCell(6).SetCellValue("已取消"); //订单状态
                        break;
                    default:
                        break;
                }
                rowtemp.CreateCell(7).SetCellValue(list[i].sendout_time.ToString()); //回报发货时间
                rowtemp.CreateCell(8).SetCellValue(list[i].consignee + "/" + list[i].consignee_tel); //收货人
                rowtemp.CreateCell(9).SetCellValue(list[i].consignee_address); //收货地址

                var invoice = "";
                //抬头 / 税号 / 公司地址 / 公司地址 / 开户账号 / 开户银行
                var invoice_str = list[i]?.invoice_str?.ToString();
                var invoice_code = list[i]?.invoice_code?.ToString();
                var invoice_address = list[i]?.invoice_address?.ToString();
                var invoice_account = list[i]?.invoice_account?.ToString();
                var invoice_bank = list[i]?.invoice_bank?.ToString();
                if (!string.IsNullOrWhiteSpace(invoice_str))
                {
                    invoice += invoice_str + "/";
                }
                if (!string.IsNullOrWhiteSpace(invoice_code))
                {
                    invoice += invoice_code + "/";
                }
                if (!string.IsNullOrWhiteSpace(invoice_address))
                {
                    invoice += invoice_address + "/";
                }
                if (!string.IsNullOrWhiteSpace(invoice_account))
                {
                    invoice += invoice_account + "/";
                }
                if (!string.IsNullOrWhiteSpace(invoice_bank))
                {
                    invoice += invoice_bank + "/";
                }
                rowtemp.CreateCell(10).SetCellValue(invoice?.ToString());  //发票信息
                rowtemp.CreateCell(11).SetCellValue(list[i]?.track_name?.ToString()); //快递公司
                rowtemp.CreateCell(12).SetCellValue(list[i]?.trackingorder?.ToString());  //物流单号

            }
            // 写入到客户端 
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            book.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }

 

asp.net api使用NPOI 导出xls

标签:address   origin   ret   for   文件   attach   datetime   encode   ice   

原文地址:https://www.cnblogs.com/ichengq/p/10679311.html


评论


亲,登录后才可以留言!