简体   繁体   中英

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.....

the below method is action method that will call when I click exporttoexcel action link on view

    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

    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

   Response.Clear();

I am not sure why I am getting this this exception and I am using open xml dll for export to 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 ...

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. As the name implies, the only responsibility of the method is to create the Excel file and return it as a byte[] array.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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