导出到Excel时如何根据条件赋予标题?

时间:2019-04-16 05:09:16

标签: .net excel export-to-excel

将网格导出到excel时。我有一个条件,必须根据条件指定Excel工作表的标题(而不是列标题)。

ViewType ='全部' 标题=“所有供应商合同”

ViewType ='已上传' 标题=“所有已签署的供应商合同”

ViewType ='未上传' 标题=“所有非Sigend供应商合同”

        if (!Directory.Exists(targetDirectory))
        {
            Directory.CreateDirectory(targetDirectory);
        }

        var filename = "VendorContract" + DateTime.Now.ToShortDateString().Replace('/', '_') + ".xlsx";
        var savePath = Path.Combine(targetDirectory, filename);
        var loadURL = _configuration["BManageUrl"] + uploadPath + filename;
        ViewType = (ViewType == null) ? "All" : ViewType;
        var VendorDocumentList = _bsDocumentService.GetDocumentVendorContractList(ContractNumber, CurrentUserDetails.SellerID, ViewType);
        ExcelHelper.ListToExcel<DocVendorContractViewModel>(VendorDocumentList, savePath);

        return Ok(loadURL);
    }

Excel中的预期输出(如果视图类型为全部)

所有供应商合同//这是标题

数字ID名称//此3是列的标题

(1)(22)(acb)//这3个是数据

(2)(23)(asdasd)

1 个答案:

答案 0 :(得分:0)

您的问题对我来说并不完全清楚,但是您是否正在寻找switch

        switch (ViewType)
        {
            case "All":
                Title = "All Vendor Contract";
                break;
            case "Uploaded":
                Title = "All Signed Vendor Contract";
                break;
            case "Not Uploaded":
                Title = "All UnSigned Vendor Contract";
                break;
            default:
                Title = "Wrong ViewType";
                break;
        }

或者您可以使用

做同样的事情
        if (ViewType == "All")
        {
            Title = "All Vendor Contract";
        }
        else if (ViewType == "Uploaded")
        {
            Title = "All Signed Vendor Contract";
        }
        else if (ViewType == "Not Uploaded")
        {
            Title = "All UnSigned Vendor Contract";
        }
        else
        {
            Title = "Wrong ViewType";
        }

这是一个品味问题。

我无法帮助您创建Excel工作表,因为我不知道

    var VendorDocumentList = _bsDocumentService.GetDocumentVendorContractList(ContractNumber, CurrentUserDetails.SellerID, ViewType);
    ExcelHelper.ListToExcel<DocVendorContractViewModel>(VendorDocumentList, savePath);

这是非公开代码,因此除非您发布此代码,否则您必须自己找到它。