C#動態產生WORD文檔並進行操作

來源:互聯網
上載者:User

//記得添加應用

//添加應用的方法是:在資源管理員中右鍵應用按鈕添加新的應用,之後再.net中找到Microsoft.offoce.word.11版本

//祝您學習愉快。。。。

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Msword = Microsoft.Office.Interop.Word;//定義一個msword
namespace 建立WORD文檔並在裡面填充內容
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Msword.Application wordApp;//定義一個wordapp應用程式
        private Msword.Document wordDoc;//定義一個worddoc文檔
        private void button1_Click(object sender, EventArgs e)
        {
            object Nothing =System.Reflection.Missing.Value;//定義一個missing
            wordApp = new Msword.ApplicationClass();//對wordapp進行初始化
            wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);//對一個wordDoc進行初始化
            object filename = "F:\\aa.doc";

            if (File.Exists(filename.ToString()))//判斷檔案是否存在
            {
                File.Delete(filename.ToString());//若檔案存在就刪除這個檔案
            }
            wordApp.Visible = true;//設定動態建立的word文檔可見
            wordApp.Selection.PageSetup.LeftMargin = wordApp.CentimetersToPoints(float.Parse("2"));//設定word文檔的左邊距
            wordApp.Selection.PageSetup.RightMargin = wordApp.CentimetersToPoints(float.Parse("2"));//設定word文檔右邊距
            wordApp.ActiveWindow.HorizontalPercentScrolled = 11;//設定文檔的水平滑動距離
            wordApp.ActiveWindow.View.Type = Msword.WdViewType.wdOutlineView;//設定文檔的頁首類型
            wordApp.ActiveWindow.View.SeekView = Msword.WdSeekView.wdSeekCurrentPageHeader;//設定文檔的頁首是顯示在文檔的頭部
            wordApp.ActiveWindow.ActivePane.Selection.InsertAfter ( "這是我的頁首內容");
            wordApp.Selection.ParagraphFormat.Alignment = Msword.WdParagraphAlignment.wdAlignParagraphRight ;//設定頁首內容的對齊
            //wordApp.ActiveWindow.View.SeekView = Msword.WdSeekView.wdSeekMainDocument;//跳出頁首設定
            wordApp.Selection.ParagraphFormat.LineSpacing = 11f;//設定段落的距離為11f
            Msword.Paragraph para1;//定義一個段落為para1
            para1 = wordDoc.Paragraphs.Add(ref Nothing);//對para1進行初始化
            para1.Range.Text = "這是我的內容2這是我的內容2這是我的內容2這是我的內容2這是我的內容2這是我的內容2這是我的內容2這是我的內容2這是我的內容2";//對段落para1進行新增內容
            para1.Format.CharacterUnitFirstLineIndent = 2;//設定首行縮排
            para1.Range.Font.Color = Msword.WdColor.wdColorBlue;//設定字型的顏色
            para1.Format.SpaceAfter = 6;//在段落para1後面設定空行為6
            para1.Range.InsertParagraphAfter();//將段落para1添加到文檔中
            Msword.Paragraph para2;//同上
            object endofdoc = "\\endofdoc";//定義標籤endofdoc,"endofdoc"表示文檔的末尾書籤
            object range1 = wordDoc.Bookmarks.get_Item(ref endofdoc).Range;//定位到文檔的書籤處
            para2 = wordDoc.Paragraphs.Add(ref range1);//同上
            para2.Range.Text = "asfdasdfasdfasdffasdfsadfasdfasfdasdfas";//同上
            para2.Range.InsertParagraphAfter();//同上
            Msword.Paragraph para3;//同上
            object range2 = wordDoc.Bookmarks.get_Item(ref endofdoc).Range;//同上
            para3 = wordDoc.Paragraphs.Add(ref range2);//同上
            para3.Range.Text = "eeeeeeeeeeeeeeeeeeeeeeee";//同上
            object sttar = (object)(para3.Range.Start + 2);//"para3.Range.Start "表示para3段落的開始處加上兩個字元
            object eend = (object)(para3.Range.Start + 2 );//同上
            wordDoc.Range(ref sttar, ref eend).InsertBefore("ttt");//定位到para3段落的開始處加上兩個字元並新增內容"ttt"
            para3.Format.CharacterUnitFirstLineIndent = 2;//設定首行縮排
            para3.Range.Font.Bold=5;//設定段落para3字型顏色
            para3.Range.InsertParagraphAfter();//同上
            Msword.Table table1 = wordDoc.Tables.Add(para3.Range , 8, 8, ref Nothing, ref Nothing);//在文檔中添加8行8列的表格
            table1.Cell(1, 1).Range.Font.Color = Msword.WdColor.wdColorBrown;//設定表格中的第1行1列的字型顏色為wdColorBrown
            table1.Cell(1, 1).Range.Text = "搜點的撒旦";//在表格中的第一行第一類中新增內容
            table1.Range.ParagraphFormat.Alignment = Msword.WdParagraphAlignment.wdAlignParagraphRight;//設定表格中的內容alignment
            table1.Cell(3,3).Merge(table1.Cell(8,3));//將3行3列一直到8行3列進行組合成一列
            object row = 4;
            object col = 4;
            table1.Cell(3, 1).Split(ref row, ref col);//將3行1列分開為4行4列
            string  picturefile = "F:\\All photos\\我的照片\\aa.jpg";//設定圖片的路徑
            object linktofile=false;
            object linktodoctument=true;
            object x1=(object)(para2.Range.Start);
            object x2=(object)(para2.Range.End);
            object rr1 = wordDoc.Range(ref x1, ref x2);
            wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(picturefile, ref linktofile, ref linktodoctument, ref rr1);//將圖片添加到文檔中
            //將圖片的外形設定為可變
            Msword.Shape shape1 = wordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
            shape1.WrapFormat.Type = Msword.WdWrapType.wdWrapSquare;
            //儲存wordDoc
            wordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                ref Nothing, ref Nothing);
            //釋放記憶體
            wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
       }
   }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.