【发布时间】:2016-11-15 08:35:54
【问题描述】:
这是调用控制器的ajax调用
@using(Html.BeginForm("ExportData", "ViewData", FormMethod.Post,
new {
id = "myform", name = "myform"
})) {
<button type = "submit"> Export Raw Policy </button>
}
<div id = "divProcessing"> <img src = ~/assets/layouts/layout3/img/loading-spinner-blue.gif" > </p> </div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type = "text/javascript" >
$(document)
.ready(function() {
// Hide the "busy" Gif at load:
$("#divProcessing").hide();
// Handle the form submit event, and make the Ajax request:
$("#myform")
.on("submit",
function(event) {
event.preventDefault();
// Show the "busy" Gif:
$("#divProcessing").show();
var url = $(this).attr("action");
var formData = $(this).serialize();
$.ajax({
url: url,
type: "POST",
data: formData,
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function(resp) {
// Hide the "busy" gif:
$("#divProcessing").hide();
// Do something useful with the data:
$("<h3></h3>")
.appendTo("#divResult");
}
})
});
}); </script>
}
此方法是将数据表导出到excel中
public static void ExportToExcel(DataTable table)
{
try
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
//sets font
HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
HttpContext.Current.Response.Write("<BR><BR><BR>");
//sets the table border, cell spacing, border color, font of the text, background, foreground, font height
HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " +
"borderColor='#000000' cellSpacing='0' cellPadding='0' " +
"style='font-size:11.0pt; font-family:Calibri; background:white;'> <TR>");
//am getting my grid's column headers
int columnscount = table.Columns.Count;
for (int j = 0; j < columnscount; j++)
{
//write in new column
HttpContext.Current.Response.Write("<Td>");
//Get column headers and make it as bold in excel columns
HttpContext.Current.Response.Write("<B>");
HttpContext.Current.Response.Write(table.Columns[j].ToString());
HttpContext.Current.Response.Write("</B>");
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
foreach (DataRow row in table.Rows)
{
//write in new row
HttpContext.Current.Response.Write("<TR>");
for (int i = 0; i < table.Columns.Count; i++)
{
HttpContext.Current.Response.Write("<Td>");
HttpContext.Current.Response.Write(HttpContext.Current.Server.HtmlEncode(row[i].ToString()));
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
}
HttpContext.Current.Response.Write("</Table>");
HttpContext.Current.Response.Write("</font>");
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
catch (Exception ex)
{
string abc = ex.ToString();
}
}
这是我的控制器
public ActionResult ExportData()
{
var industryData = _rawDataHlper.GetIndustryData();
if (industryData != null)
{
ExportToExcel((industryData));
}
return RedirectToAction("Index");
}
我正在尝试使用 ajax 导出到 excel,但由于 ajax 没有下载文件。我正在努力寻找解决方案。我试图将我的数据表转换为 Json,然后将其传递给 ajax。从 ajax 成功我将得到 json 并将其导出到 excel 中。我写了下面的函数来转换成 Json
public string DataTableToJSONWithJavaScriptSerializer(DataTable table)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
Dictionary<string, object> childRow;
foreach (DataRow row in table.Rows)
{
childRow = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
childRow.Add(col.ColumnName, row[col]);
}
parentRow.Add(childRow);
}
return jsSerializer.Serialize(parentRow);
}
但由于我的数据表有超过 50,000 条记录,我遇到了内存不足异常。我试图转换成字符串并将字符串发送到ajax成功,但我得到了内存不足的异常。
谁能建议我如何通过 ajax 做到这一点。我的整个过程需要 4 到 5 分钟,这就是为什么我在后台下载文件时使用 ajax 显示加载器的原因。任何帮助,将不胜感激。 谢谢
【问题讨论】:
-
使用一些实用程序解析器来填充 excel。
-
什么样的实用程序?我试图在网上搜索,但找不到任何相关的解决方案
-
感谢您的链接,但我可以通过 ajax 调用它吗?因为那是我想做的,我已经有数据表到 excel 代码,但我不能通过 ajax 调用
标签: c# .net ajax asp.net-mvc datatable