Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS导出excel文件-文件已损坏_Javascript_Excel_File - Fatal编程技术网

Javascript JS导出excel文件-文件已损坏

Javascript JS导出excel文件-文件已损坏,javascript,excel,file,Javascript,Excel,File,导出NETCORE 3.1服务器中生成的excel文件时遇到问题 我必须向它发出一个发帖请求 这是密码 JS: } 服务器代码: [HttpPost] public IActionResult ExportRelatorio([FromBody] string data) { var relatorios = JsonConvert.DeserializeObject<List<Relatorio>>(data);

导出NETCORE 3.1服务器中生成的excel文件时遇到问题

我必须向它发出一个发帖请求

这是密码

JS:

} 服务器代码:

[HttpPost]
        public IActionResult ExportRelatorio([FromBody] string data)
        {
            var relatorios = JsonConvert.DeserializeObject<List<Relatorio>>(data);
            string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            string fileName = "GestãoCircuitos-" + DateTime.Now.ToString("yyyy-MM-dd") + ".xlsx";
            try
            {
                using (var workbook = new XLWorkbook())
                {
                    IXLWorksheet worksheet = workbook.Worksheets.Add("Relatórios");
                    worksheet.Cell(1, 1).Value = "Data";
                    worksheet.Cell(1, 2).Value = "Matricula";
                    worksheet.Cell(1, 3).Value = "Condutor";
                    worksheet.Cell(1, 4).Value = "Rota";
                    worksheet.Cell(1, 5).Value = "Distância Ideal";
                    worksheet.Cell(1, 6).Value = "Kms";
                    worksheet.Cell(1, 7).Value = "Diferencial";
                    worksheet.Cell(1, 8).Value = "Desvio (%)";
                    worksheet.Cell(1, 9).Value = "Tempo de Condução";
                    for (int index = 1; index <= relatorios.Count; index++)
                    {
                        worksheet.Cell(index + 1, 1).Value = relatorios[index - 1].Data;
                        worksheet.Cell(index + 1, 2).Value = relatorios[index - 1].Viatura;
                        worksheet.Cell(index + 1, 3).Value = relatorios[index - 1].Condutor;
                        worksheet.Cell(index + 1, 4).Value = relatorios[index - 1].Rota;
                        worksheet.Cell(index + 1, 5).Value = relatorios[index - 1].DistanciaIdeal;
                        worksheet.Cell(index + 1, 6).Value = relatorios[index - 1].Kms;
                        worksheet.Cell(index + 1, 7).Value = relatorios[index - 1].Dif;
                        worksheet.Cell(index + 1, 8).Value = relatorios[index - 1].Desvio;
                        worksheet.Cell(index + 1, 9).Value = relatorios[index - 1].TempoConducao;
                    }
                    using (var stream = new MemoryStream())
                    {
                        workbook.SaveAs(stream);
                        var content = stream.ToArray();
                        return File(content, contentType, fileName);
                    }
                }

            }
            catch (Exception ex)
            {
                return View();
            }
    }
[HttpPost]
public IActionResult ExportRelatorio([FromBody]字符串数据)
{
var relatorios=JsonConvert.DeserializeObject(数据);
string contentType=“application/vnd.openxmlformats officedocument.spreadsheetml.sheet”;
字符串fileName=“GestãoCircuitos-”+DateTime.Now.ToString(“yyyy-MM-dd”)+“.xlsx”;
尝试
{
使用(var工作簿=新的XL工作簿())
{
IXL工作表工作表=工作簿。工作表。添加(“关系”);
工作表.单元格(1,1).Value=“数据”;
工作表。单元格(1,2)。Value=“矩阵”;
工作表.Cell(1,3).Value=“Condutor”;
工作表。单元格(1,4)。Value=“Rota”;
单元格(1,5).Value=“Dist–ncia-Ideal”;
工作表.Cell(1,6).Value=“Kms”;
工作表.Cell(1,7).Value=“diferenceal”;
工作表.Cell(1,8).Value=“Desvio(%)”;
工作表.Cell(1,9).Value=“Tempo de Condução”;
用于(int index=1;index
$.ajax
处理二进制数据

您可能希望尝试使用本机blob支持

另外,我在这里添加了一些清理

async function exportRelatorio(tabela) {
    const resp = await fetch("/Relatorios/ExportRelatorio/", {
        body: JSON.stringify(tabela),
        credentials: 'same-origin',
        method: 'POST',
        headers: {
            "Content-Type": 'application/json; charset=utf-8',
        },
    });
    if(!resp.ok) throw new Error("Not OK");
    const blob = await resp.blob();
    // Programatically create a link and click it:
    const a = document.createElement("a");
    a.href = URL.createObjectURL(blob);
    a.download = "circuitos.xlsx";
    document.body.appendChild(a);
    a.click();
    setTimeout(() => {  // Clean up
        URL.revokeObjectURL(a.href);
        a.parentNode.removeChild(a);
    }, 1000);
}

它给了我这个未捕获的(承诺中的)类型错误:未能在“窗口”上执行“获取”:带有GET/HEAD方法的请求不能有body.at exportRelatrio(site.js?v=5x4gjkPy3gfdgyolifigwqfjbczvDmzPcwj30isq:59)在htmlButtoneElement.onclick(relatrios:1)哎呀,我忘了
方法:'POST'
。几乎就在那里了,现在我在服务器端的参数data为空。你能帮忙吗?你能看到你的浏览器还在浏览器的检查器中发布数据吗?
async function exportRelatorio(tabela) {
    const resp = await fetch("/Relatorios/ExportRelatorio/", {
        body: JSON.stringify(tabela),
        credentials: 'same-origin',
        method: 'POST',
        headers: {
            "Content-Type": 'application/json; charset=utf-8',
        },
    });
    if(!resp.ok) throw new Error("Not OK");
    const blob = await resp.blob();
    // Programatically create a link and click it:
    const a = document.createElement("a");
    a.href = URL.createObjectURL(blob);
    a.download = "circuitos.xlsx";
    document.body.appendChild(a);
    a.click();
    setTimeout(() => {  // Clean up
        URL.revokeObjectURL(a.href);
        a.parentNode.removeChild(a);
    }, 1000);
}