How to save doc file using C#

六眼飞鱼酱① 提交于 2019-12-29 08:15:05

问题


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.

回答1:


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




回答2:


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();
        }
    }
}



回答3:


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);


来源:https://stackoverflow.com/questions/7811481/how-to-save-doc-file-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!