We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

Export excel file to specific path

Thread ID:

Created:

Updated:

Platform:

Replies:

128605 Jan 31,2017 11:12 AM Sep 11,2018 12:26 PM ASP.NET MVC 7
loading
Tags: Grid
Laura Jordan
Asked On January 31, 2017 11:12 AM

Hi, 

I am using the Grid control export functionality to download the grid data file in the client, but I want to saved the generated excel book to specific path in the server. Could you help me to do so?

Really appreciate the support,

Thanks

Mani Sankar Durai [Syncfusion]
Replied On February 1, 2017 07:21 AM

Hi Laura, 

Thanks for contacting Syncfusion support. 

We have analyzed your query and based on your requirement we have prepared a sample that can be downloaded from the below link. 

Please refer the below code example. 
[Index.cshtml] 
@(Html.EJ().Grid<object>("FlatGrid") 
         .Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.datasource)) 
        .AllowPaging() 
        .ToolbarSettings(toolBar => toolBar.ShowToolbar(true).ToolbarItems(items => 
            { 
                items.AddTool(ToolBarItems.ExcelExport); 
                items.AddTool(ToolBarItems.WordExport); 
                items.AddTool(ToolBarItems.PdfExport); 
            })) 
         .Columns(col => 
            { 
... 
            }) 
) 
[controller.cs] 
  public void ExportToExcel(string GridModel) 
        { 
            ExcelExport exp = new ExcelExport(); 
            string targetFolder = HttpContext.Server.MapPath("") + "\\New folder\\"; 
            var DataSource = OrderRepository.GetAllRecords().ToList(); 
            GridProperties obj = ConvertGridObject(GridModel); 
            exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron",true, targetFolder);    //specify the path which helps to save the excel file in local folder 
        } 

In the above code example we have used server.MapPath to place the target folder. In the Export method we have assigned the targetFolder to save in the specified path. 

Please let us know if you need further assistance. 

Regards, 
Manisankar Durai. 


Laura Jordan
Replied On February 1, 2017 12:21 PM

Thanks, the code works just as I expected. The only detail is that the file name doesn´t generated as I wanted. It creates one folder for the day, then another folder for the month inside, and then the file named as 2017_CxC_JAR.xlsx inside. Where "CxC_JAR" is the value of the variable "name". 

This is my code:

string fileName = String.Format("{0:dd/MM/yyyy}", DateTime.Now) + "_" + name+ ".xlsx";
string targetFolder = HttpContext.Server.MapPath("") + "\\Repository\\" + User.Identity.Name.ToString() + "\\";
exp.Export(obj, DataSource, fileName, ExcelVersion.Excel2010, false, false, "flat-saffron", true, targetFolder);

What I want is the file named as String.Format("{0:dd/MM/yyyy}", DateTime.Now) + "_" + name+ ".xlsx" located inside the targetFolder.

Really appreciate the support,

Thanks.


Farveen Sulthana Thameeztheen Basha [Syncfusion]
Replied On February 2, 2017 07:48 AM

Hi Laura, 


When we give the filename with dateFormat using SlashString.Format("{0:dd/MM/yyyy}", dt)”, it will consider each slash as separate folder which is the default behavior.  So we suggest you to use any other dateformat for Exporting filename. For instance, you can use this format String.Format("{0:dd-MM-yyyy}", dt)” which will resolve your problem. 


Please refer to the code example:- 

public void ExportToExcel(string GridModel) 
        { 
            
            ExcelExport exp = new ExcelExport(); 
            string fileName = String.Format("{0:dd-MM-yyyy}", DateTime.Now) + "_" + name + ".xlsx".ToString(); 
            string targetFolder = HttpContext.Server.MapPath("") + "\\New folder\\"; 
            var DataSource = new NorthwindDataContext().OrdersViews.Take(100).ToList(); 
            GridProperties obj = ConvertGridObject(GridModel); 
            exp.Export(obj, DataSource, fileName, ExcelVersion.Excel2010, false, false, "flat-saffron", true, targetFolder); 
        } 


Please get back to us if you need any further assistance. 


Regards, 

Farveen sulthana T. 


Laura Jordan
Replied On February 2, 2017 01:07 PM

Hi,

This issue has been resolved, thanks.

Another question, how can I open the excel file instead of downloading it?

Really appreciate the support,

Thanks.

Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On February 3, 2017 07:22 AM

Hi Laura,  
 
From your query, we understand that you would like to use the local save option of the Exporting Grid. Using this option, Exported file will be saved to the specified folder which is the default behavior and this has been explained in our previous updates.  
 
Refer to the following overload function of the Export which highlights the local save option. 
 
        public void ExportToExcel(string GridModel) 
        { 
 
            ExcelExport exp = new ExcelExport(); 
            string fileName = String.Format("{0:dd-MM-yyyy}", DateTime.Now) + "_" + name + ".xlsx".ToString(); 
            string targetFolder = HttpContext.Server.MapPath("") + "\\New folder\\"; 
            var DataSource = new NorthwindDataContext().OrdersViews.Take(100).ToList(); 
            GridProperties obj = ConvertGridObject(GridModel); 
            //Grid properties, dataSource, filename, version of file, include/exclude hidden column, include/exclude template column, theme, enable/disable local save, folder to be saved 
            exp.Export(obj, DataSource, fileName, ExcelVersion.Excel2010, false, false, "flat-saffron", true/*enable/disable local save, */, targetFolder /*folder to be saved*/); 
        } 
 
Refer to the following Help Document on Exporting. 
 
 
You can also download the exported file with the prompt window as show cased in the following demo. 
 
 
It will alert the user and asked to choose the location for saving the exported documented. 
 
Regards, 
Seeni Sakthi Kumar S. 


Laura Jordan
Replied On February 15, 2017 05:54 PM

Ok, and how can I export the file to specific path?

Like:
string path = "Some specific path in my computer"
string targetFolder = HttpContext.Server.MapPath(path) + "\\New folder\\"; 

Appreciate the support,

Thanks.

Mani Sankar Durai [Syncfusion]
Replied On February 16, 2017 06:46 AM

Hi Laura, 

We can export the file to the specific path by mentioning the targetFolder to the export method. 
Refer the below code example. 
public void ExportToExcel(string GridModel) 
        { 
            ExcelExport exp = new ExcelExport(); 
            string path = Url.Content("D:\\excel");   //mention the path to store the excel file 
            string targetFolder = path + "\\New folder\\";  //no need to use server.MapPath 
            var DataSource = OrderRepository.GetAllRecords().ToList(); 
            GridProperties obj = ConvertGridObject(GridModel); 
            exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron",true, targetFolder); 
        } 

From the above code example we can save the export file to the any path in our system by passing directly to the targetFolder string variable. Also it is not necessary to mention Server.MapPath(). 

Please let us know if you need further assistance. 

Regards, 
Manisankar Durai 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;