在WPF应用程序上管理Word文档

时间:2012-10-10 18:19:07

标签: c# .net wpf

我正在创建一个加载和编辑Word文档的WPF应用程序。代码如下:

            public bool Generate(string path, ProjectVO project)
            {
                saveAs = path;
                projectName = project.Name;
                projectVersion = project.Version;

                object missing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
                //Setup our Word.Document class we'll use.
                Microsoft.Office.Interop.Word.Document aDoc = null;


                // Check to see that file exists
                if (File.Exists((string)fileName))
                {
                    DateTime today = DateTime.Now;

                    object readOnly = false;
                    object isVisible = false;
                    try
                    {

                        //Set Word to be not visible.
                        wordApp.Visible = false;

                        //Open the word document
                        aDoc = wordApp.Documents.Open(ref fileName, ref missing,
                            ref readOnly, ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref missing,
                            ref missing, ref isVisible, ref missing, ref missing,
                            ref missing, ref missing);

                        // Activate the document
                        aDoc.Activate();

                        // Find Place Holders and Replace them with Values.
                        this.FindAndReplace(wordApp, "{Name}", projectName);
                        this.FindAndReplace(wordApp, "{Version}", projectVersion);



                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        //Close the document
                        aDoc.Close(ref missing, ref missing, ref missing);
                        return false;


                    }


                }
                else
                {
                   return false;
                }

                //Save the document as the correct file name.
                aDoc.SaveAs(ref saveAs, 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);




                //Close the document
                aDoc.Close(ref missing, ref missing, ref missing);
                return true;

        }

它可以正常工作,但是当客户端没有Microsoft Word时,不会创建Word文档。有没有办法创建它,并保存在文件夹上,即使客户端无法打开它? 或者有没有办法将其保存为PDF或TXT作为未安装Word的替代形式? 非常感谢!

2 个答案:

答案 0 :(得分:2)

如果客户端计算机没有安装Word,则无法创建Word文档。

您当然可以创建一个txt文件,但不能通过Office Interop创建。

答案 1 :(得分:2)

您可以使用“open xml sdk”生成一个没有单词的docx但不是很容易。

http://www.microsoft.com/en-us/download/details.aspx?id=5124

Sdk包含一个从现有docx文件自动生成代码的工具。

要生成pdf我建议使用“iTextSharp”libray http://itextpdf.com/