简体   繁体   English

使用asp.net mvc导出到excel函数时,带有响应对象的nullreference异常

[英]nullreference exception with reponse object in export to excel function using asp.net mvc

I have got the below method that will do export to excel, My aim is when I click the link on web page I need to export the data from kendo ui grid to excel along with that asp.net MVC4 for that purpose I have written below method..... 我有以下将导出到excel的方法,我的目的是当我单击网页上的链接时,出于这个目的,我需要将kendo ui网格中的数据与该asp.net MVC4一起导出到excel。方法.....

the below method is action method that will call when I click exporttoexcel action link on view 下面的方法是当我单击视图上的exporttoexcel操作链接时将调用的操作方法

    public ActionResult ExportToExcel()
    {

        byte[] file;
        string targetFilename = string.Format("{0}-{1}.xlsx", "Generated", "excel");

        DataTable dt = common.CreateExcelFile.ListToDataTable(GetSearchDraftPRResults());
        common.CreateExcelFile excelFileForExport = new CreateExcelFile();
        file = excelFileForExport.CreateExcelDocumentAsStream(dt, targetFilename);
        Response.Buffer = true;
        return File(file, "application/vnd.ms-excel", targetFilename);          
    }

and the below method is for creating excel document 下面的方法是用于创建excel文档

    public byte[] CreateExcelDocumentAsStream(DataTable dt, string filename)
    {

        DataSet ds = new DataSet();
        ds.Tables.Add(dt);
        System.Web.HttpResponse Response = null;

        System.IO.MemoryStream stream = new System.IO.MemoryStream();
        using (SpreadsheetDocument document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook, true))
        {
            WriteExcelFile(ds, document);
        }
        stream.Flush();
        stream.Position = 0;

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";

        Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
        Response.AddHeader("content-disposition", "attachment; filename=" + filename);
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        byte[] data1 = new byte[stream.Length];
        stream.Read(data1, 0, data1.Length);
        return stream.ToArray();
    }

but when I click the action link I am getting error NullReferenceexpection at this line 但是,当我单击操作链接时,此行出现错误NullReferenceexpection

   Response.Clear();

I am not sure why I am getting this this exception and I am using open xml dll for export to excel 我不确定为什么会收到此异常,并且我正在使用打开的xml dll导出到excel

I am not sure about this procedure, Is this is right way for export to excel functionality .. would any one pls guide me in correct direction ... 我不确定此过程,这是导出到excel功能的正确方法吗..任何人都可以指导我正确的方向...

Would any one please help on this that would be very grateful to me 有人可以帮忙吗,我会非常感激

many Thanks in advance.... 提前谢谢了....

You really don't need to mess with Response in the CreateExcelDocumentAsStream() method. 您真的不需要在CreateExcelDocumentAsStream()方法中弄乱Response As the name implies, the only responsibility of the method is to create the Excel file and return it as a byte[] array. 顾名思义,该方法的唯一职责是创建Excel文件并将其作为byte[]数组返回。

It's the (MVC) Action responsibility then to set the appropriate response headers and behavior to accommodate the client (Web Browser, in this case) needs. 然后,(MVC)操作负责设置适当的响应头和行为,以适应客户端(在这种情况下为Web浏览器)的需求。

Also, when you return a FileResult , it takes care of setting the Http response, no Response.Clear() or Response.Buffer needed in the Action as well. 同样,当您返回FileResult ,它会负责设置Http响应,在Action中也不需要Response.Clear()Response.Buffer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM