1

I changed my jQuery DataTable to a server side implementation and it works fine. Now I want to add the ability to download an XLS; similar to the built in csv export for non server side tables. I have something similar to this:

$(document).ready( function () {
    $('#example').dataTable( {
        "sDom": 'T<"clear">lfrtip',
        "oTableTools": {
            "aButtons": [ {
                "sExtends": "download",
                "sButtonText": "Download XLS",
                "sUrl": "/generate_xls.php"
            } ]
        }
    } );

Questions:

  1. How do I download a file in my .NET application without having to use php?

  2. How do I get all my table data for the xls or csv file? Obviously they are located on the server so it's not as straight forward as my initial front end implementation.

Feel free to provide any helpful links or pointers! Thanks!

4

1 回答 1

0

好的,所以我能够弄清楚自己(在另一个答案的帮助下)。基本上,我将“sURL”设置为指向控制器操作,然后该控制器创建并下载了这样的 CSV 文件:

    public ActionResult AccountsExportCSV()
    {
        MemoryStream output = new MemoryStream();
        StreamWriter writer = new StreamWriter(output);
        load_repo();
        var accounts = _repo.GetAllAccounts();

        writer.Write("Email");
        writer.Write(",");
        writer.Write("Full Name");
        writer.Write(",");
        writer.Write("Phone");
        writer.Write(",");
        writer.Write("Points");
        writer.Write(",");
        writer.Write("Date Added");
        writer.Write(",");
        writer.Write("Last Sync");
        writer.Write(",");
        writer.Write("User Type");
        writer.WriteLine();

        int userType = 0;

        foreach (Account account in accounts)
        {
            writer.Write(account.Email);
            writer.Write(",");
            writer.Write(account.FullName);
            writer.Write(",");
            writer.Write(account.Phone);
            writer.Write(",");
            writer.Write(account.Points);
            writer.Write(",");
            writer.Write(account.AddDate);
            writer.Write(",");
            writer.Write(account.LastDate);
            writer.Write(",");
            userType = Convert.ToInt32(account.UserType);
            switch (userType)
            {
                case 0:
                    writer.Write("Unknown");
                    break;
                case 1:
                    writer.Write("User");
                    break;
                case 10:
                    writer.Write("Support");
                    break;
                case 20:
                    writer.Write("Manager");
                    break;
                case 30:
                    writer.Write("Admin");
                    break;
                case 40:
                    writer.Write("Developer");
                    break;
            }
            writer.WriteLine();
        }

        writer.Flush();
        output.Position = 0;

        return File(output, "text/comma-seperated-values", "accounts.csv");
    }
于 2013-09-25T14:50:38.213 回答