Home » excel » c# – Export to excel opening it

c# – Export to excel opening it

Posted by: admin May 14, 2020 Leave a comment


I currently have an option in my app to export to Excel. It saves the data to a file in disk.

I would like to open Excel instead and show the data. Do any of you know if this is possible and how to accomplish it?

I guess I can just open the file I’m already saving to disk, but it would be just better to not save a file to disk at all.

How to&Answers:

I am assuming you are using Interop. Just set the Application to Visible.

excelApp.Visible = true;


InteropExcel.Application excelApp;

Just remember to still release all the COM references so that your application does not also hold a handle to Excel. That may cause your Excel file to be read-only.


How are you currently creating your file?

If you are using the Excel engine or POI (or what ever thier abbreviation is) to create an XLS / XLSX it would just be a case of not saving the workbook and making the instance visible (as per above)?

If you are not dependant on or do not want to be dependant on Excel (i.e. using 3rd party libraries like Syncfusion to create the file) or just outputting your data in an excel readable format like CSV, then I guess you’re stuck with a file-based operation…

As for a temp file being easier than an unsaved one… the data needs to be created in either instance, either simple CSV or a coded population of Excel cells, so I don’t quite see what is meant by that.


Refer Below code to open excel without saving

    System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=DateTime.Now + ".xls");
    System.Web.HttpContext.Current.Response.Charset = "";

    Response.Cache.SetCacheability(HttpCacheability.NoCache);  // If you want the option to open the Excel file without saving than       

    System.Web.HttpContext.Current.Response.ContentType = "application/vnd.xls";
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);        

    //style to format numbers to string
    string style = @"<style> .textmode { } </style>";