How to save doc file using C#

前端 未结 3 1340
别那么骄傲
别那么骄傲 2020-12-11 11:16

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          


        
相关标签:
3条回答
  • 2020-12-11 11:38

    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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-11 11:40

    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);
    
    0 讨论(0)
  • 2020-12-11 11:50

    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

    0 讨论(0)
提交回复
热议问题