所以这就是我最终得到它的方式:
您不能使用 ajax 返回二进制文件 - 您将收到它的数据 - 但无法显示二进制文件
我使用EPPlus作为 excel 库,并通过通用 HTML 表单隐藏字段中的 post 方法传递数据。使用流作为返回值生成输出,从而使用户保持在页面上而无需刷新/重定向
服务器:
//the expected data description - automatic tool @ http://json2csharp.com/
public class ExpectedJSON
{
public class SomeDataSet
{
public string dude{ get; set; }
public string what { get; set; }
public string foo { get; set; }
public string bar { get; set; }
public string baz { get; set; }
public int asd { get; set; }
public string someDate { get; set; }
public string wat { get; set; }
public string grrr { get; set; }
}
public class AnotherDataSet
{
public int type { get; set; }
public string date { get; set; }
public string dancing { get; set; }
public double camels { get; set; }
public int are { get; set; }
public int crying { get; set; }
public double _for { get; set; }
public double some { get; set; }
public double beer { get; set; }
}
public class MoreData
{
public int dark { get; set; }
public double side { get; set; }
public int of { get; set; }
public int the { get; set; }
public double moon { get; set; }
public double iz { get; set; }
public double da { get; set; }
public string bomb { get; set; }
}
}
public class ExportToController : Controller
{
public ActionResult Excel([FromUri(Name= "someData")] string someDataSet,
[FromUri(Name = "anotherData")] string anotherDataSet,
[FromUri(Name = "moreData")] string moreData)
{
ExpectedJSON.SomeDataSet someDataSetJson = JsonConvert.DeserializeObject<ExpectedJSON.SomeDataSet>(someData);
ExpectedJSON.AnotherDataSet[] anotherDataSetJson = JsonConvert.DeserializeObject<Campaign.OnlineComulativeDailyBuildChart[]>(anotherData);
ExpectedJSON.MoreData[] moreDataJson = JsonConvert.DeserializeObject<Campaign.OnlineSitesGrid[]>(moreData);
string fileName = "file.xlsx";
ExcelPackage p = new ExcelPackage();
p.Workbook.Worksheets.Add("Some Sheet");
ExcelWorksheet ws = p.Workbook.Worksheets[1];
//excel library logic follows...
//...
//..
//stream logic
MemoryStream stream = new MemoryStream(p.GetAsByteArray());
FileStreamResult result = new FileStreamResult(stream, "application/vnd.ms-excel")
{
FileDownloadName = fileName
};
return result;
}
}
HTML:
<form id="exportToExcel" action="../ExportTo/Excel/" target="_blank" method="post">
<input type="hidden" id="someData" name="someData" />
<input type="hidden" id="anotherData" name="anotherData" />
<input type="hidden" id="moreData" name="moreData" />
<button type="submit">export</button>
</form>
我所有的数据组件(需要导出)将它们的数据对象签名到一个通用对象中,以便在提交时 - 使用 JSON.stringify 用它的 json 表示填充 value 属性:
function bindEvents() {
var exportForm = $("#exportToExcel");
var someData= exportForm.find("#someData");
var anotherData= exportForm.find("#anotherData");
var moreData = exportForm.find("#moreData");
exportForm.on('submit', function (e) {
someData.attr("value", JSON.stringify(exporter.getData().someData));
anotherData.attr("value", JSON.stringify(exporter.getData().anotherData));
moreData .attr("value", JSON.stringify(exporter.getData().moreData));
});
}
使用 rails/sinatra/nodeJS 的分钟 - 使用 .net 的天/周 - 但你有它。