title: Add new sheet in Excel from windows application c#
body: <p>Hi i want to add new sheet and populate data from data-table into excel from windows application c#.If If the excel is already existing i have to create a new sheet for the same excel and get data from db using datatable and save. while adding new sheet i get typemismatch error
  <a href="https://imgur.com/6zlSub9" rel="nofollow noreferrer">https://imgur.com/6zlSub9</a>
  Please help. Code attached.</p>
  <pre><code>SqlConnection con = new SqlConnection();
                  SqlCommand cmd = new SqlCommand(&quot;select * from tblBrand&quot;, con);
                  con.Open();
                  try
                  {
                      SqlDataAdapter sda = new SqlDataAdapter();
                      sda.SelectCommand = cmd;
                      System.Data.DataTable dt = new System.Data.DataTable();
                      DataSet ds = new DataSet(&quot;New_data&quot;);
                      ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
                      sda.Fill(dt);
                      ds.Tables.Add(dt);
                      string folder = Directory.GetParent(Directory.GetCurrentDirectory()).FullName + &quot;\\OutPut\\&quot;;
                      using (XLWorkbook wb = new XLWorkbook())
                      {
                          if(System.IO.File.Exists(folder + &quot;sample.xlsx&quot;))
                          {
                              Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                              app.DisplayAlerts = false;
                              app.DefaultSaveFormat = XlFileFormat.xlOpenXMLWorkbook;
                              Excel.Workbook xwb = app.Workbooks.Open(folder + &quot;sample.xlsx&quot;, 0, false, 5, &quot;&quot;, &quot;&quot;, false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, &quot;&quot;, true, false, 0, true, false, false);
                              Excel.Sheets sheet = xwb.Worksheets;
                              var xlNewSheet = (Excel.Worksheet)sheet.Add(sheet[1], Type.Missing, Type.Missing, Type.Missing);
                              xlNewSheet.Name = &quot;newsheet&quot;;
                              // xlNewSheet.Cells[0, 0] = xwb.Worksheets.Add(ds);
                              //xlNewSheet = xwb.Worksheets.Add(dt);
                              xlNewSheet = (Excel.Worksheet)xwb.Worksheets.Add(dt);
                              xlNewSheet = (Excel.Worksheet)xwb.Worksheets.get_Item(1);
                              xlNewSheet.Select();
                              xwb.Save();
                              xwb.Close();
                              releaseObject(xlNewSheet);
                              releaseObject(sheet);
                              releaseObject(xwb);
                              releaseObject(app);
                             
                          }
                          else
                          {
                              wb.Worksheets.Add(dt, &quot;Brands&quot;);
                              wb.SaveAs(folder + &quot;sample.xlsx&quot;);
                          }
  
                      }
                      con.Close();
                  }
                  catch (Exception ec)
                  {
                      MessageBox.Show(ec.Message);
                  }
  </code></pre>