Kalyani Shevale

Kalyani Shevale

  • NA
  • 3.2k
  • 711.5k

Existing spreadsheet document file create new cell in c#

Nov 21 2018 1:23 AM
I have open existing spreadsheet document file. then this file inserts some cell in the particular index value.
how to insert the new cell in this open file.
  1. DataTable dataTable = new DataTable();  
  2.                using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(srcFile, false))  
  3.                {  
  4.                    WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;  
  5.                    IEnumerable<Sheet> sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();  
  6.                    string relationshipId = sheets.First().Id.Value;  
  7.                    WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(relationshipId);  
  8.                    Worksheet workSheet = worksheetPart.Worksheet;  
  9.                    SheetData sheetData = workSheet.GetFirstChild<SheetData>();  
  10.                    IEnumerable<Row> rows = sheetData.Descendants<Row>();  
  11.   
  12.                    foreach (Cell cell in rows.ElementAt(0))  
  13.                    {  
  14.                        dataTable.Columns.Add(GetCellValue(spreadSheetDocument, cell));  
  15.                          
  16.                    }  
  17.                    ArrayList list = new ArrayList();  
  18.                    foreach (Row row in rows)  
  19.                    {  
  20.                        DataRow dataRow = dataTable.NewRow();  
  21.                        for (int i = 0; i < 1; i++)  
  22.                        {  
  23.                                    list.Add(GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i)));  
  24.                        }  
  25.                    }  
  26.                     Row row1 = new Row() { RowIndex = 2U, Spans = new ListValue<StringValue>() };  
  27.                    Cell cell1 = new Cell()  
  28.                    {  
  29.                        CellReference = "A52",  
  30.                        DataType = CellValues.String,  
  31.                        CellValue = new CellValue("Microsoft")  
  32.                    };  
  33.                    Row r = new Row();  
  34.                    r.Append(cell1);  
  35.                    sheetData.AppendChild(r);  
  36.                    worksheetPart.Worksheet.Append(sheetData);  
  37.                    worksheetPart.Worksheet.Save();  
  38.                     
  39.                }  
  40.                  
 problem in last 5 line how to insert new cell value in this existing file in c#

Answers (5)