Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/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
C# NPOI Excel文件下载损坏_C#_Angularjs_Excel_Npoi_Mcafee - Fatal编程技术网

C# NPOI Excel文件下载损坏

C# NPOI Excel文件下载损坏,c#,angularjs,excel,npoi,mcafee,C#,Angularjs,Excel,Npoi,Mcafee,我正在尝试使用NPOI、angular2和web API将数据表中的数据导出到excel。我下面的步骤是-1。将数据表中的数据写入XLSX,并将文件临时存储在服务器2上。将此文件读入内存流并返回。我看到浏览器提示用户下载文件,一旦下载文件,我就会收到一个错误,说文件已损坏。但是,当在步骤1之后在服务器上打开文件时,文件会完全打开,通过文件资源管理器下载后打开文件也可以正常工作。您能帮助我理解下载后从浏览器打开文件时为什么会发生这种情况吗 web api代码: public HttpRespo

我正在尝试使用NPOI、angular2和web API将数据表中的数据导出到excel。我下面的步骤是-1。将数据表中的数据写入XLSX,并将文件临时存储在服务器2上。将此文件读入内存流并返回。我看到浏览器提示用户下载文件,一旦下载文件,我就会收到一个错误,说文件已损坏。但是,当在步骤1之后在服务器上打开文件时,文件会完全打开,通过文件资源管理器下载后打开文件也可以正常工作。您能帮助我理解下载后从浏览器打开文件时为什么会发生这种情况吗

web api代码:

  public HttpResponseMessage ExportReport(ReportPostModel ReportModel)
            {
                try
                {
                    var dt = _IReportRepositoryObj.ExportToExcel(ReportModel.CommentsToExport, ReportModel.SelectedColumns);
                    var headers = dt.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToArray();
                    ICellStyle cellStyle;
                    XSSFWorkbook workbook = new XSSFWorkbook();
                    cellStyle = workbook.CreateCellStyle();
                    XSSFFont hFont = (XSSFFont)workbook.CreateFont();
                    hFont.FontHeightInPoints = 12;
                    hFont.FontName = "Arial";

                    hFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
                    cellStyle.SetFont(hFont);

                    var sheet = workbook.CreateSheet("sheet1");

                    var headerRow = sheet.CreateRow(0);

                    //Create header row
                    for (int i = 0; i < headers.Length; i++)
                    {
                        ICell cell = headerRow.CreateCell(i);
                        cell.SetCellValue(headers[i].ToString());
                    }

                    //dump data into the excel
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        IRow row = sheet.CreateRow(i + 1);
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            ICell cell = row.CreateCell(j);
                            cell.SetCellValue(dt.Rows[i][headers[j]].ToString());
                        }
                    }
                    string fileName = "temp" + DateTime.Now.ToString("yyyyMMddHHmmssfff")+ ".xlsx";
                    string filePath = System.Web.HttpContext.Current.Server.MapPath("~/Temp/");
                    filePath += fileName;

                    using (FileStream FWriteStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                    {
                        workbook.Write(FWriteStream);
                    }

                    //write excel into memory stream
                    using (var exportData = new MemoryStream())
                    {
                        using (FileStream FreadStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                        {
                            byte[] bytes = new byte[FreadStream.Length];
                            FreadStream.Read(bytes, 0, (int)FreadStream.Length);

                            FreadStream.Seek(0, SeekOrigin.Begin);
                            exportData.Write(bytes, 0, (int)FreadStream.Length);


                            HttpResponseMessage message = new HttpResponseMessage();
                            message.Content = new ByteArrayContent(bytes.ToArray());
                            message.Content.Headers.Add("x-filename", "temp.xlsx");
                            message.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                            message.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                            message.Content.Headers.ContentDisposition.FileName = "temp.xlsx";
                            message.StatusCode = HttpStatusCode.OK;
                            return message;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }

angular service code:

    ExportReport(CommentSpecificationModel: any, SelectedColumns: any) {
            return Observable.create((observer: any) => {
                debugger;
                let req = new XMLHttpRequest();
                req.open('POST', apiBaseUrl ;
                let ReportModel = {
                    "CommentsToExport" : CommentSpecificationModel, 
                    "SelectedColumns" : SelectedColumns
                }


                req.setRequestHeader("Content-Type", "application/json");
                req.withCredentials = true;
                req.responseType = "blob";
                req.onreadystatechange = function () {
                    if (req.readyState == 4 && req.status == 200) {
                        debugger;
                        var contentType = req.getResponseHeader("content-type");
                        var blob = new Blob([req.response], { type: contentType });
                        observer.next(blob);
                        observer.complete();
                    }
                };
                req.send(JSON.stringify(ReportModel));
            });
        }
编辑:


我发现我使用的代码中没有任何错误,问题在于chrome上安装的mcAfee endpoint security。如何使我的下载与安全软件兼容?

好的,在浏览之后,通过禁用chrome上的McAfee插件,我可以让excel下载正常工作

ExportDatatoExcel() {
        debugger;
        this.reportService.ExportSpecificationCommentReport(this.SpecificationCommentsModel, this.SelectedColumns).
            subscribe((data: any) => {
                debugger;
                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(data);
                link.download = "Export.xlsx";
                link.click();
            }
            );
    }