如何解决带有 Json 的导出功能不起作用
目标:
根据表中选定的行导出到 Excel。
问题:
当我使用 json 执行代码时,CloseXML 不想导出到 excel。
我缺少代码的哪一部分?
信息:
*使用 VS 2019 和 asp.net core mvc
*使用CloseXML导出到excel
谢谢!
@{
ViewData["Title"] = "Home Page";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Post</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" id="" class="datadata" name="scales" value="a" checked></td>
<td>a</td>
</tr>
<tr>
<td><input type="checkbox" id="" class="datadata" name="scales" value="b" checked></td>
<td>b</td>
</tr>
<tr>
<td><input type="checkbox" id="" class="datadata" name="scales" value="c" checked></td>
<td>c</td>
</tr>
</tbody>
</table>
@Html.ActionLink("Filter","DownloadExcelDocument","Home")
<button id="run" class="dd">Run</button>
<script>
$(document).ready(function () {
$(".dd").click(function () {
var id = document.getElementsByClassName("datadata");
var defaultConstructorArray = new Array();
for (var i = 0; i < id.length; i++) {
if (id[i].checked === true) {
defaultConstructorArray.push(id[i].value);
}
}
$.ajax({
type: 'POST',url: '@Url.Action("SaveExceptions","Home")',data: { 'ids': defaultConstructorArray },datatype: "json",traditional: true,success: function () {
alert("test");
},error: function (jqXHR,textStatus,errorThrown) {
alert("jqXHR:" + jqXHR.status + " errorThrown: " + errorThrown);
}
});
});
});
</script>
using ClosedXML.Excel;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using WebApplication3.Models;
namespace WebApplication3.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
[HttpGet]
public IActionResult DownloadExcelDocument()
{
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
string fileName = "asdf.xlsx";
using (var workbook = new XLWorkbook())
{
IXLWorksheet worksheet =
workbook.Worksheets.Add("Authors");
worksheet.Cell(1,1).Value = "Id";
worksheet.Cell(1,2).Value = "FirstName";
worksheet.Cell(1,3).Value = "LastName";
worksheet.Cell(2,1).Value = "a";
worksheet.Cell(2,2).Value = "a";
worksheet.Cell(2,3).Value = "a";
using (var stream = new MemoryStream())
{
workbook.SaveAs(stream);
var content = stream.ToArray();
return File(content,contentType,fileName);
}
}
}
[HttpPost]
public IActionResult SaveExceptions(string[] ids) //ids has no data,nulls
{
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
string fileName = "asdf.xlsx";
using (var workbook = new XLWorkbook())
{
IXLWorksheet worksheet =
workbook.Worksheets.Add("Authors");
worksheet.Cell(1,fileName);
}
}
}
}
}
解决方法
不要使用 Ajax。在您的成功函数中,数据参数是文件数据。 JavaScript 无法将其保存为文件。
您可以使用表单提交。
将复选框名称更改为 ids
。
<form asp-action="SaveExceptions">
<table>
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Post</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" id="" class="datadata" name="ids" value="a" checked></td>
<td>a</td>
</tr>
<tr>
<td><input type="checkbox" id="" class="datadata" name="ids" value="b" checked></td>
<td>b</td>
</tr>
<tr>
<td><input type="checkbox" id="" class="datadata" name="ids" value="c" checked></td>
<td>c</td>
</tr>
</tbody>
</table>
<input type="submit" value="Run"/>
</form>
测试结果。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。