Link to home
Start Free TrialLog in
Avatar of Moti Mashiah
Moti MashiahFlag for Canada

asked on

Asp.net mvc

Hi Guys,

I'm using "Microsoft.Office.Interop.Excel" to export my form to excel spreadsheet and it is working fine and save it in the location I provided, but what if I want to let user to save it whenever he wants, like to popup window dialog and let him choose the path?

Here is my code:
public ActionResult Exporttoexcel()
        {
            Itemmodel itm = new Itemmodel();
            try
            {
                Excel.Application application = new Excel.Application();
                Excel.Workbook workbook = application.Workbooks.Add(System.Reflection.Missing.Value);
                Excel.Worksheet worksheet = workbook.ActiveSheet;

                worksheet.Cells[1, 1] = "Itemlookup";
                worksheet.Cells[1, 2] = "Description";
                worksheet.Cells[1, 3] = "Price";
                worksheet.Cells[1, 4] = "Cost";
                int row = 2;
                foreach(var it in itm.Findall())
                {
                    worksheet.Cells[row, 1] = it.Itemlookup;
                    worksheet.Cells[row, 2] = it.Description;
                    worksheet.Cells[row, 3] = it.Price;
                    worksheet.Cells[row, 4] = it.Cost;
                    row++;
                }
                Formatexcel(worksheet, row);
                workbook.SaveAs("d:\\test\\Item_list.xlsx");
                workbook.Close();
                Marshal.ReleaseComObject(workbook);
                application.Quit();
                Marshal.FinalReleaseComObject(application);

            }
            catch(Exception ex)
            {
               ViewBag.message = ex.Message;
            }
            return RedirectToAction("Index");
        }

Open in new window

Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

in C# you can use the FolderBrowserDialog to get the path:

FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();

string selectedfolder = fbd.SelectedPath;

Open in new window


Then you can save to the path.
Avatar of Moti Mashiah

ASKER

Thank you for your answer:

Can you show some example to how i pass the fdb to  workbook.SaveAs("d:\\test\\Item_list.xlsx");
Or should I use it without saving. just popup dialog.
SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There is away to avoid saving to the server ?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dustin,
This is actually avoid me from saving the file on the server?