为什么浏览器不能显示出EPPlus US preadsheet? [英] Why the browser can't show an EPPlus spreadsheet?

查看:239
本文介绍了为什么浏览器不能显示出EPPlus US preadsheet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了很多完整不成功选项。我需要的是在服务器中创建A S preadsheed,用户得到它在一个asp.net MVC 5环境的浏览器。

在服务器上的code是:

  [HttpPost]
公众的ActionResult ExportToExcel([ModelBinder的(typeof运算(DevEx pressEditorsBinder))报告的数据)
    {
  ExcelPackage PCK =新ExcelPackage();
  变种WS = pck.Workbook.Worksheets.Add(样品1);  ws.Cells [A1] =价值样品1。
  。ws.Cells [A1] = Style.Font.Bold真实;
  VAR形状= ws.Drawings.AddShape(Shape1,eShapeStyle.Rect);
  shape.SetPosition(50,200);
  shape.SetSize(200,100);
  shape.Text =样品1文字文字文字;  如果(data.PDFFileName == NULL)data.PDFFileName =S preadsheet.xlsx
  data.PDFFileName = Path.GetFileNameWithoutExtension(data.PDFFileName)+的.xl​​sx;  FileContentResult结果=新FileContentResult(pck.GetAsByteArray(),应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet);
  result.FileDownloadName = data.PDFFileName;
  返回结果;
}

的看法是

  @ Ajax.ActionLink(创建小号preadsheet,ExportToExcel,GraphReport,空,新AjaxOptions {列举HTTPMethod =POST},{新@class =BTN BTN-警告})

在Chrome的结果是:

 远程地址:[:: 1]:49711请求URL:http://本地主机:49711 / GraphReport / ExportToExcel请求方法:POST状态code:200 OK响应头缓存控制:私人,S-达到maxage = 0内容处置:附件;文件名= S preadsheet.xlsx内容长度:3561内容类型:应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet日期:星期一,2015年6月29日22时54分38秒GMT服务器:Microsoft-IIS / 8.0的X ASPNET-版本:4.0.30319的X AspNetMvc-版本:5.2的X已启动方式:ASP.NETX-SourceFiles:=?UTF-8?B?QzpcVXNlcnNcbWNhc3Ryb1xEb2N1bWVudHNcVmlzdWFsIFN0dWRpbyAyMDEzXFByb2plY3RzXE1jU29mdFxDVlNcQmVlaGl2ZU1WQ1xHcmFwaFJlcG9ydFxFeHBvcnRUb0V4Y2Vs?=请求头接受:*/*接受编码:gzip,紧缩接受语言:PT-BR,PT; Q = 0.8,EN-US; Q = 0.6,连接; Q = 0.4连接:保持活动内容长度:31内容类型:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8

的code是有回应:

<$p$p><$c$c>PKÓÝFCjkh'¬[Content_Types].xmlµÛN!_eíYh½0ÆtÛjb}f»¤ÂÐþ½,[ij¢I{5?3ß?f½5Õ#iï6åV¡^i·jØûò©¾e%p
禾°-æ³eª\\ EA]JáNZ'&GT; ZHyW\\ A

为什么它没有被跨preTED为下载附加的文件?


解决方案

这是因为你无法通过AJAX调用下载文件。

解决方案:创建一个表单,使用POST方法,提交表单。当服务器页面响应请求,写的MIME类型文件的响应头,它将present一个下载对话框。您可能需要从浏览如果服务器返回一个错误错误页面使用 IFRAME 来prevent您的网站。

很多这样的问题,他们都有答案。

<一个href=\"http://stackoverflow.com/questions/12955266/ajax-call-to-download-file-returned-from-restful-service\">Ajax调用下载文件从RESTful服务

退换

处理来自阿贾克斯后文件下载

I'm trying a lot of options with complete unsuccess. My need is create a spreadsheed in the server and the user gets it in the browser in a asp.net MVC 5 environment.

In the server the code is:

[HttpPost]    
public ActionResult ExportToExcel([ModelBinder(typeof(DevExpressEditorsBinder))] Reports data)
    {
  ExcelPackage pck = new ExcelPackage();
  var ws = pck.Workbook.Worksheets.Add("Sample1");

  ws.Cells["A1"].Value = "Sample 1";
  ws.Cells["A1"].Style.Font.Bold = true;
  var shape = ws.Drawings.AddShape("Shape1", eShapeStyle.Rect);
  shape.SetPosition(50, 200);
  shape.SetSize(200, 100);
  shape.Text = "Sample 1 text text text";

  if (data.PDFFileName == null) data.PDFFileName = "Spreadsheet.xlsx";
  data.PDFFileName = Path.GetFileNameWithoutExtension(data.PDFFileName) + ".xlsx";

  FileContentResult result = new FileContentResult(pck.GetAsByteArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  result.FileDownloadName = data.PDFFileName;
  return result;
}

The view is

@Ajax.ActionLink("Created Spreadsheet", "ExportToExcel", "GraphReport", null, new AjaxOptions { HttpMethod = "POST" }, new { @class="btn btn-warning" })

The result in Chrome is:

Remote Address:[::1]:49711

Request URL:http://localhost:49711/GraphReport/ExportToExcel

Request Method:POST

Status Code:200 OK

Response Headers

Cache-Control:private, s-maxage=0

Content-Disposition:attachment; filename=Spreadsheet.xlsx

Content-Length:3561

Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Date:Mon, 29 Jun 2015 22:54:38 GMT

Server:Microsoft-IIS/8.0

X-AspNet-Version:4.0.30319

X-AspNetMvc-Version:5.2

X-Powered-By:ASP.NET

X-SourceFiles:=?UTF-8?B?QzpcVXNlcnNcbWNhc3Ryb1xEb2N1bWVudHNcVmlzdWFsIFN0dWRpbyAyMDEzXFByb2plY3RzXE1jU29mdFxDVlNcQmVlaGl2ZU1WQ1xHcmFwaFJlcG9ydFxFeHBvcnRUb0V4Y2Vs?=

Request Headers

Accept:*/*

Accept-Encoding:gzip, deflate

Accept-Language:pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4

Connection:keep-alive

Content-Length:31

Content-Type:application/x-www-form-urlencoded; charset=UTF-8

The code is there in response:

PKÓÝFCjkh'¬[Content_Types].xmlµÛN!_eíYh½0ÆtÛjb}f»¤ÂÐþ½,[ij¢I{5?3ß?f½5Õ#iï6åV¡^i·jØûò©¾e%p
wØ°-æ³eª\ë¨a]JáNZ ´>ZHyW"\Ã

Why it is not being interpreted as download attached file?

解决方案

It's because you cannot download file via AJAX call.

Solution: Create a form, use the POST method, submit the form. When the server page responds to the request, write a response header for the mime type of the file, and it will present a download dialog. You may need to use an iframe to prevent your site from navigating to error page if server returns an error.

Many of questions like this, they all have answers..

Ajax call to download file returned from RESTful service

Handle file download from ajax post

这篇关于为什么浏览器不能显示出EPPlus US preadsheet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆