Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

How to insert the row in exisiting template in open xml

$
0
0
I have a scenario to generate the Excel sheet based on the existing template, pls refere the attached screen short, here i have two rows, but if i want to display the records in more than 2 ros then i want to insert the row dynamically, but i tried to do that, it's nto working, please any body help me to solve this problem
public void InsertRow(string sheetName,WorkbookPart wbPart, uint rowIndex)
        {
            Sheet sheet = wbPart.Workbook.Descendants<Sheet>().Where((s) => s.Name == sheetName).FirstOrDefault();

            if (sheet != null)
            {
                
                Worksheet ws = ((WorksheetPart)(wbPart.GetPartById(sheet.Id))).Worksheet;
                SheetData sheetData = ws.WorksheetPart.Worksheet.GetFirstChild<SheetData>();
                Row refRow = GetRow(sheetData, rowIndex);++rowIndex;
                Row newRow = new Row()
                {
                    RowIndex = rowIndex
                };
                sheetData.InsertAfter(newRow, refRow);
                //sheetData.InsertAfterSelf(refRow);
                ws.Save();
            }
        }

private Row GetRow(SheetData wsData, UInt32 rowIndex)
        {
            var row = wsData.Elements<Row>().
            Where(r => r.RowIndex.Value == rowIndex).FirstOrDefault();
            if (row == null)
            {
                row = new Row();
                row.RowIndex = rowIndex;
                wsData.Append(row);
            }
            return row;
        }

 

Viewing all articles
Browse latest Browse all 1288

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>