Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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
WebApi使用EPPlus excel文件下载-打开时出错-无法读取内容-修复_Excel_Asp.net Web Api_Epplus - Fatal编程技术网

WebApi使用EPPlus excel文件下载-打开时出错-无法读取内容-修复

WebApi使用EPPlus excel文件下载-打开时出错-无法读取内容-修复,excel,asp.net-web-api,epplus,Excel,Asp.net Web Api,Epplus,在我的ExtJSWebClient上,我点击一个按钮,它下载一个excel模板并填充数据。当我点击CHROME弹出窗口打开excel文件时,它会给我下面的excel错误消息。有人知道我怎么解决这个问题吗? 实际的文件在我看来很好 这是我的webapi代码 public HttpResponseMessage CreatePosSheet(string cName, string calcMethod, string username, int cid, HttpReques

在我的ExtJSWebClient上,我点击一个按钮,它下载一个excel模板并填充数据。当我点击CHROME弹出窗口打开excel文件时,它会给我下面的excel错误消息。有人知道我怎么解决这个问题吗? 实际的文件在我看来很好

这是我的webapi代码

        public HttpResponseMessage CreatePosSheet(string cName, string calcMethod, string username, int cid, HttpRequestMessage Request)
    {
        string templatePath = HttpContext.Current.Server.MapPath(@"~\tempLocation\");
        string templateFileName = System.Configuration.ConfigurationManager.AppSettings["TemplateFileName"];
        string templateName = string.Format("{0}{1}", templatePath, templateFileName);
        FileInfo xlTemplate = new FileInfo(templateName);

        HttpResponseMessage response;
        response = Request.CreateResponse(HttpStatusCode.OK);
        MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("application/octet-stream");
        //MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.Content = new ByteArrayContent(ExcelSheet(xlTemplate, cName, calcMethod, username, cid));
        response.Content.Headers.ContentType = mediaType;
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        response.Content.Headers.ContentDisposition.FileName = "Orders.xlsx";

        return response;
    }
这是ExcelSheet代码

        public byte[] ExcelSheet(FileInfo xlTemplate, string cName, string calcMethod, string username, int cid)
    {
        object[] tempObj = Orders(username, cid);
        List<TradeDetailDTO> tList = new List<TradeDetailDTO>();
        List<TempClass> acctList = new List<TempClass>();
        List<TempClass2> sectorList = new List<TempClass2>();
        tList = tempObj[0] as List<TradeDetailDTO>;
        acctList = tempObj[1] as List<TempClass>;
        sectorList = tempObj[2] as List<TempClass2>;

        #region set column variables

        int colAcctNum = 1;
        int colAcctDesc = 2;
        int colAcctTrdLvl = 5;


        int colCname = 6;
        int rowCname = 15;

        #endregion

        using (var package = new ExcelPackage(xlTemplate))
        {

            var worksheet = package.Workbook.Worksheets["Data"];
            var wkshtFormula = package.Workbook.Worksheets["Formulas"];
            wkshtFormula.Cells[rowCname, colCname].Value = cName;

            package.Save();
            return package.GetAsByteArray();
        }
    }
public byte[]ExcelSheet(FileInfo xlTemplate,string cName,string calcMethod,string username,int cid)
{
object[]tempObj=订单(用户名,cid);
List tList=新列表();
List acctList=新列表();
列表扇区列表=新列表();
tList=tempObj[0]作为列表;
acctList=tempObj[1]作为列表;
sectorList=tempObj[2]作为列表;
#区域集列变量
int colAcctNum=1;
int colAcctDesc=2;
int colAcctTrdLvl=5;
int colCname=6;
int rowCname=15;
#端区
使用(var包=新的ExcelPackage(xlTemplate))
{
var工作表=包.工作簿.工作表[“数据”];
var wkshtFormula=package.Workbook.Worksheets[“公式”];
wkshtFormula.Cells[rowCname,colCname].Value=cName;
package.Save();
return package.GetAsByteArray();
}
}

您必须删除对
.Save()
的调用,因为它会作为副作用关闭pck。有关更多信息,请参见此:


什么是
ExcelSheet
?您好@Ernie我添加了上面的其余代码,您是否看到任何错误?