Sajid Hussain

Sajid Hussain

  • 1.2k
  • 514
  • 100.6k

How to save file using Dialoguebox in Excellibrary

Dec 9 2015 12:32 AM
i am trying to import data from db to excel sheet using excel sheet,its work fine,but problem is that there is a fixed path where i can save a copy,but my requirement is that i have to import multiple sheet base on multiple queries,so i want to use savedialoguebox to save these file with different name and different location,how is it possible?my code is as follow
Hide Expand Copy Code
string connectionstring = null;
   string sql = null;
                            string data = null;  
                          int i = 0;  
                          int j = 0;  
                            Microsoft.Office.Interop.Excel.Application xlApp; 
                           Microsoft.Office.Interop.Excel.Workbook xlWorkBook; 
                           Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; 
                           object misValue = System.Reflection.Missing.Value;  

                             xlApp = new Microsoft.Office.Interop.Excel.Application();  
                          xlWorkBook = xlApp.Workbooks.Add(misValue);      
                      xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);  
                            connectionstring = "Data Source=(local);Initial Catalog=Report_Database;integrated security=True"
                           cnn = new SqlConnection(connectionstring);  
                          cnn.Open();                      
      sql = "SELECT * FROM Report_Tab";   
                         SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);     
                       DataSet ds = new DataSet(); 
                           dscmd.Fill(ds);   
                           for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)    
                        {  
                              for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
 {                                  
  data = ds.Tables[0].Rows[i].ItemArray[j].ToString();  
                                  xlWorkSheet.Cells[i + 1, j + 1] = data;    
                            }           
                 }                     
         xlApp.ActiveWorkbook.SaveCopyAs("C:\\Users\\sajid\\Desktop\\test.xls");        
                    xlApp.ActiveWorkbook.Saved = true;                          
  xlApp.Quit();                   
           releaseObject(xlWorkSheet);                  
          releaseObject(xlWorkBook);                   
         releaseObject(xlApp);                           
    MessageBox.Show("Excel file created , you can find the file D:\\Sam-informations.xls");