如何避免在打开程序生成Excel文件警告 [英] How to avoid warning on opening a programmatically generated Excel file

查看:281
本文介绍了如何避免在打开程序生成Excel文件警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用生成XML编程Excel文件。在随后尝试打开该文件时,我收到以下消息:



你试图打开,'MyFile.xls的文件,是在一个不同的格式通过文件扩展名指定。验证该文件没有损坏,是来自可信来源打开文件之前,你要立即打开该文件?



当用户按下是,该文件打开没有问题。但这个警告,需要做出额外的点击是烦人。 ?能否请你建议我怎么能摆脱它



这是我的代码的一部分:

  VAR流= File.Create(路径); 
变种W =新的XmlTextWriter(流,NULL); //创建一个从预申报数据流的XML作家。

//先写Excel的头
w.WriteStartDocument();
w.WriteProcessingInstruction(MSO的应用,进程id =Excel.Sheet');

w.WriteStartElement(工作表);
w.WriteAttributeString(的xmlns,瓮:架构 - 微软-COM:办公室:电子数据表);
w.WriteAttributeString(的xmlns,O,空,瓮:架构 - 微软-COM:办公室:办公室);
w.WriteAttributeString(的xmlns,X,NULL,瓮:架构 - 微软-COM:办公室:EXCEL);
w.WriteAttributeString(的xmlns,SS,空,瓮:架构 - 微软-COM:办公室:电子数据表);
w.WriteAttributeString(的xmlns,HTML,NULL,http://www.w3.org/TR/REC-html40);

w.WriteStartElement(DocumentProperties);
w.WriteAttributeString(的xmlns,瓮:架构 - 微软-COM:办公室:办公室);
w.WriteEndElement();

//创建工作簿
w.WriteStartElement(ExcelWorkbook);
w.WriteAttributeString(的xmlns,瓮:架构 - 微软-COM:办公室:EXCEL);
w.WriteEndElement();


解决方案

而不是使用一个XmlTextWriter创建电子表格,你倒是使用专门编写创建Excel文件库中可能有更好的运气。否则,你会像那些你所看到的问题,以结束 - 的东西小错误一样的xmlns属性造成您的文件出现损坏。 。图书馆专门编写创建XLS文件就已经解决了这个问题。



EPPlus似乎支持得好:的 http://epplus.codeplex.com/


I generate excel files using xml programmatically. On trying then to open the file, I receive the following message:

"The file you are trying to open, 'MyFile.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"

When the user presses "Yes", the file opens without problems. But this warning and need to make an extra click is annoying. Could you please suggest how I could get rid of it?

This is a part of my code:

        var stream = File.Create(path);
        var w = new XmlTextWriter(stream, null); // Creates the XML writer from pre-declared stream.

        //First Write the Excel Header
        w.WriteStartDocument();
        w.WriteProcessingInstruction("mso-application", "progid='Excel.Sheet'");

        w.WriteStartElement("Workbook");
        w.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:spreadsheet");
        w.WriteAttributeString("xmlns", "o", null, "urn:schemas-microsoft-com:office:office");
        w.WriteAttributeString("xmlns", "x", null, "urn:schemas-microsoft-com:office:excel");
        w.WriteAttributeString("xmlns", "ss", null, "urn:schemas-microsoft-com:office:spreadsheet");
        w.WriteAttributeString("xmlns", "html", null, "http://www.w3.org/TR/REC-html40");

        w.WriteStartElement("DocumentProperties");
        w.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:office");
        w.WriteEndElement();

        // Creates the workbook
        w.WriteStartElement("ExcelWorkbook");
        w.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:office:excel");
        w.WriteEndElement();

解决方案

Instead of using an XmlTextWriter to create your spreadsheet, you'd probably have better luck using a library specifically written to create excel files. Otherwise, you're going to end up with issues like those that you are seeing - minor mistakes in things like xmlns attributes which cause your file to appear corrupted. A library specifically written to create xls files would already have solved this problem.

EPPlus seems to be well-supported: http://epplus.codeplex.com/

这篇关于如何避免在打开程序生成Excel文件警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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