'How to merge Excel column with dynamic value
I trying to merge the excel column when there is same value in the first 2 row. The value is coming from my database query. Currently what I had generated is like this:
I want to merge the column like this:
Here is my function code:
public static async Task<MemoryStream> getNewListingExcelWithContentColumnMerge<T>(List<T> list, List<string> header, string name, string headerTitle)
{
try
{
int row = 3;
int col = 1;
MemoryStream output = new MemoryStream();
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet ws = package.Workbook.Worksheets.Add(name);
ws.PrinterSettings.PaperSize = ePaperSize.A4;
ws.PrinterSettings.FitToPage = true;
ws.Cells["A1:D1"].Merge = true;
ws.Cells["A1:D1"].Value = headerTitle;
for (int i = 0; i < header.Count(); i++)
{
ws.Cells[row, col].Value = header[i];
col++;
}
ws.Cells[1, 1, 1, col].Style.Font.Bold = true;
col = 1;
row++;
for (int i = 0; i < list.Count(); i++)
{
Type type = list[i].GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
object asd = property.GetValue(list[i], null);
//Add data to the next line if consist of '\n'.
if (asd != null && asd.ToString().Contains(Environment.NewLine))
{
var splitValue = asd.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
var sameValuePlacementList = new List<int>();
for (var j = 0; j < splitValue.Count(); j++)
{
if (j == 0)
{
ws.Cells[row, col].Value = splitValue[j];
ws.Cells[row, col].Style.WrapText = true;
}
else
{
ws.Cells[row, col].RichText.Add(Environment.NewLine + splitValue[j]);
}
}
}
else
{
ws.Cells[row, col].Value = asd;
}
col++;
}
col = 1;
row++;
}
ws.Cells[3, 1, ws.Dimension.End.Row, ws.Dimension.End.Column - 1].AutoFitColumns();
ws.Cells[3, 1, ws.Dimension.End.Row, ws.Dimension.End.Column - 1].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
ws.Cells[3, 1, ws.Dimension.End.Row, ws.Dimension.End.Column - 1].Style.Border.Top.Style = ExcelBorderStyle.Thin;
ws.Cells[3, 1, ws.Dimension.End.Row, ws.Dimension.End.Column - 1].Style.Border.Right.Style = ExcelBorderStyle.Thin;
ws.Cells[3, 1, ws.Dimension.End.Row, ws.Dimension.End.Column - 1].Style.Border.Left.Style = ExcelBorderStyle.Thin;
package.SaveAs(output);
return output;
}
}
catch (Exception ex)
{
return null;
}
}
What can I try next?
Solution 1:[1]
int tempRow = startRowForMerge;
int firstSameRowIndex = 0;
int lastSameRowIndex = 0;
int maxCount = tempRow + list.Count() - 1; // -1 because array start from 0
bool needToMerge = false;
foreach(int tempColumn in mergeColumnList)
{
tempRow = startRowForMerge;
for (int i = 0; i < list.Count(); i++)
{
if (tempRow < maxCount)
{
if (row > 4 && ws.Cells[tempRow, tempColumn].Value.ToString() == ws.Cells[tempRow - 1, tempColumn].Value.ToString())
{
needToMerge = true;
if (firstSameRowIndex == 0)
{
firstSameRowIndex = tempRow - 1;
}
lastSameRowIndex = tempRow;
}
else
{
if (needToMerge == true) // to merge data in between the list
{
ws.Cells[firstSameRowIndex, tempColumn, lastSameRowIndex, tempColumn].Merge = true;
ws.Cells[firstSameRowIndex, tempColumn, lastSameRowIndex, tempColumn].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
needToMerge = false;
firstSameRowIndex = 0;
lastSameRowIndex = 0;
}
}
}
else
{
if (needToMerge == true) // to merge data in last row
{
ws.Cells[firstSameRowIndex, tempColumn, lastSameRowIndex, tempColumn].Merge = true;
ws.Cells[firstSameRowIndex, tempColumn, lastSameRowIndex, tempColumn].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
needToMerge = false;
firstSameRowIndex = 0;
lastSameRowIndex = 0;
}
}
tempRow++;
}
}
Solution 2:[2]
You can do it this way:
ws.Range[ws.Cells[1, 1], ws.Cells[4, 1]].Merge();
It will merge rows from 1 to 4 in column 1.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | wkwkwk |
| Solution 2 | Skundlony |


