c# - 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 Name
andGuid
列只按预期出现一次,但我不知道如何将单元格合并到一个单元格中。
当前输出的图像:
[![在此处输入图像描述][2]][2]
想要输出的图像:
[![在此处输入图像描述][3]][3]
解决方案
如果有人会遇到同样的问题,我已经设法使用Merge
属性解决了这个问题,我必须提取开始列索引、行索引和结束行索引和结束列索引的位置。
var Wsc_Guid = worksheet.Cells[startRowIndex, guidIndex, startRowIndex + numOfSameRecords, guidIndex];//Select the correct cells for Guids
Wsc_Guid.Merge = true;
推荐阅读
- spring - 如何创建具有 3 个或更多不同客户端来源的 Mono?
- javascript - 使用 lodash 比较两个对象数组,不包括属性
- reactjs - 如何在 React 组件中等待 Redux 状态更新?
- python - OpenCV无缝克隆闪烁
- c++ - 预构建 grpc 和 find_package
- spring - Spring security BasicAuthenticationFilter 返回 403 而不是 401
- javascript - 在Javascript中使用字符串数组对对象数组进行排序
- angular - 在 ngx 日期范围选择器中编辑清除按钮方法
- selenium - org.openqa.selenium.TimeoutException:使用 titleContains() 和 Selenium 4.0.0-alpha-5 和 Java 11 提供的函数可能已停止错误
- php - 学说计数加入,分组选择