导出excel打开它

时间:2013-08-22 22:17:00

标签: c# .net excel export-to-excel

我目前在我的应用中有一个选项可以导出到Excel。它将数据保存到磁盘中的文件中。

我想打开Excel并显示数据。你们中的任何人都知道这是否可能以及如何实现它?

我想我可以打开我已保存到磁盘的文件,但最好不要将文件保存到磁盘上。

3 个答案:

答案 0 :(得分:2)

我假设你正在使用Interop。只需将应用程序设置为可见。

excelApp.Visible = true;

,其中

InteropExcel.Application excelApp;

请记住仍然释放所有COM引用,以便您的应用程序不会保留Excel的句柄。这可能会导致您的Excel文件为只读。

答案 1 :(得分:1)

您目前是如何创建文件的?

如果您正在使用Excel引擎或POI(或其他任何缩写)来创建XLS / XLSX,那么只是不保存工作簿并使实例可见(如上所述)?

如果您不依赖或不想依赖Excel(即使用Syncfusion等第三方库来创建文件),或者只是以Excel等Excel格式输出您的数据,那么我想你是坚持使用基于文件的操作......

对于临时文件比未保存的文件更容易...数据需要在任何一个实例中创建,无论是简单的CSV还是Excel单元格的编码群,所以我不太明白这是什么意思

答案 2 :(得分:0)

参考下面的代码打开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();