Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an excel file named "Sample.csv" stored in project folder named "Files ".When the user click button this excel file should be saved on local drive.How to do that?

What I have tried:

private void btnSample_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel file |*.xls;*.xlsx;*.csv";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                const string MyFileName = "sample.csv";

                string execPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                var filePath = Path.Combine(execPath, MyFileName);

                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook book = app.Workbooks.Open(filePath);

                book.SaveAs(sfd.FileName); //Save   
                book.Close();
            }
        }
Posted
Updated 29-Jan-20 19:19pm
v3
Comments
Richard MacCutchan 30-Jan-20 5:15am    
Why are you using SaveFileDialog since you do not use the selected path and name that the user chooses?

What happens when you run this code?
Richard Deeming 30-Jan-20 9:33am    
The selected path is used:
book.SaveAs(sfd.FileName);
Richard MacCutchan 30-Jan-20 10:08am    
Oops!

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