Export to excel opening it
我目前在我的应用中可以选择导出到Excel。它将数据保存到磁盘上的文件中。
我想改为打开Excel并显示数据。你们中的任何人都知道这是否可行以及如何实现吗?
我想我可以打开已经保存到磁盘的文件,但是最好根本不将文件保存到磁盘。
我假设您正在使用Interop。只需将应用程序设置为可见。
1 | excelApp.Visible = true; |
其中
1 | InteropExcel.Application excelApp; |
请记住,仍要释放所有COM引用,以便您的应用程序也不会保留Excel的句柄。这可能会导致您的Excel文件为只读。
您当前如何创建文件?
如果您使用Excel引擎或POI(或任何缩写)创建XLS / XLSX,那只是不保存工作簿并使实例可见(如上所述)的一种情况? >
如果您不依赖或不想依赖Excel(即使用诸如Syncfusion之类的第三方库来创建文件)或只是以excel可读格式(如CSV)输出数据,那么我想您\\'坚持使用基于文件的操作...
至于临时文件比未保存的文件要容易...在两种情况下都需要创建数据,无论是简单的CSV还是Excel单元格的编码填充,因此我不太清楚这是什么意思那
请参阅下面的代码以打开excel而不保存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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(); |