Search code examples
c#excelopenxmlexport-to-excel

How can I can merge the row in multiple times in spreadsheet?


I have to merge the cell multiple times in Spread Sheet through C#.

Below example is the code what I have tried. It Works if only one condition is given if (cellNameWorkOrder == "POSTER: 10% MUST HAVE APPROACH AND CLOSE-UP SHOTS - PHOTO OF EACH CREATIVE") but when I put OR condition the spread sheet syays it's corrupt after download sheets.

if (cellNameWorkOrder == "POSTER: 10% MUST HAVE APPROACH AND CLOSE-UP SHOTS - PHOTO OF EACH CREATIVE" || cellNameWorkOrder == "BULLETINS: 100% CLOSE-UP AND APPROACH OF EACH UNIT")
using (var workbook = SpreadsheetDocument.Create(Savepath, SpreadsheetDocumentType.Workbook))
{
    var workbookPart = workbook.AddWorkbookPart();
    workbook.WorkbookPart.Workbook = new Workbook();
    workbook.WorkbookPart.Workbook.Sheets = new Sheets();
    foreach (DataRow dsrow in table.Rows)
    {
        int innerColIndex = 0;
        rowIndex++;
        Row newRow = new Row();
        foreach (String col in columns)
        {
            Stylesheet stylesheet1 = new Stylesheet();
            Cell cell = new Cell();
            cell.DataType = CellValues.String;
            cell.CellValue = new CellValue(dsrow[col].ToString());
            cell.CellReference = excelColumnNames[innerColIndex] + rowIndex.ToString();

            if (table.TableName == "Work Order Report")
            {

                string cellNameWorkOrder = dsrow[col].ToString();
                if (cellNameWorkOrder == "POSTER: 10% MUST HAVE APPROACH AND CLOSE-UP SHOTS - PHOTO OF EACH CREATIVE" || cellNameWorkOrder == "BULLETINS: 100% CLOSE-UP AND APPROACH OF EACH UNIT")
                {
                    MergeCells mergeCells = new MergeCells();
                    var cellAddress = cell.CellReference;
                    var cellAddressTwo = "I" + rowIndex.ToString();
                    mergeCells.Append(new MergeCell() { Reference = new StringValue(cellAddress + ":" + cellAddressTwo) });
                    sheetPart.Worksheet.InsertAfter(mergeCells, sheetPart.Worksheet.Elements<SheetData>().First());
                }
            }

            newRow.AppendChild(cell);
            innerColIndex++;
        }

        sheetData.AppendChild(newRow);
    }
}

Solution

  • You can only have one MergeCells element in the worksheet (or none). The MergeCells element should contain all of the MergeCell elements that you require.

    Obviously there are quite a few ways of achieving this given the code you already have, here's one of them:

    using (var workbook = SpreadsheetDocument.Create(Savepath, SpreadsheetDocumentType.Workbook))
    {
        var workbookPart = workbook.AddWorkbookPart();
        workbook.WorkbookPart.Workbook = new Workbook();
        workbook.WorkbookPart.Workbook.Sheets = new Sheets();
    
        //declare our MergeCells here
        MergeCells mergeCells = null;
    
        foreach (DataRow dsrow in table.Rows)
        {
            int innerColIndex = 0;
            rowIndex++;
            Row newRow = new Row();
            foreach (String col in columns)
            {
                Stylesheet stylesheet1 = new Stylesheet();
                Cell cell = new Cell();
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(dsrow[col].ToString());
                cell.CellReference = excelColumnNames[innerColIndex] + rowIndex.ToString();
    
                if (table.TableName == "Work Order Report")
                {
                    string cellNameWorkOrder = dsrow[col].ToString();
                    if (cellNameWorkOrder == "POSTER: 10% MUST HAVE APPROACH AND CLOSE-UP SHOTS - PHOTO OF EACH CREATIVE" || cellNameWorkOrder == "BULLETINS: 100% CLOSE-UP AND APPROACH OF EACH UNIT")
                    {
                        if (mergeCells == null)
                            mergeCells = new MergeCells();
    
                        var cellAddress = cell.CellReference;
                        var cellAddressTwo = "I" + rowIndex.ToString();
                        mergeCells.Append(new MergeCell() { Reference = new StringValue(cellAddress + ":" + cellAddressTwo) });
                    }
                }
    
                newRow.AppendChild(cell);
                innerColIndex++;
            }
    
            sheetData.AppendChild(newRow);
        }
        //add the mergeCells to the worksheet if we have any
        if (mergeCells != null)
            sheetPart.Worksheet.InsertAfter(mergeCells, sheetPart.Worksheet.Elements<SheetData>().First());
    }