c# html to excel,C# MVC HTML to EXCEL not working

作者原本有一个控制器方法可以将HTML转换为Excel文件,在不作为Web方法时工作正常。但当HTML字符串过大时,该方法不再生效。作者将其转换为Web方法并通过jQuery调用,数据传递成功,但无法创建Excel文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I had a controller method to convert html to excel which worked fine when it was not a web method. But stopped working when the html string got too large. I converted it to a web method which I call via jQuery. It passes the data to the method, but no longer creates the Excel file. The code within the method has not changed. The only difference is the way I call it and the [HttpPost] decoration. The code follows:

[HttpPost]

[ValidateInput(false)]

public void ExportExcel(string aContent)

{

StringBuilder sb = new StringBuilder();

sb.Append(aContent);

Response.ClearContent();

Response.ContentType = "application/vnd.ms-excel";

Response.AddHeader("Content-Disposition", "attachment; filename=ProjectData.xls");

Response.Write(sb);

Response.End();

}

And this is the jQuery stuff:

// namespace

var excel = excel || {};

$(document).ready(function () {

$('#ExcelUrl').on('click', function () {

var table = $('#tableName').val();

var content = $('#' + table).html();

excel.utilFunctions.exportToExcel(content);

});

});

excel.utilFunctions = (function ($) {

var exportToExcel = function (content) {

$.ajax({

url: "/Home/ExportExcel",

type: "POST",

data: { aContent: content }

}).done(function(data) {

alert(data);

}).fail(function(error) {

alert(error.responseText);

});

};

//public functions

return {

exportToExcel: exportToExcel

};

})(jQuery);

Any help is appreciated.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值