Solved

Asp.net mvc

Posted on 2016-08-05
7
52 Views
Last Modified: 2016-08-05
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

0
Comment
Question by:Moti Mashiah
  • 4
  • 2
7 Comments
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41744662
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.
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 41744706
Thank you for your answer:

Can you show some example to how i pass the fdb to  workbook.SaveAs("d:\\test\\Item_list.xlsx");
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 41744707
Or should I use it without saving. just popup dialog.
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 80

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 250 total points
ID: 41744732
First you have to save it to your web server then the user can download the file
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 41744738
There is away to avoid saving to the server ?
0
 
LVL 12

Accepted Solution

by:
Dustin Saunders earned 250 total points
ID: 41744796
Can you show some example to how i pass the fdb to  workbook.SaveAs("d:\\test\\Item_list.xlsx");

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

string selectedfolder = fbd.SelectedPath;
string outFile = selectedfolder + "\\Item_list.xlsx";
workbook.SaveAs(outFile);

Open in new window

0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 41744865
Dustin,
This is actually avoid me from saving the file on the server?
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question