首页 > 解决方案 > C# MVC 从 DB 导出到 excel 。合并单元格

问题描述

我知道这个问题有很多主题,但我的要求更具体。我正在使用 EF 将记录选择到我的项目中,然后将它们导出到 Excel。我用过这个片段代码

让我解释一下我想要做什么。给定下表(这是为了简化,您将在代码中看到该表有点大):

Name | Content
 A     "Content1"
 A     "Content2"
 A     "Content3"
 B     "other content"
 ......

当我导出到 excel 时,我不想A在每个内容旁边出现 3 次,我希望只有一个“A”(我能够做到)并合并 3 个单元格(将它们对齐到中心如果可能的话也一样)成一个(不接触Content柱子)。

这是我的代码:

    public IActionResult Index()
    {
        var knownCampaigns = _repository.getDataForExport();
        //return View(result);
        string sWebRootFolder = _hostingEnvironment.WebRootPath;
        string sFileName = @"demo.xlsx";
        string URL = string.Format("{0}://{1}/{2}", Request.Scheme, Request.Host, sFileName);
        FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
        if (file.Exists)
        {
            file.Delete();
            file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
        }
        using (ExcelPackage package = new ExcelPackage(file))
        {
            // add a new worksheet to the empty workbook
            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("CampaignMatches");
            //First add the headers
            worksheet.Cells[1, 2].Value = "Customer Name";
            worksheet.Cells[1, 3].Value = "Guid";
            worksheet.Cells[1, 4].Value = "Campaign Title";
            worksheet.Cells[1, 5].Value = "Referrer Title";
            worksheet.Cells[1, 6].Value = "Activity Date";
            worksheet.Cells[1, 7].Value = "Is clicked?";
            int counter = 2;
            string oldGuid = "";
            foreach (var campaign in knownCampaigns)
            {
                if (oldGuid == campaign.Guid || worksheet.Cells["C" + (counter - 1)].Value.ToString() == campaign.Guid)
                {
                    oldGuid = campaign.Guid;
                    worksheet.Cells["A" + counter].Value = "";
                    worksheet.Cells["B" + counter].Value = "";
                }
                else
                {
                    oldGuid = "";
                    worksheet.Cells["A" + counter].Value = campaign.customerName;
                    worksheet.Cells["B" + counter].Value = campaign.Guid;
                }
                worksheet.Cells["C" + counter].Value = campaign.campaignTitle;
                worksheet.Cells["D" + counter].Value = campaign.reffererTitle;
                worksheet.Cells["E" + counter].Value = campaign.activityDate;
                worksheet.Cells["F" + counter].Value = campaign.is_clicked;
                counter++;
            }
            package.Save(); //Save the workbook.

        }
        var result = PhysicalFile(Path.Combine(sWebRootFolder, sFileName), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        Response.Headers["Content-Disposition"] = new ContentDispositionHeaderValue("attachment")
        {
            FileName = file.Name
        }.ToString();
        return result;
    }

现在,我的Customer NameandGuid列只按预期出现一次,但我不知道如何将单元格合并到一个单元格中。

当前输出的图像:

[![在此处输入图像描述][2]][2]

想要输出的图像:

[![在此处输入图像描述][3]][3]

标签: c#excelentity-frameworkasp.net-core

解决方案


如果有人会遇到同样的问题,我已经设法使用Merge属性解决了这个问题,我必须提取开始列索引、行索引和结束行索引和结束列索引的位置。

 var Wsc_Guid = worksheet.Cells[startRowIndex, guidIndex, startRowIndex + numOfSameRecords, guidIndex];//Select the correct cells for Guids
 Wsc_Guid.Merge = true;

推荐阅读