将HTML表导出到Excel,而不是使用C#下载文件
Export HTML table to Excel not downloading file using C#
我正在创建一个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!");
})
});
这是我在同一目录下的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");
}
};
}
);
如有必要,这里是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"
]
}
这是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>
好吧,所以Umbraco页面正在加载,当Backoffice的开发人员部分打开时会出现警告框,但当我单击导出表按钮时,什么都没有发生。单击此按钮时,我正在尝试下载Excel工作表。我该怎么做?我是不是错过了什么?
添加
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
}
});
然后将按钮元素更改为:
<button ng-controller="AngularUmbracoPackage.ButtonController" class="btn btn-success" ng-click="ButtonClickHandler()">Export Table</button>
相关文章:
- asp.net网站文件下载历史记录
- Firefox,如何提交表单触发文件下载,但停留在网页上
- Interent Explorer中的数据URI文件下载
- 将页面上的文本替换为 jQuery 并提示文件下载
- JavaScript文件下载
- 是否可以触发文件下载到用户的浏览器
- Javascript JSON 到 Excel 文件下载
- 使用Javascript将多个文件下载为zip文件或文件夹
- 巴比伦JS-场景从本地文件下载
- 文件下载html与错误处理FileNotFound
- 使用jQuery捕获站点范围内的文件下载
- 在WinJS中将文件下载到文件系统
- JQuery启动文件下载,然后运行一个函数
- 文件下载无法在firefox上运行
- 创建文件下载js
- 如何使用JQuery禁用pdf文件下载选项
- 使用JavaScript/Jquery创建文本文件下载/更新到本地客户端机器中的特定位置
- CollectionFS中是否存在用于文件下载的事件侦听器/回调
- 文件下载后,Document Ready无法工作
- 浏览器不生成文件下载对话框