Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

My requiremnet is to merge three cell of excel. I am using Open XMl for excel.
Another requirement is to delete a particular column from the SheetData
Below is the method which i am using to generate the excel file.
Please go through the code. There i mention my requirement as comments.

Any help will be highly appreciated.

C#
public static void AddToMultipleSheets(WorksheetPart worksheetPart, WorkbookPart workbookPart, DataTable data, int startRow)
{
    Worksheet worksheet = worksheetPart.Worksheet;
    string Id = workbookPart.GetIdOfPart(worksheetPart);
    SheetData sheetData = worksheet.Descendants<SheetData>().FirstOrDefault();
    UInt32Value rowIndex = (UInt32Value)((UInt32)startRow);

    for (int rIndex = 0; rIndex < data.Rows.Count; rIndex++)
    {
        Row row = new Row()
            {
                RowIndex = rowIndex++,
                Spans = new ListValue<StringValue>() { InnerText = "1:3" },
                DyDescent = 0.25D
            };

        for (int cIndex = 0; cIndex < data.Columns.Count; cIndex++)
        {
            Cell cell = new Cell()
            {
                CellReference = ColumnCaption.Instance.Get((Convert.ToInt32((UInt32)rowIndex) - 2), cIndex),
                DataType = CellValues.String
            };
            CellFormat cellFormat = new CellFormat();
            CellValue cellValue = new CellValue();
            cellValue.Text = data.Rows[rIndex][cIndex].ToString();
            // My Requirement is to merge three adjacent cells.
            // If suppose my last column is "J" i need to merge J,K,L and value to placed in the merged cell
            cell.Append(cellValue);
            row.Append(cell);  
        }
        sheetData.Append(row);
        // Other requirment is to delete one column from the sheet data, say "M" Column

    }           
}
Posted

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900