Solved

Asp.net mvc

Posted on 2016-08-05
7
42 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
Comment Utility
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
Comment Utility
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
Comment Utility
Or should I use it without saving. just popup dialog.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 78

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 250 total points
Comment Utility
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
Comment Utility
There is away to avoid saving to the server ?
0
 
LVL 12

Accepted Solution

by:
Dustin Saunders earned 250 total points
Comment Utility
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
Comment Utility
Dustin,
This is actually avoid me from saving the file on the server?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now