在 C# 中创建、插入文本并保存 Word 文档
Create, insert text and save a Word doc in C#
我发现了大量关于创建 word 文档实例、插入各种文本和格式的有用文档,但找不到任何东西来保存尚未以编程方式创建和打开的文档。
基本上我想创建一个 docx 文件并用富文本框中的文本填充它。使用我在 找到的代码,如果我首先创建一个文档,我就能实现这一点。但是,尽管建议使用 _document.SaveAs() (它不存在 - 我猜是版本差异)或 .Save() 如果文件不存在,据说会提示 SaveAs 对话框,但我总是会收到类型不匹配错误。所以如果我预先创建要使用的文件,这就是工作代码:
OpenFileDialog SDO = new OpenFileDialog();
SDO.ShowDialog();
Microsoft.Office.Interop.Word._Application oWord;
object oMissing = Type.Missing;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = false;
oWord.Documents.Open(SDO.FileName);
oWord.Selection.TypeText(richTextBox1.Text);
oWord.ActiveDocument.Save();
oWord.Quit();
现在人们会假设删除 OpenFileDialogue Documents.Open 的行将在某种程度上保存在 C# 中创建的新文件,但是即使使用:
Microsoft.Office.Interop.Word._Application oWord;
object oMissing = Type.Missing;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = false;
SaveFileDialog SD = new SaveFileDialog();
SD.Filter = "Word File |*.docx";
SD.Title = "Save File";
SD.ShowDialog();
oWord.Documents.Save(SD.FileName,WdNewDocumentType.wdNewXMLDocument);
oWord.Selection.TypeText(richTextBox1.Text);
oWord.ActiveDocument.Save();
oWord.Quit();
我看到的其他示例打开文档以便您可以自己保存它,但我需要它在没有任何人为干预的情况下保存,除了选择一个文件名。
感谢任何帮助,也排除了像 spire 和 gem 这样的第三方 dll 的选项,所以不是一个选项:(
如果有人有创建和保存在程序 运行 之前不存在的 word 文档的简单示例,我将不胜感激。
我就是这样做的。
app = new Word.Application();
object oMissing = System.Reflection.Missing.Value;
Word._Document oDoc = app.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
.....
app.ActiveDocument.SaveAs2(fileName);
其中文件名是我想要的文件名。当我最初这样做时,我发现有很多未记录(因此不受支持)的功能。 SaveAs2 就是其中之一!但它确实有效。
The Microsoft MSDN documentation has tons of useful guides and examples.
您将要包括:
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Tools.Word;
然后申报你的申请:
Word.Application app = new Word.Application();
Word.Document doc = app.Documents.Add();
有两种方法可以保存这些文档:
我发现了大量关于创建 word 文档实例、插入各种文本和格式的有用文档,但找不到任何东西来保存尚未以编程方式创建和打开的文档。
基本上我想创建一个 docx 文件并用富文本框中的文本填充它。使用我在
OpenFileDialog SDO = new OpenFileDialog();
SDO.ShowDialog();
Microsoft.Office.Interop.Word._Application oWord;
object oMissing = Type.Missing;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = false;
oWord.Documents.Open(SDO.FileName);
oWord.Selection.TypeText(richTextBox1.Text);
oWord.ActiveDocument.Save();
oWord.Quit();
现在人们会假设删除 OpenFileDialogue Documents.Open 的行将在某种程度上保存在 C# 中创建的新文件,但是即使使用:
Microsoft.Office.Interop.Word._Application oWord;
object oMissing = Type.Missing;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = false;
SaveFileDialog SD = new SaveFileDialog();
SD.Filter = "Word File |*.docx";
SD.Title = "Save File";
SD.ShowDialog();
oWord.Documents.Save(SD.FileName,WdNewDocumentType.wdNewXMLDocument);
oWord.Selection.TypeText(richTextBox1.Text);
oWord.ActiveDocument.Save();
oWord.Quit();
我看到的其他示例打开文档以便您可以自己保存它,但我需要它在没有任何人为干预的情况下保存,除了选择一个文件名。
感谢任何帮助,也排除了像 spire 和 gem 这样的第三方 dll 的选项,所以不是一个选项:(
如果有人有创建和保存在程序 运行 之前不存在的 word 文档的简单示例,我将不胜感激。
我就是这样做的。
app = new Word.Application();
object oMissing = System.Reflection.Missing.Value;
Word._Document oDoc = app.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
.....
app.ActiveDocument.SaveAs2(fileName);
其中文件名是我想要的文件名。当我最初这样做时,我发现有很多未记录(因此不受支持)的功能。 SaveAs2 就是其中之一!但它确实有效。
The Microsoft MSDN documentation has tons of useful guides and examples.
您将要包括:
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Tools.Word;
然后申报你的申请:
Word.Application app = new Word.Application();
Word.Document doc = app.Documents.Add();
有两种方法可以保存这些文档: