C#操作Word完全方法

來源:互聯網
上載者:User

前提:
匯入COM庫:Microsoft word 11.0 Object Library.
引用裡面就增加了
建立新Word
1 object oMissing = System.Reflection.Missing.Value;
2 Word._Application oWord;
3 Word._Document oDoc;
4 oWord = new Word.Application();
5 oWord.Visible = true;
6 oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
7    ref oMissing, ref oMissing);
開啟文檔:
1 object oMissing = System.Reflection.Missing.Value;
2 Word._Application oWord;
3 Word._Document oDoc;
4 oWord = new Word.Application();
5 oWord.Visible = true;
6 object fileName = @"E:CCCXCXXTestDoc.doc";
7 oDoc = oWord.Documents.Open(ref fileName,
8 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
9 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
10 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
匯入模板
1 object oMissing = System.Reflection.Missing.Value;
2 Word._Application oWord;
3 Word._Document oDoc;
4 oWord = new Word.Application();
5 oWord.Visible = true;
6 object fileName = @"E:XXXCCXTest.doc";
7 oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
8                ref oMissing, ref oMissing);
添加新表
1 object oMissing = System.Reflection.Missing.Value;
2 Word._Application oWord;
3 Word._Document oDoc;
4 oWord = new Word.Application();
5 oWord.Visible = true;
6 oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
7    ref oMissing, ref oMissing);
8
9 object start = 0;
10 object end = 0;
11 Word.Range tableLocation = oDoc.Range(ref start, ref end);
12 oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
表插入行
1 object oMissing = System.Reflection.Missing.Value;
2 Word._Application oWord;
3 Word._Document oDoc;
4 oWord = new Word.Application();
5 oWord.Visible = true;
6 oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
7    ref oMissing, ref oMissing);
8
9 object start = 0;
10 object end = 0;
11 Word.Range tableLocation = oDoc.Range(ref start, ref end);
12 oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
13
14 Word.Table newTable = oDoc.Tables[1];
15 object beforeRow = newTable.Rows[1];
16 newTable.Rows.Add(ref beforeRow);
儲存格合并
1 object oMissing = System.Reflection.Missing.Value;
2 Word._Application oWord;
3 Word._Document oDoc;
4 oWord = new Word.Application();
5 oWord.Visible = true;
6 oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
7    ref oMissing, ref oMissing);
8
9 object start = 0;
10 object end = 0;
11 Word.Range tableLocation = oDoc.Range(ref start, ref end);
12 oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
13
14 Word.Table newTable = oDoc.Tables[1];
15 object beforeRow = newTable.Rows[1];
16 newTable.Rows.Add(ref beforeRow);
17
18 Word.Cell cell = newTable.Cell(1, 1);
19 cell.Merge(newTable.Cell(1, 2));
儲存格分離
1 object oMissing = System.Reflection.Missing.Value;
2 Word._Application oWord;
3 Word._Document oDoc;
4 oWord = new Word.Application();
5 oWord.Visible = true;
6 oDoc = oWord.Documents.Add( oMissing,
7    ref oMissing, ref oMissing);
8
9 object start = 0;
10 object end = 0;
11 Word.Range tableLocation = oDoc.Range(ref start, ref end);
12 oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
13
14 Word.Table newTable = oDoc.Tables[1];
15 object beforeRow = newTable.Rows[1];
16 newTable.Rows.Add(ref beforeRow);
17
18 Word.Cell cell = newTable.Cell(1, 1);
19 cell.Merge(newTable.Cell(1, 2));
20
21 object Rownum = 2;
22 object Columnnum = 2;
23 cell.Split(ref Rownum, ref  Columnnum);
通過段落控制插入
1 object oMissing = System.Reflection.Missing.Value;
2 object oEndOfDoc = "endofdoc"; /**//* endofdoc is a predefined bookmark */
3
4 //Start Word and create a new document.
5 Word._Application oWord;
6 Word._Document oDoc;
7 oWord = new Word.Application();
8 oWord.Visible = true;
9 oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
10    ref oMissing, ref oMissing);
11
12 //Insert a paragraph at the beginning of the document.
13 Word.Paragraph oPara1;
14 oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
15 oPara1.Range.Text = "Heading 1";
16 oPara1.Range.Font.Bold = 1;
17 oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
18 oPara1.Range.InsertParagraphAfter();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.