我正在使用jqgrid导出excel我的Razor代码如下:
//Set and activate navigations
jQuery("#analyticGridPager").jqGrid('navGrid', '#analyticGridPager', { add: false, edit: false, del: false, search: false },
{}, {}, {}, { multipleSearch: false, overlay: false, width: 480 }).jqGrid('navButtonAdd',
'#analyticGridPager',
{
caption: " Export to Excel ",
buttonicon: "ui-icon-bookmark",
onClickButton: function () {
exportExcel($(this));
}, position: "last"
});
function exportExcel() {
grid = $("#analyticGrid")
var mya = new Array();
mya = grid.getDataIDs(); // Get All IDs
var data = grid.getRowData(mya[0]); // Get First row to get the labels
var colNames = new Array();
var ii = 0;
for (var i in data) { colNames[ii++] = i; } // capture col names
var html = "";
for (k = 0; k < colNames.length; k++) {
html = html + colNames[k] + "\t";
// output each Column as tab delimited
}
html = html + "\n"; // Output header with end of line
for (i = 0; i < mya.length; i++) {
data = grid.getRowData(mya[i]); // get each row
for (j = 0; j < colNames.length; j++) {
html = html + data[colNames[j]] + "\t"; // output each Row as tab delimited
}
html = html + "\n"; // output each row with end of line
}
html = html + "\n"; // end of line at the end
//alert(html);
var params = { "htmlMarkup": html }
var str = jQuery.param(params);
window.open("/Analytics/CsvExport?" + str, '_blank')
}
现在在控制器上我已经编写了一个生成csv文件而不是excel文件的方法。我需要生成excel文件扩展名。
public ActionResult CsvExport()
{
string htmlMarkup = Request.QueryString["htmlMarkup"].ToString();
byte[] contents = Encoding.ASCII.GetBytes(htmlMarkup);
return File(contents, "application/vnd.ms-excel");
}
请纠正我的错误。
答案 0 :(得分:0)
请更换以下内容,以便进行测试。
还要更新你的web.config。
string htmlMarkup = Request.QueryString [&#34; htmlMarkup&#34;]。ToString();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
byte[] contents = Encoding.ASCII.GetBytes(htmlMarkup);
Response.End();
return File(contents, "application/excel");
更新您的web.cofig
<httpRuntime targetFramework="4.5" requestValidationMode="2.0" maxUrlLength="10999" relaxedUrlToFileSystemMapping="true" maxQueryStringLength="2097151" enable="true" executionTimeout="60000" maxRequestLength="30000000" />