Y
Y
Yana2017-10-20 10:55:16
ASP.NET
Yana, 2017-10-20 10:55:16

How to generate Excel based on Json?

Good afternoon!
I'm a teapot, but I'm learning. The question arose: how to generate an Excel file based on Json in c # MVC
there is a View where I pass a string to the controller

$.ajax({
    type: "POST",
    url: "/Naryad/AjaxTest4",
    data: JSON.stringify(testDataN),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: successFunc,
    error: errorFunc
});
               function successFunc(data, status) {
                alert(data);
            }
            function errorFunc(errorData) {
                alert('Ошибка' + errorData.responseText);
            }  })
    })

controller:
[HttpPost]
        public ActionResult AjaxTest4(Naryad[] testDataN)
        {

            JavaScriptSerializer serializer = new JavaScriptSerializer();
         

             string tmp = string.Empty;
             for (int i = 0; i < testDataN.Length; i++)
             {
                 string oborud = testDataN[i].Oborud;
                 string on = String.Format("{0}", oborud);
                 tmp += string.Format("Сервер2 получил данные! Значения:  {0}", on + ", " + testDataN[i].Date + ", " + testDataN[i].Staff + ", " + testDataN[i].TypeTo + ", " + testDataN[i].Work + ", " + testDataN[i].Tp + "," + testDataN[i].Tf);

             }
            return View();
            
            }

I read a lot of information and got confused in it Is it
possible to generate a file in ActionResult AjaxTest4 or you need to implement it somehow in another Action
Please help me figure it out

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Pavlov, 2017-10-20
@lexxpavlov

The easiest way is to convert an array of outfits to csv. And then you can convert from csv to xls using, for example, the EPPlus library ( a simple use case).
The conversion itself is best done in a separate method, rather than in AjaxTest4(). Better even in a separate class, IMHO.

M
Messi, 2017-10-20
@FitTech

Personally, I would do this:
Using DataTables , based on the data in json, I would build a table () on the page with the data that is needed in the document, and then just export to excel.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question