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. Image for the cookie policy date

Export excel file to specific path

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

7 Replies

MS Mani Sankar Durai Syncfusion Team February 1, 2017 12:21 PM UTC

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. 



LJ Laura Jordan February 1, 2017 05:21 PM UTC

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.



FS Farveen Sulthana Thameeztheen Basha Syncfusion Team February 2, 2017 12:48 PM UTC

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. 



LJ Laura Jordan February 2, 2017 06:07 PM UTC

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.


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team February 3, 2017 12:22 PM UTC

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. 



LJ Laura Jordan February 15, 2017 10:54 PM UTC

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.


MS Mani Sankar Durai Syncfusion Team February 16, 2017 11:46 AM UTC

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 


Loader.
Live Chat Icon For mobile
Up arrow icon