简体   繁体   English

.NET C#使用ExcelPackage通过JS post导出到excel

[英].NET C# exporting to excel via JS post using ExcelPackage

I'm not sure what I'm missing here. 我不确定我在这里缺少什么。 I've got a button that when clicked, I'm using javascript to call a controller. 我有一个按钮,当单击该按钮时,我正在使用javascript调用控制器。 This controller should create an excel file and return it to the user giving them the ability to download/save the file. 该控制器应创建一个excel文件,并将其返回给用户,使他们能够下载/保存文件。 I've tried a few different methods, but can't manage to get it to work. 我尝试了几种不同的方法,但无法使其正常工作。 Here's my javascript side: 这是我的JavaScript面:

function exportList() {
    var val = $("#team-dropdown").val();
    const date = new Date().toISOString();
    const param = {
        "Date": date,
        "GroupID": 1
    }

    $.ajax({
        url: "@Url.Action("ExportToExcel", "Home")",
        type: "POST",
        data: param
    });
}

Here's my server side: 这是我的服务器端:

public FileResult ExportToExcel(DateTime date, int groupID)
        {
            Load l = new Load();
            List<Load> loadList = l.GetLoadsForGroup(date, groupID);

            var fileDownloadName = "fileName.xlsx";
            var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            ExcelPackage pck = new ExcelPackage();
            var ws = pck.Workbook.Worksheets.Add("New workbook");
            ws.View.ShowGridLines = true;
            ws.DefaultColWidth = 25;
            ws.Cells[1, 1].Value = "Order #";

            var currRow = 2;
            foreach (var load in loadList)
            {
                ws.Cells[2, 2].Value = load.LoadNumber;                    
            }



            var fs = new MemoryStream();
            pck.SaveAs(fs);
            fs.Position = 0;
            var fsr = new FileStreamResult(fs, contentType);
            fsr.FileDownloadName = fileDownloadName;
            return (fsr);
        }

Not sure what the best way to do this is. 不确定执行此操作的最佳方法是什么。 If there's a better way, please feel free to elaborate. 如果有更好的方法,请随时详细说明。

Your method looks fine. 您的方法看起来不错。 In that case you just need to use a html form to post instead of using the js function. 在这种情况下,您只需要使用html form发布即可,而不是使用js函数。 Alternatively, if you would like to use a ActionResult you can write: 另外,如果您想使用ActionResult ,可以编写:

public ActionResult ExportToExcel()
    {
        Load l = new Load();
        List<Load> loadList = l.GetLoadsForGroup(date, groupID);

        var fileDownloadName = "fileName.xlsx";
        var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

        ExcelPackage pck = new ExcelPackage();
        var ws = pck.Workbook.Worksheets.Add("New workbook");
        ws.View.ShowGridLines = true;
        ws.DefaultColWidth = 25;
        ws.Cells[1, 1].Value = "Order #";

        var currRow = 2;
        foreach (var load in loadList)
        {
            ws.Cells[2, 2].Value = load.LoadNumber;
        }

        Response.Clear();
        Response.ContentType = contentType;
        Response.AddHeader("content-disposition", "attachment; filename=\"" + fileDownloadName + "\"");
        Response.BinaryWrite(pck.GetAsByteArray());
        Response.Flush();
        Response.End();
        return View();
    }

And you get the same result as your method. 您将获得与您的方法相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JS中使用C#POST获取数据 - Fetching data using C# POST in JS 通过JS和C#中的POST请求解析JSON数据 - Parsing JSON data via a POST request in JS and C# AngularJS:如何通过$ http发布将JS对象发送到c#控制器? - AngularJS: How to send JS object to c# controller via $http post? JS:数据表打印和导出Excel - JS: datatables printing and exporting excel 如何在 ASP.NET Core MVC 中将 js 字符串发布到 C# - How to post js string to C# in ASP.NET Core MVC 使用 HTTP Post(RESTful API)将来自 React.JS 前端的请求传递到 C# 后端(ASP.NET) - Passing a Request from React.JS front-end to C# back-end (ASP.NET) using HTTP Post (RESTful API) 通过按钮从JS调用c# - Calling c# from JS via button 如何在asp.net中使用C#将使用jquery flot动态生成的图表导出到Excel中? - How to export charts which are dynamically generated by using jquery flot into Excel using C# in asp.net? 使用asp.net使用Facebook c#sdk或javascript sdk在Face Book Wall上发布 - Post On Face Book Wall using asp.net using Facebook c# sdk or javascript sdk Asp.net 内核 - 使用 javascript 或 ZD7EFA19FBE7D29372FD5ADB60224 上传之前选择并验证 excel 文件 - Asp.net Core-choose and Validate excel file before uploading using javascript or C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM