title: C# MVC Export from DB to excel . Merge cells
body: <p>I know there are many topics on the issue, but my requirements are more specific.. I'm using EF to select records into my project, and then export them to Excel. I've used <a href="https://stackoverflow.com/questions/43136185/asp-net-core-return-excel-file-xlsx-directly-in-one-call-to-the-server-on-the?fireglass_rsn=true#fireglass_params%7C&amp;tabid=acc122ddd13a169b&amp;application_server_address=fg-vb2.dmz.local&amp;popup=true&amp;is_right_side_popup=false&amp;start_with_session_counter=1">This snippet code</a>.</p>
  
  <p>Let me explain what I'm trying to do. Given the following table(It's for simplification, as you will see in the code the table is a bit larger):</p>
  
  <pre><code>Name | Content
   A     "Content1"
   A     "Content2"
   A     "Content3"
   B     "other content"
   ......
  </code></pre>
  
  <p>When I export to excel, I don't want <code>A</code> to appear 3 times next to each content, I'd like to have only one "A" (which I was able to do) and merge the 3 cells (align them to the center too if possible) into one(without touching the <code>Content</code> column) . </p>
  
  <p>This is my code:</p>
  
  <pre><code>    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;
      }
  </code></pre>
  
  <p>Right now, my <code>Customer Name</code> and <code>Guid</code> column only appears once as intended, but I don't know how to merge the cells together into one cell.</p>
  
  <p>Image of current output:</p>
  
  <p>[![enter image description here][2]][2]</p>
  
  <p>Image of wanted output:</p>
  
  <p>[![enter image description here][3]][3]</p>