将HTML表导出到Excel而不是使用C#下载文件

时间:2022-12-01 09:57:30

I'm creating an Umbraco website, and I am creating a plugin for the backend of Umbraco so a user can export an Excel worksheet from an HTML table. I'm using AngularJS and a C# controller to do this. Here are my files.

我正在创建一个Umbraco网站,我正在为Umbraco的后端创建一个插件,以便用户可以从HTML表格中导出Excel工作表。我正在使用AngularJS和C#控制器来执行此操作。这是我的文件。

//This is my C# Controller at /App_Code/ExportBlankDictionaryController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Editors;
using Umbraco.Core.Persistence;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.IO;

namespace AngularUmbracoPackage.App_Code
{
    [Umbraco.Web.Mvc.PluginController("AngularUmbracoPackage")]
    public class ExportBlankDictionaryController : UmbracoAuthorizedJsonController
    {
        //[System.Web.Http.AcceptVerbs("GET", "POST")]
        //[System.Web.Http.HttpGet]
        public void ExportExcell()
        {
            var keys = new System.Data.DataTable("BlankDictionaryItems");
            keys.Columns.Add("Keys", typeof(string));
            keys.Columns.Add("Description", typeof(string));

            keys.Rows.Add("Enter First Dictionary Name Here", " ");

            var grid = new GridView();
            grid.DataSource = keys;
            grid.DataBind();

            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.BufferOutput = true;
            HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename=BlankDictionaryItems.xls");
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

            HttpContext.Current.Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            grid.RenderControl(htw);

            HttpContext.Current.Response.Output.Write(sw.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }
    }
}
// This is my AngularJS controller at /App_Plugins/datatable/datatable.controller.js:

angular.module("umbraco")
    .controller("AngularUmbracoPackage.ExportBlankDictionaryController", function ($scope, keyResource) {
        keyResource.exportToExcell().then(function (response) {
            alert("Table Generated!");
        })
    });

This is my datatable.resource.js file within the same directory:

这是我在同一目录下的datatable.resource.js文件:

// Adds the resource to umbraco.resources module:
angular.module('umbraco.resources').factory('keyResource',
    function ($q, $http) {
        // The factory object returned
        return {
            // This calls the API controller we setup earlier
            exportToExcell: function () {
                console.log("button clicked");
                return $http.post("backoffice/AngularUmbracoPackage/ExportBlankDictionary/ExportExcell");
            }
        };
    }
);

If necessary, here is the package.manifest.json file:

如有必要,这是package.manifest.json文件:

{
    propertyEditors:[
    {
        name: "DataTable editor",
        alias: "my.datatable.editor",
        editor:{
            view: "~/app_plugins/datatable/table.html",
            valueType: "JSON"
        }
    }
    ],
    javascript: [
    "~/app_plugins/datatable/datatable.controller.js",
    "~/app_plugins/datatable/datatable.resource.js"
    ]
}

Here is the table.html file which is the view:

这是table.html文件,它是视图:

<div class="ExportDiv" ng-controller="AngularUmbracoPackage.ExportBlankDictionaryController">
    <table id="table1" class="table table-bordered" runat="server">
        <thead>
            <tr>
                <th>Key</th>
                <th>Populate Dictionary Item Names in Key Column</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Enter First Dictionary Name Here</td>
                <td></td>
            </tr>
        </tbody>
    </table>
</div>

<button class="btn btn-success" ng-click="exportToExcel()">Export Table</button>

Okay, so the Umbraco page is loading, the alert box comes up when the developer section of Backoffice is opened, but when I click the Export Table button, nothing happens. I am trying to get an Excel sheet to download when this button is clicked. How can I do this? Am I missing something?

好的,所以Umbraco页面正在加载,当Backoffice的开发人员部分打开时会出现警告框,但是当我单击Export Table按钮时,没有任何反应。我试图在单击此按钮时下载Excel表格。我怎样才能做到这一点?我错过了什么吗?

1 个解决方案

#1


2  

Add

angular.module("umbraco")
    .controller("AngularUmbracoPackage.ButtonController", function ($scope, keyResource) {
        $scope.ButtonClickHandler = function(){
             console.log("clicked me!");
             keyResource.exportToExcell().then(function (response) {
             //do something with the response from the server
        }
    });

then change the button element to:

然后将按钮元素更改为:

<button ng-controller="AngularUmbracoPackage.ButtonController" class="btn btn-success" ng-click="ButtonClickHandler()">Export Table</button>

#1


2  

Add

angular.module("umbraco")
    .controller("AngularUmbracoPackage.ButtonController", function ($scope, keyResource) {
        $scope.ButtonClickHandler = function(){
             console.log("clicked me!");
             keyResource.exportToExcell().then(function (response) {
             //do something with the response from the server
        }
    });

then change the button element to:

然后将按钮元素更改为:

<button ng-controller="AngularUmbracoPackage.ButtonController" class="btn btn-success" ng-click="ButtonClickHandler()">Export Table</button>