如何使用C#保存doc文件

发布于 2024-12-10 17:58:46 字数 990 浏览 0 评论 0原文

我一直在使用以下代码编写Word文件,但无法存储Word文件。有没有办法使用C#存储word文件?

object oMissing = System.Reflection.Missing.Value;
                object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
                //Start Word and create a new document.
                Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();

            Microsoft.Office.Interop.Word._Document oDoc = new Microsoft.Office.Interop.Word.Document();

            oWord.Visible = true;

            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            //Insert a paragraph at the beginning of the document.
            Microsoft.Office.Interop.Word.Paragraph oPara1;

            oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
            oPara1.Range.Text = "Heading 1";
            oPara1.Range.Font.Bold = 1;
            oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.

I have been using the following to code to write in word file but not able to store the word file. Is there any way to store the word file using C# ?

object oMissing = System.Reflection.Missing.Value;
                object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
                //Start Word and create a new document.
                Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();

            Microsoft.Office.Interop.Word._Document oDoc = new Microsoft.Office.Interop.Word.Document();

            oWord.Visible = true;

            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            //Insert a paragraph at the beginning of the document.
            Microsoft.Office.Interop.Word.Paragraph oPara1;

            oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
            oPara1.Range.Text = "Heading 1";
            oPara1.Range.Font.Bold = 1;
            oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

守护在此方 2024-12-17 17:58:46

您应该只能使用“另存为”。

oDoc.SaveAs("MyFile.doc", ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

如果您使用 .NET 4.0,则不需要 oMissings。

S

You should just be able to use SaveAs.

oDoc.SaveAs("MyFile.doc", ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

If you are using .NET 4.0 you don't need the oMissings.

S

莫多说 2024-12-17 17:58:46

我刚刚使用 .NET 4 和 C# 创建了一个新的控制台应用程序,引用了 Microsoft Word 对象库,粘贴了您的代码并删除了所有那些 refmissing ,就像 .NET 4 一样,并且不再需要可选参数,在这里最终的代码确实很神奇:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Office.Interop.Word;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Word._Application oWord = new Application();

            oWord.Visible = true;

            var oDoc = oWord.Documents.Add();

            //Insert a paragraph at the beginning of the document.
            var paragraph1 = oDoc.Content.Paragraphs.Add();

            paragraph1.Range.Text = "Heading 1";
            paragraph1.Range.Font.Bold = 1;
            paragraph1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.

            oDoc.SaveAs2(@"C:\Temp\TestDocumentWith1Paragraph.docx");

            oWord.Quit();
        }
    }
}

I just created a new console application using .NET 4 and C#, referenced Microsoft Word Object Library, pasted your code and removed all those ref missing as with .NET 4 and optional parameters are no longer needed, here the final code which really works like a charme:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Office.Interop.Word;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Word._Application oWord = new Application();

            oWord.Visible = true;

            var oDoc = oWord.Documents.Add();

            //Insert a paragraph at the beginning of the document.
            var paragraph1 = oDoc.Content.Paragraphs.Add();

            paragraph1.Range.Text = "Heading 1";
            paragraph1.Range.Font.Bold = 1;
            paragraph1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.

            oDoc.SaveAs2(@"C:\Temp\TestDocumentWith1Paragraph.docx");

            oWord.Quit();
        }
    }
}
月牙弯弯 2024-12-17 17:58:46

试试这个:

var FileName = 'file name with path'

    oWord.ActiveDocument.SaveAs(ref FileName, 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);
    oDoc.Close(ref missing, ref missing, ref missing);

Try this:

var FileName = 'file name with path'

    oWord.ActiveDocument.SaveAs(ref FileName, 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);
    oDoc.Close(ref missing, ref missing, ref missing);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文