title: click button in the excel file using C#
body: <p>I have excel file and it has export button in the header. When we open excel and click on export a macro runs in background and exports excel to some location.</p>
  
  <p>I am writing C# winform application which is wrapper over this excel file. This winform application has Export button. When user clicks on winform button C# code should open the excel file and programmatically click export button in the excel file and file is exported.</p>
  
  <p>I am using OLEDBConnection to connect the excel file.  How to click button in the excel file using C#?</p>
  
  <p>I have code to to read data from excel. </p>
  
  <pre><code>private void CreateConnectionString()
          {
              ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file + ";Extended Properties="
                      + "\"Excel 12.0 Xml;HDR=YES;IMEX=3;READONLY=FALSE\"";
          }
  
  private void ReadDataFromExcelAndShowInGrid()
          {
              DataSet dataSet = new DataSet();
              string sql = "Select * From [TimeSheet$]";
              using (OleDbConnection cn = new OleDbConnection(ConnectionString))
              {
                  using (OleDbDataAdapter adapter = new OleDbDataAdapter(sql, cn))
                  {
                      adapter.Fill(dataSet, "Data");
                  }
              }
  
              DataRowCollection dataRows = dataSet.Tables["Data"].Rows;
              for (int i = 0; i &lt; dataRows.Count; ++i)
              {
                  string cDateTime = ConvertAndReturnStringToDateTime(dataRows[i].ItemArray[0].ToString(), "MM-dd-yyyy");
                  if (cDateTime != string.Empty)
                  {
                      if (EndOfWeek.Equals(cDateTime))
                      {
                          var date = ConvertAndReturnStringToDateTime(dataRows[i].ItemArray[1].ToString(), "dd-MM-yyyy");
                          var start = ConvertAndReturnStringToDateTime(dataRows[i].ItemArray[2].ToString(), "dd-MM-yyyy hh:mm:ss");
                          var stop = ConvertAndReturnStringToDateTime(dataRows[i].ItemArray[3].ToString(), "dd-MM-yyyy hh:mm:ss");
                          table.Rows.Add(cDateTime, date,
                              start, stop,
                              dataRows[i].ItemArray[4].ToString(), dataRows[i].ItemArray[5].ToString(),
                              dataRows[i].ItemArray[6].ToString(), dataRows[i].ItemArray[7].ToString(),
                              dataRows[i].ItemArray[8].ToString());
                      }
                  }
              }
          }
  private string ConvertAndReturnStringToDateTime(string dateTimeFromFile, string dateFormat)
          {
              string dateTime = string.Empty;
  
              double fileDateweek = 0.0d;
              if (double.TryParse(dateTimeFromFile, out fileDateweek))
              {
                  dateTime = DateTime.FromOADate(fileDateweek).ToString(dateFormat);
              }
              return dateTime;
          }
  </code></pre>