Introduction

This code can be used to add updated rows and cell values in an Excel sheet (.xlsx) using open XML and C#.
For that, you need to add open XML to your application. Please follow below steps:
Go to add references -> Right-click on it - > Click on manage nuget packages -> Browse open XML -> You will see DocumentFormat.OpenXml -> Click on install.
Code
  1. public void UpdateExcelSheetData() {
  2. string fileName = @ "file name with .xlsx extension and file path ";
  3. using(SpreadsheetDocument spreadSheet =
  4. SpreadsheetDocument.Open(fileName, true)) {
  5. AddUpdateCellValue(spreadSheet, "test sheet1", 8, "A", "test data1");
  6. AddUpdateCellValue(spreadSheet, "test sheet2", 8, "B", "test data2");
  7. AddUpdateCellValue(spreadSheet, "test sheet3", 8, "A", "test data3");
  8. }
  9. }
  10. public void AddUpdateCellValue(SpreadsheetDocument spreadSheet, string sheetname,
  11. uint rowIndex, string columnName, string text) {
  12. // Opening document for editing
  13. WorksheetPart worksheetPart =
  14. RetrieveSheetPartByName(spreadSheet, sheetname);
  15. if (worksheetPart != null) {
  16. Cell cell = InsertCellInSheet(columnName, (rowIndex + 1), worksheetPart);
  17. cell.CellValue = new CellValue(text);
  18. //cell datatype
  19. cell.DataType =
  20. new EnumValue < CellValues > (CellValues.String);
  21. // Save the worksheet.
  22. worksheetPart.Worksheet.Save();
  23. }
  24. }
  25. //retrieve sheetpart
  26. public WorksheetPart RetrieveSheetPartByName(SpreadsheetDocument document,
  27. string sheetName) {
  28. IEnumerable < Sheet > sheets =
  29. document.WorkbookPart.Workbook.GetFirstChild < Sheets > ().
  30. Elements < Sheet > ().Where(s => s.Name == sheetName);
  31. if (sheets.Count() == 0)
  32. return null;
  33. string relationshipId = sheets.First().Id.Value;
  34. WorksheetPart worksheetPart = (WorksheetPart)
  35. document.WorkbookPart.GetPartById(relationshipId);
  36. return worksheetPart;
  37. }
  38. //insert cell in sheet based on column and row index
  39. public Cell InsertCellInSheet(string columnName, uint rowIndex, WorksheetPart worksheetPart) {
  40. Worksheet worksheet = worksheetPart.Worksheet;
  41. SheetData sheetData = worksheet.GetFirstChild < SheetData > ();
  42. string cellReference = columnName + rowIndex;
  43. Row row;
  44. //check whether row exist or not
  45. //if row exist
  46. if (sheetData.Elements < Row > ().Where(r => r.RowIndex == rowIndex).Count() != 0)
  47. row = sheetData.Elements < Row > ().Where(r => r.RowIndex == rowIndex).First();
  48. //if row does not exist then it will create new row
  49. else {
  50. row = new Row() {
  51. RowIndex = rowIndex
  52. };
  53. sheetData.Append(row);
  54. }
  55. //check whether cell exist or not
  56. //if cell exist
  57. if (row.Elements < Cell > ().Where(c => c.CellReference.Value == columnName + rowIndex).Count() > 0)
  58. return row.Elements < Cell > ().Where(c => c.CellReference.Value == cellReference).First();
  59. //if cell does not exist
  60. else {
  61. Cell refCell = null;
  62. foreach(Cell cell in row.Elements < Cell > ()) {
  63. if (string.Compare(cell.CellReference.Value, cellReference, true) > 0) {
  64. refCell = cell;
  65. break;
  66. }
  67. }
  68. Cell newCell = new Cell() {
  69. CellReference = cellReference
  70. };
  71. row.InsertBefore(newCell, refCell);
  72. worksheet.Save();
  73. return newCell;
  74. }
  75. }
  76. // retrieve cell based on column and row index
  77. public Cell RetreiveCell(Worksheet worksheet,
  78. string columnName, uint rowIndex) {
  79. Row row = RetrieveRow(worksheet, rowIndex);
  80. var newRow = new Row() {
  81. RowIndex = (uint) rowIndex + 1
  82. };
  83. //adding new row
  84. worksheet.InsertAt(newRow, Convert.ToInt32(rowIndex + 1));
  85. //create cell with value
  86. Cell cell = new Cell();
  87. cell.CellValue = new CellValue("");
  88. cell.DataType =
  89. new EnumValue < CellValues > (CellValues.String);
  90. newRow.AddAnnotation(cell);
  91. worksheet.Save();
  92. row = newRow;
  93. if (row == null)
  94. return null;
  95. return row.Elements < Cell > ().Where(c => string.Compare(c.CellReference.Value, columnName +
  96. (rowIndex + 1), true) == 0).First();
  97. }
  98. // it will return a row based on worksheet and rowindex
  99. public Row RetrieveRow(Worksheet worksheet, uint rowIndex) {
  100. return worksheet.GetFirstChild < SheetData > ().
  101. Elements < Row > ().Where(r => r.RowIndex == rowIndex).First();
  102. }