title: Update existing excel sheet with openxml and C#
body: <p>I need to export a large amount of data from database to excel. I have created a method to export data from a select..but I managed to create a new excel. Not to update an existing one. I've searched on the internet but I cannot understand what am I doing wrong. </p>
  
  <p>Here is my code for the method:</p>
  
  <pre><code>public static void ExportDSToExcel(DataSet ds, string destination)
      {
          using (var workbook = SpreadsheetDocument.Open(destination, true))
          {
              //var workbookPart = workbook.AddWorkbookPart();
              workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
              workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();
  
              uint sheetId = 1;
  
              foreach (DataTable table in ds.Tables)
              {
                  var sheetPart = workbook.WorkbookPart.AddNewPart&lt;WorksheetPart&gt;();
                  var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
                  sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);
  
                  DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild&lt;DocumentFormat.OpenXml.Spreadsheet.Sheets&gt;();
                  string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);
  
                  if (sheets.Elements&lt;DocumentFormat.OpenXml.Spreadsheet.Sheet&gt;().Count() &gt; 0)
                  {
                      sheetId =
                          sheets.Elements&lt;DocumentFormat.OpenXml.Spreadsheet.Sheet&gt;().Select(s =&gt; s.SheetId.Value).Max() + 1;
                  }
  
                  DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };
                  sheets.Append(sheet);
  
                  DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
  
                  List&lt;String&gt; columns = new List&lt;string&gt;();
                  foreach (DataColumn column in table.Columns)
                  {
                      columns.Add(column.ColumnName);
  
                      DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                      cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                      cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
                      headerRow.AppendChild(cell);
                  }
  
                  sheetData.AppendChild(headerRow);
  
                  foreach (DataRow dsrow in table.Rows)
                  {
                      DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
                      foreach (String col in columns)
                      {
                          DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                          cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                          cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); //
                          newRow.AppendChild(cell);
                      }
  
                      sheetData.AppendChild(newRow);
                  }
              }
  </code></pre>
  
  <p>And here is where I call the method:</p>
  
  <pre><code>string _oracleConnection = "Data Source=XXXXX;Persist Security Info=True;User ID=XXXXXX;Password=YYYYY";
  
          OracleConnection con = new OracleConnection(_oracleConnection);
          using (con = new OracleConnection(_oracleConnection))
          {
              using (OracleCommand cmd = new OracleCommand())
              {
                  cmd.Connection = con;
                  cmd.CommandType = CommandType.Text;
  
                   cmd.CommandText = "SELECT connect_by_root proj_catg_name, parent_proj_catg_id,LTRIM(SYS_CONNECT_BY_PATH (proj_catg_short_name, '-'),'-') id_path, proj_catg_name FROM pcatval start with  proj_catg_name in ('XXXXXXXXXXXXX', 'XXXXXXXXXXXXXX') CONNECT BY PRIOR proj_catg_id = parent_proj_catg_id ";
  
                  con.Open();
                  DataSet ds = new System.Data.DataSet();
                  OracleDataAdapter oda = new OracleDataAdapter(cmd);
                  oda.Fill(ds);
                  string destination = @"D:\TEST.xlsx";
                  P_IAP.Excelfile.ExportDSToExcel(ds, destination);
              }
  
              Excelfile excelfile = new Excelfile();
  </code></pre>
  
  <p>Do you have any idea what should I change? Lets say that I wanna update an excel file named TEST and the sheet called Sheet1</p>