3
Answers

How to download excel file in window Application.

Sumit Ringane

Sumit Ringane

7y
160
1
In my database table  near about thousand record.
How could I download excel file using window application ?
 
 
 
 
Answers (3)
1
Manav Pandya

Manav Pandya

NA 7.1k 24k 7y
Hello 
 
Try following code :
 
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3. var saveFileDialog = new SaveFileDialog();  
  4. saveFileDialog.DefaultExt = "xls";  
  5. saveFileDialog.Filter = "Excel files (*.xls)|*.xls |All files (*.*)|*.*";  
  6. if (saveFileDialog.ShowDialog() == DialogResult.OK)  
  7. {  
  8. const string MyFileName = "filename.xls";  
  9. string execPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);  
  10. var filePath = Path.Combine(execPath, MyFileName);  
  11. Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();  
  12. Microsoft.Office.Interop.Excel.Workbook book = app.Workbooks.Open(filePath);  
  13. book.SaveAs(saveFileDialog.FileName); //Save  
  14. book.Close();  
  15. }  
  16. }  
Thanks 
0
Laxmidhar Sahoo

Laxmidhar Sahoo

NA 2.3k 1.4k 7y
  1. DialogResult result = openFileDialog1.ShowDialog();  
  2. if (result == DialogResult.OK) // Test result.  
  3. {  
  4.     string pathDestination = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "Files");  
  5.     foreach (string item in openFileDialog1.FileNames)  
  6.     {  
  7.         File.Copy(item, Path.Combine(pathDestination, Path.GetFileName(item)));  
  8.     }  
  9. }  
Try the bellow code may work
 
 
0
Sagar  Pandurang Kap

Sagar Pandurang Kap

NA 2.7k 7.5k 7y
Hi,
 
Try this :-
 
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string sourcePath = Application.StartupPath;
File.Copy(sourcePath + "\\filename.xls", saveFileDialog1.FileName);
}
 
Hope it helps.