如何在Microsoft Excel中导出excel数据而不显示警告消息。 [英] How to export excel data without warning message in Microsoft in excel sheet .

查看:316
本文介绍了如何在Microsoft Excel中导出excel数据而不显示警告消息。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打开excel表后,我在excel表中导出数据,因此请求Microsoft错误filename.xls的文件格式和扩展名不匹配。文件可能已损坏或不安全。除非您信任其源,否则不要打开它。你还想打开它吗?

i想要帮助基础解决方案



我尝试了什么:



  string  attachment =  附件; filename = + Session [  name]。ToString()。替换(< span class =code-string>  )+    -  + Session [  name1]。ToString()+    -  + Session [  name2]。 ToString()+  。xls; 

System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new Dat aGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();

/// /获取控件的HTML。
dgGrid.RenderControl(hw);
/// /将HTML写回浏览器。
/// / Response.ContentType = application / vnd.ms-excel;
// Response.ContentType =application / vnd.ms-excel;
// Response.AppendHeader(Content-Disposition,附件);
// this.EnableViewState = false;
// Response.Write(tw.ToString());
// Response.End();

解决方案

你'不生成Excel文件。您正在生成HTML片段,但告诉浏览器它是Excel文件。然后,Excel将尽力将HTML转换为正确的Excel文件,但您几乎无法控制它是如何做到的,并且您当然无法摆脱文件内容与扩展名不匹配的警告。



相反,您需要创建一个真正的Excel文件。但是,由于这是一个ASP.NET应用程序,因此无法使用Interop:

Microsoft目前不建议,也不支持从任何无人参与的非交互式客户端应用程序或组件(包括ASP,ASP.NET)自动化Microsoft Office应用程序,DCOM和NT服务),因为Office在此环境中运行时可能会出现不稳定的行为和/或死锁。





有多种方式在服务器上创建Excel电子表格而不使用Office互操作。例如:


您无法使用简单的流编写器创建Excel文件。您需要使用OLEDb或 Microsoft.Office.Interop.Excel命名空间( ) [ ^ ]。

I export datat in excel sheet after i open excel sheet so ask Microsoft error that "The File Format and Extension of filename.xls dont match. The file could be corrupted or unsafe.unless you trust its source ,dont open it. Do you want to open it anyway???
i want to urgnt base Solution

What I have tried:

string attachment = "attachment; filename=" + Session["name"].ToString().Replace(" ", "") + "-" + Session["name1"].ToString() + "-" + Session["name2"].ToString() + ".xls";

                System.IO.StringWriter tw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                DataGrid dgGrid = new DataGrid();
                dgGrid.DataSource = dt;
                dgGrid.DataBind();

                ////Get the HTML for the control.
                dgGrid.RenderControl(hw);
                ////Write the HTML back to the browser.
                ////Response.ContentType = application/vnd.ms-excel;
                //Response.ContentType = "application/vnd.ms-excel";
                //Response.AppendHeader("Content-Disposition", attachment);
                //this.EnableViewState = false;
                //Response.Write(tw.ToString());
                //Response.End();

解决方案

You're not generating an Excel file. You're generating an HTML fragment, but telling the browser that it's an Excel file. Excel will then do its best to convert the HTML to a proper Excel file, but you have very little control over how it does that, and you certainly can't get rid of the warning that the file content doesn't match the extension.

Instead, you need to create a real Excel file. However, as this is an ASP.NET application, you can't use Interop:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.



There are various ways to create Excel spreadsheets on the server without using Office interop. For example:


You cannot create Excel files with simple stream writers. You need to use OLEDb or Microsoft.Office.Interop.Excel namespace ()[^].


这篇关于如何在Microsoft Excel中导出excel数据而不显示警告消息。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆