Json 的导出功能不起作用
The Export function with Json do not work
目标:
根据 table.
中的选定行导出到 Excel
问题:
当我使用 json 执行代码时,CloseXML 不想导出到 excel。
我缺少代码的哪一部分?
信息:
*使用 VS 2019 和 asp.net 核心 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, 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);
}
}
}
}
}
不要使用 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>
测试结果。
目标:
根据 table.
问题:
当我使用 json 执行代码时,CloseXML 不想导出到 excel。
我缺少代码的哪一部分?
信息:
*使用 VS 2019 和 asp.net 核心 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, 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);
}
}
}
}
}
不要使用 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>
测试结果。