ASP.NET应用程序中的服务器端DataTable XLS下载
本文关键字:DataTable XLS 下载 服务器端 NET 应用程序 ASP | 更新日期: 2023-09-27 18:29:44
我将jQuery DataTable更改为服务器端实现,它运行良好。现在我想添加下载XLS的功能;类似于非服务器端表的内置csv导出。我有类似的东西:
$(document).ready( function () {
$('#example').dataTable( {
"sDom": 'T<"clear">lfrtip',
"oTableTools": {
"aButtons": [ {
"sExtends": "download",
"sButtonText": "Download XLS",
"sUrl": "/generate_xls.php"
} ]
}
} );
问题:
如何在不使用php的情况下在.NET应用程序中下载文件?
如何获取xls或csv文件的所有表格数据?很明显,它们位于服务器上,所以它不像我最初的前端实现那样直接。
请随时提供任何有用的链接或指针!谢谢
好的,所以我能够弄清楚自己(在另一个答案的帮助下)。基本上,我将"sURL"设置为指向控制器操作,然后该控制器创建并下载了一个CSV文件,如下所示:
public ActionResult AccountsExportCSV()
{
MemoryStream output = new MemoryStream();
StreamWriter writer = new StreamWriter(output);
load_repo();
var accounts = _repo.GetAllAccounts();
writer.Write("Email");
writer.Write(",");
writer.Write("Full Name");
writer.Write(",");
writer.Write("Phone");
writer.Write(",");
writer.Write("Points");
writer.Write(",");
writer.Write("Date Added");
writer.Write(",");
writer.Write("Last Sync");
writer.Write(",");
writer.Write("User Type");
writer.WriteLine();
int userType = 0;
foreach (Account account in accounts)
{
writer.Write(account.Email);
writer.Write(",");
writer.Write(account.FullName);
writer.Write(",");
writer.Write(account.Phone);
writer.Write(",");
writer.Write(account.Points);
writer.Write(",");
writer.Write(account.AddDate);
writer.Write(",");
writer.Write(account.LastDate);
writer.Write(",");
userType = Convert.ToInt32(account.UserType);
switch (userType)
{
case 0:
writer.Write("Unknown");
break;
case 1:
writer.Write("User");
break;
case 10:
writer.Write("Support");
break;
case 20:
writer.Write("Manager");
break;
case 30:
writer.Write("Admin");
break;
case 40:
writer.Write("Developer");
break;
}
writer.WriteLine();
}
writer.Flush();
output.Position = 0;
return File(output, "text/comma-seperated-values", "accounts.csv");
}