【问题标题】:C# MVC Export from DB to excel . Merge cellsC# MVC 从 DB 导出到 excel 。合并单元格
【发布时间】:2019-02-21 07:14:32
【问题描述】:

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

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

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 NameGuid 列仅按预期出现一次,但我不知道如何将单元格合并为一个单元格。

当前输出的图片:

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

想要输出的图片:

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

【问题讨论】:

    标签: c# excel entity-framework asp.net-core


    【解决方案1】:

    看起来不是那么明显。看起来工作表上有一个 Elements 属性,您可以添加 MergedCell 的类型。 https://docs.microsoft.com/en-us/office/open-xml/how-to-merge-two-adjacent-cells-in-a-spreadsheet

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-17
        • 1970-01-01
        • 2016-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-12
        • 1970-01-01
        相关资源
        最近更新 更多