Click here to Skip to main content
15,891,888 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am saving workbook

workbook.save();

but it is showing saveas dialouge box.
i need to save workbook without showing saveaas dialouge box.
Posted

C#
excel = new Excel.ApplicationClass();
workbooks = excel.Workbooks;
workbook = workbooks.Add(true);
ExportCurrentData(ref excel,dsData, dataSetIndex);
worksheet = (Excel.Worksheet)excel.ActiveSheet;
worksheet.Activate();
excel.saveworkspace();
 
Share this answer
 
private object m_strSampleFolder = "C:\\Projects\\ExcelProject";
       private void btnConvertToExcel_Click(object sender, EventArgs e)
       {

           Excel._Workbook objBook;
           Excel.Workbooks objBooks;
           Excel.Sheets objSheets;
           Excel._Worksheet objSheet;
           Excel.Range range;

           try
           {
               Excel.Application objApp;
               // Instantiate Excel and start a new workbook.
               objApp = new Excel.Application();
               objBooks = objApp.Workbooks;
               objBook = objBooks.Add(Missing.Value);
               objSheets = objBook.Worksheets;
               objSheet = (Excel._Worksheet)objSheets.get_Item(1);

               range = objSheet.get_Range("A1", Missing.Value);
               range = range.get_Resize(c1FlexGrid1.Rows.Count, c1FlexGrid1.Cols.Count);

               string sql = "SELECT * FROM AccessHistory";
               SqlDataAdapter DA = new SqlDataAdapter(sql, Connstr);
               DataSet ds = new DataSet();
               DA.Fill(ds);

               //Create an array.
               string[,] tempArray = new string[c1FlexGrid1.Rows.Count, c1FlexGrid1.Cols.Count];
               //Fill the array.
               for (int RRow = 0; RRow < c1FlexGrid1.Rows.Count - 1; RRow++)
               {
                   for (int RColm = 0; RColm < c1FlexGrid1.Cols.Count - 1; RColm++)
                   {
                       tempArray[RRow, RColm] = ds.Tables[0].Rows[RRow].ItemArray[RColm].ToString();
                   }
               }
               //Set the range value to the array.
               range.set_Value(System.Reflection.Missing.Value, tempArray);

               //Return control of Excel to the user.
               //objApp.Visible = true;
               //objApp.UserControl = true;

               object misValue = System.Reflection.Missing.Value;
               objBook.SaveAs(m_strSampleFolder + "AccessHistory.xlsx", Excel.XlFileFormat.xlWorkbookDefault, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
               objBook.Close(true, misValue, misValue);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message);
           }
           finally
           {
               MessageBox.Show("NOw in the finally block");
              Process objApp=Process.Start(m_strSampleFolder + "AccessHistory.xlsx");
           }
       }
 
Share this answer
 
Just add below line before saving.

excelApp.DisplayAlerts = false;
 
Share this answer
 
Comments
Member 14183767 12-Apr-19 5:52am    
hi , is there a way to get workbook name and store it in a db table?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900