导出到excel打开它
我目前在我的应用程序中有一个选项来导出到Excel。 它将数据保存到磁盘中的文件。
我想打开Excel来显示数据。 你们有没有人知道这是可能的,以及如何做到这一点?
我想我可以打开我已经保存到磁盘的文件,但是最好不要将文件保存到磁盘。
我假设你正在使用Interop。 只需将应用程序设置为可见即可。
excelApp.Visible = true;
哪里
InteropExcel.Application excelApp;
只要记住仍然释放所有的COM引用,以便您的应用程序不会持有Excel的句柄。 这可能会导致您的Excel文件是只读的。
你目前如何创build你的文件?
如果你正在使用Excel引擎或POI(或者它的缩写)来创build一个XLS / XLSX,那么这只是一个不保存工作簿并使实例可见的情况(如上所述)?
如果你不依赖或不想依赖于Excel(即使用第三方库,如Syncfusion来创build文件),或者只是以excel可读的格式(如CSV)输出数据,那么我想你被困在一个基于文件的操作…
至于临时文件比未保存的文件更容易…数据需要在任一实例中创build,无论是简单的CSV还是编码的Excel单元格群体,所以我不太明白这是什么意思。
参考下面的代码打开excel而不保存
System.Web.HttpContext.Current.Response.Clear(); 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); Gridview1.RenderControl(htmlWrite); //style to format numbers to string string style = @"<style> .textmode { } </style>"; System.Web.HttpContext.Current.Response.Write(style); System.Web.HttpContext.Current.Response.Output.Write(stringWrite.ToString()); System.Web.HttpContext.Current.Response.Flush(); System.Web.HttpContext.Current.Response.End();