.Net Core Spire.XLS 在Excel中合并单元格 合并单元格

核心方法
sheet.Range["A1:B5"].Merge();

合并前
合并前.png
代码(根据自己实际情况,这只是项目里的代码块)
#region ExportVipData 导出订单管理
        public string ExportGoodOrderData(string tempFileName, string tagFileName, List data,string state)
        {
            Dictionary dic = new Dictionary();
            //新建Workbook
            Workbook workbook = new Workbook();
            //根据模板生成Workbook
            workbook.LoadTemplateFromFile(tempFileName);

            //获取第一个工作表
            Worksheet sheet = workbook.Worksheets[0];
            int i = 2;
            int start = 0;
            try
            {
                foreach (GoodsOrderModel item in data)
                {
                    try
                    {
                        GoodsOrderDetailData orderDetailData = BizBase.Get().ViewGoodsOrderDetail(item.Code);
                        string goodInfo = "";
                        string cause = "";
                        string phone = "";
                        string time = "";
                        int y = 2;
                        int x = 2;
                        int lenth = 0;
                        if (orderDetailData != null && orderDetailData.OrderDetailList.Count > 0)
                        {
                            lenth = orderDetailData.OrderDetailList.Count;
                            foreach (var orderDetail in orderDetailData.OrderDetailList)
                            {
                                if (state == "10")
                                {
                                    if (orderDetail.NGoodsState == 0 || orderDetail.NGoodsState == 1)
                                    {
                                        goodInfo = $"{orderDetail.SGoodsName}*{orderDetail.SSpecif}*{orderDetail.NCount}";
                                    }
                                }
                                else
                                {
                                    if (orderDetail.NGoodsState == 0 || orderDetail.NGoodsState == 1)
                                    {
                                        goodInfo= $"{orderDetail.SGoodsName}*{orderDetail.SSpecif}*{orderDetail.NCount}";
                                    }
                                    else
                                    {
                                        goodInfo = $"{orderDetail.SGoodsName}*{orderDetail.SSpecif}*{orderDetail.NCount}(退货商品)";
                                    }
                                }
                                if (!string.IsNullOrEmpty(orderDetail.SPhone))
                                {
                                    phone = orderDetail.SPhone;
                                }
                                if (!string.IsNullOrEmpty(orderDetail.SCause))
                                {
                                    cause = orderDetail.SCause;
                                }
                                if (!string.IsNullOrEmpty(orderDetail.TRefund.ToString()))
                                {
                                    time = orderDetail.TRefund.ToString();
                                }
                                sheet.SetCellValue(i, 1, (i - 1).ToString());
                                sheet.SetText(i, 2, Filters(item.Code)); // 订单号
                                sheet.SetCellValue(i, 3, Filters(item.SVipName)); // 用户名称
                                sheet.SetCellValue(i, 4, Filters(item.SConsigneePhone)); // 电话号码
                                sheet.SetCellValue(i, 5, Filters(item.SLogisticsAddress)); // 自提点
                                sheet.SetCellValue(i, 6, Filters(goodInfo)); // 商品明细
                                sheet.SetCellValue(i, 7, Filters(item.SPickUpCode)); // 入库码
                                sheet.SetCellValue(i, 8, Filters(item.SPickDay)); // 提货时间
                                switch (item.NState)
                                {
                                    case 0:
                                        sheet.SetCellValue(i, 9, "未支付"); //订单状态
                                        break;
                                    case 1:
                                        sheet.SetCellValue(i, 9, "已支付"); //订单状态
                                        break;
                                    case 2:
                                        sheet.SetCellValue(i, 9, "已完成"); //订单状态
                                        break;
                                    case 3:
                                        sheet.SetCellValue(i, 9, "支付失败"); //订单状态
                                        break;
                                    case 4:
                                        sheet.SetCellValue(i, 9, "申请退款"); //订单状态
                                        break;
                                    case 5:
                                        sheet.SetCellValue(i, 9, "退款失败"); //订单状态
                                        break;
                                    case 6:
                                        sheet.SetCellValue(i, 9, "退款成功"); //订单状态
                                        break;
                                    case 7:
                                        sheet.SetCellValue(i, 9, "待评价"); //订单状态
                                        break;
                                    case 8:
                                        sheet.SetCellValue(i, 9, "已评价"); //订单状态
                                        break;
                                    case 9:
                                        sheet.SetCellValue(i, 9, "已发货"); //订单状态
                                        break;
                                    case 10:
                                        sheet.SetCellValue(i, 9, "待发货"); //订单状态
                                        break;
                                    case 11:
                                        sheet.SetCellValue(i, 9, "提货成功"); //订单状态
                                        break;
                                    case 12:
                                        sheet.SetCellValue(i, 9, "待提货"); //订单状态
                                        break;
                                }
                                sheet.SetCellValue(i, 10, Filters(item.SRefundMoney));
                                if (!string.IsNullOrEmpty(phone))
                                {
                                    sheet.SetCellValue(i, 11, Filters(phone));
                                }
                                if (!string.IsNullOrEmpty(cause))
                                {
                                    sheet.SetCellValue(i, 12, Filters(cause));
                                }
                                if (!string.IsNullOrEmpty(time))
                                {
                                    sheet.SetCellValue(i, 13, Filters(time));
                                }
                                //sheet.SetCellValue(i, 12, Filters(item.TRefund.ToString()));
                                
                                i++;
                                y++;
                            }
                           
                            if (i==y)
                            {
                                // 表格合并
                                sheet.Range[$"B{x}:B{lenth+1}"].Merge();
                                sheet.Range[$"C{x}:C{lenth+1}"].Merge();
                                sheet.Range[$"D{x}:D{lenth+1}"].Merge();
                                sheet.Range[$"E{x}:E{lenth+1}"].Merge();
                                sheet.Range[$"G{x}:G{lenth+1}"].Merge();
                                sheet.Range[$"H{x}:H{lenth+1}"].Merge();
                                start = start + x + lenth;
                            }
                            else
                            {
                                // 表格合并
                                sheet.Range[$"B{start}:B{start+ lenth-1}"].Merge();
                                sheet.Range[$"C{start}:C{start+ lenth-1}"].Merge();
                                sheet.Range[$"D{start}:D{start+ lenth-1}"].Merge();
                                sheet.Range[$"E{start}:E{start+ lenth-1}"].Merge();
                                sheet.Range[$"G{start}:G{start+ lenth-1}"].Merge();
                                sheet.Range[$"H{start}:H{start+ lenth-1}"].Merge();
                                start = start + lenth;
                            }
                            
                        }
                        
                    }
                    catch(Exception ex)
                    {
                        continue;
                    }
                }
                CellRange range = sheet.Range[$"A2:M{i}"];
                range.BorderInside(LineStyleType.Thin);
                range.BorderAround(LineStyleType.Medium);
                range.Style.HorizontalAlignment = HorizontalAlignType.Center; // 文字居中
            }
            catch (Exception ex)
            {
            }
            workbook.SaveToFile(tagFileName);
            return tagFileName;
        }
        #endregion
结果:
合并后.png

你可能感兴趣的:(.Net Core Spire.XLS 在Excel中合并单元格 合并单元格)