0

我正在创建一个加载和编辑 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 时的替代形式?非常感谢!

4

2 回答 2

2

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

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

于 2012-10-10T19:56:06.237 回答
2

您可以使用“open xml sdk”生成不带单词的 docx,但这并不容易。

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

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

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

于 2012-10-10T20:11:25.450 回答