如何使用 C#、.NET 将文本写入 word 文件 [英] How to write text into a word file using C#, .NET
问题描述
我正在尝试使用 c# 编写一些文本并将其附加到 word 文件中,但是,我无法获得预期的结果.你能帮我解决这个问题吗?
I am trying to write and append some text to a word file using c#, however, I am unable to get expected results. Could you please help me out of this ?
下面是我的代码-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WFA1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//FileStream F = new FileStream("testdoc2.docx", FileMode.OpenOrCreate, FileAccess.ReadWrite);
Console.WriteLine("Sourav");
string filename = @"C:\\Desktop\\myfile.docx";
Console.WriteLine(filename);
try
{
using (FileStream fs = File.OpenWrite(filename))
{
Byte[] content = new UTF8Encoding(true).GetBytes("Hello I am learning C#");
fs.Write(content, 0, content.Length);
}
}
catch (Exception Ex)
{
Console.Write(e.ToString());
}
}
}
}
上面的代码是后面的windows窗体应用程序代码.我已经使用 FileStream 类来写入数据.但是我面临以下问题:-
The above code is a windows form application code behind. I have use FileStream class to write data. However I am facing below issues :-
- 没有创建文件
- 代码一直运行,直到我手动停止.
因此,我也尝试了以下方法,并且能够将文本写入文件.
Hence, I tried the below approach too, and I was able to write text to the file.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Microsoft.Office.Interop.Word;
namespace WFA1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open("C:\\Users\\SS5014874\\Desktop\\testdoc1.docx");
object missing = System.Reflection.Missing.Value;
//string s = "Hi";
//Console.WriteLine(s);
doc.Content.Text = textBox1.Text.ToString();
doc.Save();
doc.Close(ref missing);
app.Quit(ref missing);
}
}
}
然而,我仍然没有得到预期的结果.以下是我的问题:-
However, still I did not get expected results. below are my issues:-
- 无法附加任何文本.请让我知道如何使用这种方法追加.有没有我们可以调用的方法来附加文本.
- 即使我使用了 Quit 方法,应用程序也不会退出,直到我手动退出.
另外,在哪里可以找到 Microsoft.Office.Interop.Word
Also, where can I find the list of methods of class Microsoft.Office.Interop.Word
请让我知道任何其他信息.
Please let me know for any other information.
推荐答案
请点击此链接 https://support.microsoft.com/en-us/kb/316384
或
你可以试试这个.
添加以下指令:
使用 Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;
使用 System.Reflection;
using System.Reflection;
用于添加 Microsoft.Office.Interop.Word;
For adding Microsoft.Office.Interop.Word;
- 在项目"菜单上,单击添加引用".
- 在 COM 选项卡上,找到 Microsoft Word 对象库,然后选择.
(就我而言,它是 Microsoft Word 12.0 对象库)
(In my case it is Microsoft Word 12.0 Object Library)
使用这些代码.我正在尝试像您的代码一样维护这些代码 -
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(@"e:\testdoc1.docx");
object missing = System.Reflection.Missing.Value;
doc.Content.Text += textBox1.Text;
app.Visible = true; //Optional
doc.Save();
this.Close();
}
这篇关于如何使用 C#、.NET 将文本写入 word 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!