title: To create an spreadsheet document in OPEN XML and and add an TABLE to it
body: <p>I am trying to create an spread sheet by <strong>open xml</strong> with an <strong>table</strong>. But while opening the created excel file always it returns the error "Excel found unreadable content in test1.xlsx, do u want to recover. Why this error always happens? When the excel is created without table, i didnt get any error. Please find the below sample code which i used to create.</p>
  
  <pre><code>            using (SpreadsheetDocument spreadsheetdocument = SpreadsheetDocument.Create(@"c://test.xlsx", SpreadsheetDocumentType.Workbook))
          {
              // add a workbookpart to the document.
              WorkbookPart workbookpart = spreadsheetdocument.AddWorkbookPart();
              workbookpart.Workbook = new Workbook();
  
              // add a worksheetpart to the workbookpart.
              WorksheetPart worksheetpart = workbookpart.AddNewPart&lt;WorksheetPart&gt;();
              worksheetpart.Worksheet = new Worksheet(new SheetData());
  
              // add sheets to the workbook.
              Sheets sheets = spreadsheetdocument.WorkbookPart.Workbook.
                  AppendChild&lt;Sheets&gt;(new Sheets());
  
              // append a new worksheet and associate it with the workbook.
              Sheet sheet = new Sheet()
              {
                  Id = spreadsheetdocument.WorkbookPart.
                      GetIdOfPart(worksheetpart),
                  SheetId = 1,
                  Name = "mysheet"
              };
              sheets.Append(sheet);
  
               TableDefinitionPart tableDefinitionPart = worksheetpart.AddNewPart&lt;TableDefinitionPart&gt;("rId1");
  
              GenerateTablePartContent(ref tableDefinitionPart);
  
              TableParts tableParts1 = new TableParts() { Count = (UInt32Value)2U };
              TablePart tablePart1 = new TablePart() { Id = "rId1" };
              tableParts1.Append(tablePart1);
              worksheetpart.Worksheet.Append(tableParts1);
               spreadsheetdocument.Close();
  } 
  //Added spaces to get the code in one block
   private void GenerateTablePartContent(TableDefinitionPart part)
      {
          Table table1 = new Table() { Id = (UInt32Value)1U, Name = "Table1", DisplayName = "Table1", Reference = "A1:C3", TotalsRowShown = false };
          AutoFilter autoFilter1 = new AutoFilter() { Reference = "A1:C3" };
  
          TableColumns tableColumns1 = new TableColumns() { Count = (UInt32Value)3U };
          TableColumn tableColumn1 = new TableColumn() { Id = (UInt32Value)1U, Name = "Column1" };
          TableColumn tableColumn2 = new TableColumn() { Id = (UInt32Value)2U, Name = "Column2" };
          TableColumn tableColumn3 = new TableColumn() { Id = (UInt32Value)3U, Name = "Column3" };
  
          tableColumns1.Append(tableColumn1);
          tableColumns1.Append(tableColumn2);
          tableColumns1.Append(tableColumn3);
          TableStyleInfo tableStyleInfo1 = new TableStyleInfo() { Name = "TableStyleMedium2", ShowFirstColumn = false, ShowLastColumn = false, ShowRowStripes = true, ShowColumnStripes = false };
  
          table1.Append(autoFilter1);
          table1.Append(tableColumns1);
          table1.Append(tableStyleInfo1);
  
          part.Table = table1;
      }
  </code></pre>