Solved

Asp.net mvc

Posted on 2016-08-05
7
54 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 13

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 81

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 13

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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