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.
- DataTable dataTable = new DataTable();
- using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(srcFile, false))
- {
- WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
- IEnumerable<Sheet> sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();
- string relationshipId = sheets.First().Id.Value;
- WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(relationshipId);
- Worksheet workSheet = worksheetPart.Worksheet;
- SheetData sheetData = workSheet.GetFirstChild<SheetData>();
- IEnumerable<Row> rows = sheetData.Descendants<Row>();
-
- foreach (Cell cell in rows.ElementAt(0))
- {
- dataTable.Columns.Add(GetCellValue(spreadSheetDocument, cell));
-
- }
- ArrayList list = new ArrayList();
- foreach (Row row in rows)
- {
- DataRow dataRow = dataTable.NewRow();
- for (int i = 0; i < 1; i++)
- {
- list.Add(GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i)));
- }
- }
- Row row1 = new Row() { RowIndex = 2U, Spans = new ListValue<StringValue>() };
- Cell cell1 = new Cell()
- {
- CellReference = "A52",
- DataType = CellValues.String,
- CellValue = new CellValue("Microsoft")
- };
- Row r = new Row();
- r.Append(cell1);
- sheetData.AppendChild(r);
- worksheetPart.Worksheet.Append(sheetData);
- worksheetPart.Worksheet.Save();
-
- }
-
problem in last 5 line how to insert new cell value in this existing file in c#