Angular 6 MVC项目在服务中捕获ActionResult

时间:2019-01-17 17:26:27

标签: c# json angular model-view-controller actionresult

在Angular 6 C#/ MVC项目中,我正在使用ClosedXML nuget包将数据导出到excel。我通过在RouteCofing.cs action =“ ExportToExcel”,中将其设置为操作方法来直接调用ExportToExcel方法来进行尝试,并且效果很好并生成xlsx文件,但是在Angular项目中我不确定如何检索控件回到服务类。控制器中的其余方法将Task返回,但是此方法将返回ActionResult,我不确定如何在服务类中返回结果。请查看附件图片以获取错误信息。

代码如下:

[![public ActionResult ExportToExcel()
        {
              //var sheetNames = new List<string>() { "sheetName1", "sheetName2" };
              var sheetNames = new List<string>() { "sheetName1" };


            string fileName = "SubmissionForm_" + DateTime.Now + ".xlsx";
            GenerateBook gb = new GenerateBook();
            DataSet ds = gb.GetDSExportToExcel(); //gb.GetDataSetExportToExcel();

            XLWorkbook wbook = new XLWorkbook();

            for (int k = 0; k < ds.Tables.Count; k++)
            {
                DataTable dt = ds.Tables\[k\];
                IXLWorksheet Sheet = wbook.Worksheets.Add(sheetNames\[k\]);
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    Sheet.Cell(1, (i + 1)).Value = dt.Columns\[i\].ColumnName;
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        Sheet.Cell((i + 2), (j + 1)).Value = dt.Rows\[i\]\[j\].ToString();
                    }
                }
            }

            Stream spreadsheetStream = new MemoryStream();
            wbook.SaveAs(spreadsheetStream);
            spreadsheetStream.Position = 0;

            return new FileStreamResult(spreadsheetStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = fileName };

        }\[!\[enter image description here\]\[1\]\]\[1\]
    enter code here][1]][1]

enter image description here

从服务类致电:

private getExportExcelUrl = this.urlStr +'/ Home / ExportToExcel';
公共SetExport(数据:任何){

    return this.http.post(this.getExportExcelUrl, null).subscribe((data: any) => { });
        //.pipe(map(res => <any[]>res))
        //.pipe(catchError(this.handleError));
}

2 个答案:

答案 0 :(得分:0)

返回文件(内存,GetMimeType(文件),文件名);

您将必须返回一个内存流(在下文中表示为“内存”),然后是mimeType和fileName。

  private string GetMimeType(string file)
  {
    string extension = Path.GetExtension(file).ToLowerInvariant();
    switch (extension)
    {
      case ".txt": return "text/plain";
      case ".pdf": return "application/pdf";
      case ".doc": return "application/vnd.ms-word";
      case ".docx": return "application/vnd.ms-word";
      case ".xls": return "application/vnd.ms-excel";
      case ".png": return "image/png";
      case ".jpg": return "image/jpeg";
      case ".jpeg": return "image/jpeg";
      case ".gif": return "image/gif";
      case ".csv": return "text/csv";
      default: return "";
    }
  }

通过您的API方法:

      spreadsheetStream.Position = 0;
      return File(spreadsheetStream, GetMimeType(fileName), filename);

有关更多详细信息,请参见this blog

希望这会有所帮助。

答案 1 :(得分:0)

在您的信息中,传递responseTypearrayBufferblob的参数选项。示例:

this.http.get(this.getExportExcelUrl, { responseType: 'blob' })

编辑:我将客户端请求更改为GET。我不确定您的控制器如何路由请求。我通常使用Route属性映射自己的地图。