简体   繁体   English

如何将c#应用程序中的文本插入MS Word文档并将其另存为新文件

[英]how to insert text from c# application into MS word document and save it as new file

I have created a desktop application which in that I need to save texts of textbox into into a word document, the document should be created by user and the position of the document would be chosed by user and then the value of textbox should be saved into that. 我创建了一个桌面应用程序,其中需要将文本框的文本保存到Word文档中,该文件应由用户创建,并且文件的位置应由用户选择,然后将文本框的值保存到那。

for example: 例如:

"this is textBox value" “这是textBox的值”

then there is a save button, by clicking that the save window should be opened then user will give a name to that and set its position then click OK finally the text of textbox will be saved into that word document. 然后有一个保存按钮,通过单击应打开保存窗口,然后用户将为其命名并设置其位置,然后单击“ 确定”,最后文本框的文本将保存到该Word文档中。

any idea how to do it? 知道怎么做吗? I have searched allot in google but I couldn't find anything to find the way ... 我已经在Google中搜索了配额,但找不到任何方法...

Word exposes a COM API, search for Word Interop or Office Interop. Word公开COM API,搜索Word Interop或Office Interop。

With this API you may start word, load a document, add to this document, for example at the current cursor position and save the document to a new file. 使用此API,您可以开始单词,加载文档,添加到该文档(例如在当前光标位置)并将文档保存到新文件。

I have found a solution for my own question, I thought to share it if anyone else has faced this kind of problems, here is the code: 我已经为自己的问题找到了解决方案,如果有人遇到这种问题,我想与他人分享,下面是代码:

    private void btnSave_Click(object sender, EventArgs e)
    {


        SaveFileDialog saveFileDialog1 = new SaveFileDialog();

        saveFileDialog1.Filter = "Word document|*.doc";
        saveFileDialog1.Title = "Save the Word Document";
        saveFileDialog1.FileName = "MS word document.docx";

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            File.WriteAllText(saveFileDialog1.FileName,txtResult.Text.ToString());
        }
    }

this works fine, but I want to set font-family and font-size, if anyone knows please share your solution? 这工作正常,但是我想设置字体系列和字体大小,如果有人知道,请分享您的解决方案?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM