ASP.NET----实现新建,编辑word内容功能

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;

namespace contractTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Word 2010");
            Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
            oWord.Visible = true;
            Microsoft.Office.Interop.Word.Document oDoc;
            object oMissing = System.Reflection.Missing.Value;
            object strFileName = "F:\\test.doc";
            //if (File.Exists((string)strFileName))
            //    File.Delete((string)strFileName);

            ////Ceate An New Word创建一个新的word文档
            //oDoc = oWord.Documents.Add(ref oMissing,ref oMissing,ref oMissing,ref oMissing);
            //oDoc.Paragraphs.Last.Range.Text = "Create An New Word!\n\r";
            //oDoc.SaveAs(ref strFileName,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);
            //oDoc.Close(ref oMissing,ref oMissing,ref oMissing);
            //oWord.Quit(ref oMissing,ref oMissing,ref oMissing );
            //Console.WriteLine("Create An New Word");
            

            //Open The Word打开word文档进行操作
            oDoc = null;
            oWord = null;
            oWord = new Microsoft.Office.Interop.Word.Application();
            oDoc = oWord.Documents.Open(ref strFileName,
                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);
            oDoc.Paragraphs.Last.Range.Text = "Open The Word!\n\r";
            Console.WriteLine("Open The New Word");

            //Insert a paragraph插入你要输入的文字内容
            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;//24pt spacing after paragraph
            oPara1.Range.InsertParagraphAfter();
            Console.WriteLine("Insert a paragraph");

            //close关闭文档,不需要机器关闭可注释后手动进行关闭
            oDoc.Save();
            oDoc.Close(ref oMissing,ref oMissing ,ref oMissing );
            oWord.Quit(ref oMissing,ref oMissing,ref oMissing);
            Console.WriteLine("Close Over");
        }
    }
}

  

posted @ 2011-09-19 20:38  brainmao  阅读(2520)  评论(0编辑  收藏  举报