This is a migrated thread and some comments may be shown as answers.

generate excel file with some data in form entred by user

3 Answers 21 Views
Spreadsheet
This is a migrated thread and some comments may be shown as answers.
Wassim
Top achievements
Rank 1
Veteran
Iron
Wassim asked on 02 Dec 2020, 10:31 AM

Hello,
I have a form with different text fields and dropdown lists.

after entering the information, I have a button that allows me to validate the creation of this new element.
this button must call an action of my controller which is used to generate an excel file where I must call the template of this file from my local files and display the information entered in the fields that correspond in the excel file
I am using npoi library (c #) in back and telerik ui for asp.net mvc 

It would be much appreciated if you can help me on this.

Thanks for replying 

 

 

3 Answers, 1 is accepted

Sort by
0
Aleksandar
Telerik team
answered on 04 Dec 2020, 08:07 AM

Hello Wassim,

Based on the provided details I am not sure what the issue is. Is the form used a Telerik UI for ASP.NET MVC Form or a regular HTML form? Is the issue related to submitting the data? Also, I am not familiar with the library of choice for the backend processing and Excel file generation, but once you have the form fields submitted to the action method you should be able to set the passed values in the respective template. If you have issues using the npoi library you can check the following StackOverflow thread for guidance.

Should you have any Telerik UI for ASP.NET MVC related questions feel free to reach out.

Regards,
Aleksandar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Wassim
Top achievements
Rank 1
Veteran
Iron
answered on 09 Dec 2020, 08:09 AM

Hello Aleksandar,

in the same context, I tried to call a method action of my controller to generate an excel file after clicking on a button

.ToolBar(toolbar =>
{
toolbar.Custom().Text("Create").IconClass("fa fa-file-excel").Action("GenerateExcelAction",  "GenerateExcelController").HtmlAttributes(new { @class = "btn btn-outline-primary" });
})

 

using NPOI.HSSF.UserModel;
using System.IO;

public ActionResult GenerateExcelAction()
        {
            try
            {
                // Opening the Excel template...
                FileStream fs =
                    new FileStream(Server.MapPath(@"\Content\Templates\myFile.xls"), FileMode.Open, FileAccess.Read);
         
                // Getting the complete workbook...
                HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true);

                // Getting the worksheet by its name...
                HSSFSheet shhet1= (HSSFSheet)templateWorkbook.GetSheet("sheet1");

                HSSFRow dataRow = (HSSFRow)sheet1.GetRow(3);
                dataRow .GetCell(5).SetCellValue("some text");
           
                // Forcing formula recalculation...
                sheet1.ForceFormulaRecalculation = true;

                MemoryStream ms = new MemoryStream();

                // Writing the workbook content to the FileStream...
                templateWorkbook.Write(ms);
               
                // Sending the server processed data back to the user computer...
                return File(ms.ToArray(), "application/vnd.ms-excel", "newFile.xls");

            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                return RedirectToAction("Index");
            }
            
        }
    }

 

this method works fine by generating me an excel but if I do it otherwise by calling a js ajax function to pass json data .. it enters the method and  nothing happens

 

.ToolBar(toolbar =>
{
                                               
toolbar.Custom().Text("Create").IconClass("fa fa-file-excel").HtmlAttributes(new { id = "createExcel", @class = "btn btn-outline-primary", onclick = "generateExcel();" });

})

 

  function generateExcel() {

        //var modelData = { "modelData": "dt" };

        $.ajax({
            url: '/GenerateExcelController/GenerateExcelAction',
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ modelData: "dttt" }),

            success: function (data) {
               alert("succeedd");
            },
            error: function (data) {
                alert("KO");
            }
        });
    }

going through the 2nd method, it recovers the data and goes through the method without returning the excel file to me like the first methode
something missing ??

Any help please?

0
Accepted
Aleksandar
Telerik team
answered on 10 Dec 2020, 12:41 PM

Hi Wassim,

You cannot return a file for download via an AJAX call. I would suggest reviewing the SO threads linked below that discusses how to download a file after an ajax call is made:

https://stackoverflow.com/questions/16670209/download-excel-file-via-ajax-mvc

https://stackoverflow.com/questions/54896957/download-a-file-through-ajax-request-in-asp-net-mvc

I hope this helps.

Regards,
Aleksandar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Spreadsheet
Asked by
Wassim
Top achievements
Rank 1
Veteran
Iron
Answers by
Aleksandar
Telerik team
Wassim
Top achievements
Rank 1
Veteran
Iron
Share this question
or