C# Merge Cells where values are the same

Viewed 175

Goal:

I am aiming to Merge cells that are the same values in specific columns.

Already have code that exports data from MySQL into Excel, just need a final touch.

Current Code:

System.Data.DataTable dt = new System.Data.DataTable();
using(MySqlConnection conn = new MySqlConnection(ConnectionString.ConnString))
{
    conn.Open();
    using (MySqlDataAdapter da = new MySqlDataAdapter(@"SELECT b.productcode, b.ProductDescription, a.AreaName, b.MinimumMonths, LAST_DAY(DATE_ADD(NOW(), INTERVAL b.MinimumMonths MONTH)) AS BestBefore  FROM bestbeforedates b join area a on b.areaid = a.id;", conn))
    {
        da.Fill(dt);

        var excelApp = new Excel.Application();
        excelApp.Workbooks.Add();
        Excel._Worksheet worksheet = (Excel.Worksheet)excelApp.ActiveSheet;

        // Set all cells to text
        worksheet.Cells.NumberFormat = "@";

        worksheet.Cells[1, "A"] = "Product Code";
        worksheet.Cells[1, "B"] = "Product Description";
        worksheet.Cells[1, "C"] = "Area";
        worksheet.Cells[1, "D"] = "Minimum Months";
        worksheet.Cells[1, "E"] = "Best Before Date";

        int headerRow = 1;
        int startCol = 1;
        int maxCol = 6;
        for (int i = startCol; i < maxCol; i++)
        {
            Excel.Range currentRange = (Excel.Range)worksheet.Cells[headerRow, i];
            currentRange.Font.Bold = true;
        }


        var row = 1;
        foreach (DataRow allRow in dt.Rows)
        {
            row++;
            worksheet.Cells[row, "A"] = allRow["ProductCode"];
            worksheet.Cells[row, "B"] = allRow["ProductDescription"];
            worksheet.Cells[row, "C"] = allRow["AreaName"];
            worksheet.Cells[row, "D"] = allRow["MinimumMonths"];
            worksheet.Cells[row, "E"] = allRow["BestBefore"];
            worksheet.Columns.AutoFit();
        }

        Cursor.Current = Cursors.Default;
        // Show excel app
        excelApp.Visible = true;

    }
}

Output

enter image description here

Desired Output

enter image description here

Question

How can I merge these cells if the values are same?

Edit:

This is where I got so far.. it does not merge the required cells.. but looking into why it isn't.

var row = 1;

DataRow prevRow = dt.NewRow();

foreach (DataRow allRow in dt.Rows)
{
    row++;
    worksheet.Cells[row, "A"] = allRow["ProductCode"];
    worksheet.Cells[row, "B"] = allRow["ProductDescription"];
    worksheet.Cells[row, "C"] = allRow["AreaName"];
    worksheet.Cells[row, "D"] = allRow["MinimumMonths"];
    worksheet.Cells[row, "E"] = allRow["BestBefore"];                           
    worksheet.Columns.AutoFit();

    
    if(allRow["MinimumMonths"].ToString() == prevRow["MinimumMonths"].ToString())
    {
        worksheet.Range[worksheet.Cells[row, "D"], worksheet.Cells[row, "D"]].Merge();
    }
    else
    {
        
    }

    prevRow = allRow;

}
1 Answers

Give the following a try:

if (row > 2 && allRow["MinimumMonths"].ToString() == prevRow["MinimumMonths"].ToString())
{
    worksheet.Range[worksheet.Cells[row - 1, "D"], worksheet.Cells[row, "D"]].Merge();
}

Here's the complete code:

var row = 1;

DataRow prevRow = dt.NewRow();
foreach (DataRow allRow in dt.Rows)
{

    row++;
    worksheet.Cells[row, "A"] = allRow["ProductCode"];
    worksheet.Cells[row, "B"] = allRow["ProductDescription"];
    worksheet.Cells[row, "C"] = allRow["AreaName"];
    worksheet.Cells[row, "D"] = allRow["MinimumMonths"];
    worksheet.Cells[row, "E"] = allRow["BestBefore"];
    worksheet.Columns.AutoFit();


    if (row > 2 && allRow["MinimumMonths"].ToString() == prevRow["MinimumMonths"].ToString())
    {
        worksheet.Range[worksheet.Cells[row - 1, "D"], worksheet.Cells[row, "D"]].Merge();
    }

    //set value
    prevRow = allRow;
}

If you don't have a need to use Excel, but rather just need to create an Excel workbook, you may want to consider using DocumentFormat.OpenXml .

Related