如何在桌面应用程序中将文本数据保存为word文件 [英] how to save text data as a word file in Desktop application

查看:64
本文介绍了如何在桌面应用程序中将文本数据保存为word文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将富文本框数据保存在word文件中.
我正在使用code.it可以正常工作,但只能在某些系统中使用.
当我在某些计算机上保存应用程序时出现错误.

我正在使用此代码

Hi, I want to save rich text box data in a word file.
I am using code.it works fine but only in some systems.
when i am save the application in some computers gives a error.

I am using this code

object missing = System.Reflection.Missing.Value;
               object Visible = true;
               object start1 = 0;
               object end1 = 0;

               Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
               Microsoft.Office.Interop.Word.Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
               Microsoft.Office.Interop.Word.Range rng = adoc.Range(ref start1, ref missing);
               SaveFileDialog saveFileDialog1 = new SaveFileDialog();
               saveFileDialog1.InitialDirectory = @"C:\";
               saveFileDialog1.Title = "Save word Files";
               saveFileDialog1.CheckFileExists = false;
               saveFileDialog1.CheckPathExists = true;
               saveFileDialog1.DefaultExt = "docx";
               saveFileDialog1.Filter = "Docx files (*.docx)|*.doc|All files (*.*)|*.*";
               saveFileDialog1.FilterIndex = 2;
               saveFileDialog1.OverwritePrompt = true;
               saveFileDialog1.RestoreDirectory = true;

               if (saveFileDialog1.ShowDialog() == DialogResult.OK)
               {
                   rng.Font.Name = "Calibri (Body)";
                   rng.InsertAfter(rtbenglishfile.Text);
                   object filename = saveFileDialog1.FileName;
                   adoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                   //WordApp.Visible = true;
                   WordApp.Visible = false;
                   adoc.Close(ref missing, ref missing, ref missing);
                   WordApp.Quit(ref missing, ref missing, ref missing);
                   WordApp = null;



这是Windows错误或程式错误.

在此先感谢.



It is a Windows Error or pro-grammatically error.

Thanks in Advance.

推荐答案

我认为该错误与所安装的Word版本或是否已安装Word有关.您确认不是这种情况吗?如果您的代码没有更改,那么可以正常运行的计算机与不能正常运行的计算机之间必须有所不同.
I would think the error relates to what version of Word is installed, or if it''s installed at all. Have you confirmed this is not the case ? If your code does not change, then something must be different between the machines that work and those that do not.


您的代码仅在安装了Office的系统上有效,因为您要绑定到Office库(通过互操作层).您必须随应用程序一起提供Office库,然后在目标系统上注册它们才能使它起作用.问题是这违反了Microsoft EULA for Office.

另外,MS库是特定于版本的.如果您在装有Office 2010的计算机上进行开发,则您的互操作程序集将与2010 Office dll绑定在一起.如果您尝试在装有Office 2007的计算机上安装,则您的应用可能会失败.使用Interop绑定到Office库仅适用于对桌面环境具有严格的,与版本相关的控制,或者必须自动执行Office应用程序本身的应用程序.

由于从您的代码中看起来您正在使用2007或更高版本的DOCX格式,因此您应该直接创建/读取/写入docx格式.这只是一个压缩的xml文件.此处有一篇文章,展示了如何使用OpenXML库执行此操作. [
Your code will only work on systems where Office is installed because you are binding to the Office libraries (through an interop layer). You would have to deliver the Office libraries with your application and register them on the target system to get this to work. Problem is that is a violation of Microsoft EULA for Office.

Plus, MS libraries are version specific. If you develop this on a machine with Office 2010, your interop assemblies are tied to the 2010 Office dll''s. If you try to install on a machine with Office 2007, your app will likely fail. Binding to the Office libraries using Interop is only good for apps where you have tight, version-dependent control over the desktop environment or you must automate an Office application itself.

Since it looks like from your code you are using 2007 or higher DOCX format, you should just create/read/write the docx format directly. It is just a zipped xml file. There is an article on here which shows how to do it with the OpenXML libraries. [^] You can also look up the spec for the docx format (The whole suite is the Office Open XML formats). It is an open standard and there is a lot of info out there on it.


这篇关于如何在桌面应用程序中将文本数据保存为word文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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