C 操作word文件(三)

2021-05-24 23:48:12 字數 4706 閱讀 9015

1. c#呼叫word列印信封

d.printout(ref missing, ref missing, ref range, 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);

d.close(ref readonly, ref missing, ref missing);

file.delete(temppath);

return true;

} 2.c#實現word中**資訊讀取

很多時候,會有很多資訊存放在word文件中。而我們需要把這些資訊提取出來,另做它用。而word的格式是ms的機密,不知道有沒有nb人可以對其做字元流的分析,反正我是沒這能力也沒這打算。所以就只能用ms提供的元件來進行程式設計。但ms沒有提供託管的類庫,而是提供了對com元件的pia轉換。具體新增,使用和相關知識,可以參見kaneboy's blog中的高手的講解,很是清晰。

而我想做的是對word文件中的表資訊進行提取。網上很難找到相關的**(開啟乙個已有文件,對其內容進行分析),但我覺得這種工作是很有意義的。寫了一段小的demo,如下:

object ofilename = @"c:/documents and settings/liush/my documents/testdoc.doc";

object oreadonly = true;

object omissing = system.reflection.missing.value;

for (int tablepos = 1; tablepos <= odoc.tables.count; tablepos++)

/個表:/n", tablepos, odoc.tables.count);

for (int rowpos = 1; rowpos <= nowtable.rows.count; rowpos++)

tablemessage += "/n";

}messagebox.show(tablemessage);

}如果看過了上面kaneboy的文章(這是乙個系列的之一),再看這段**應該不會很難理解。開啟乙個已有文件,然後遍歷其中的所有的表。這裡只是簡單的將資訊顯示出來,具體實踐上可以對這些資訊進行分析。做完這些後,終於找到了一些官方的支援文件,位址如下:

其中的word任務有對word各種操作的簡單**事例,用vb和c#寫的。看完之後,我想每個人都會明白vb對com的支援比c#不是簡單明瞭一點兩點。(可以看下這個同樣的**,用vb實現開啟word文件的操作,**如下:

dim filename as string = "c:/documents and settings/liush/my documents/testdoc.doc"

dim isreadonly as boolean = true

所以,下次我要做com操作的時候,我還會回歸我可愛的vb的。但是,用了太久的c#毛病越來越多了,動不動就習慣性加括號,加分號。。。

3.c#讀取word中的**

其實,microsoft提供了很多類和藉口供程式設計師們使用。最近乙個專案中,需要對word中的**進行操作:因為老闆要求將內部控管系統做成web版的,以便更好地跟台灣溝通,但是網路很慢,所以在網頁上輸入一些資料就很討厭,so,需要制訂乙個規格的文檔案,比如word,填寫好文件後上傳到ftp上,我們再用一支程式掃瞄這些文檔案,自動新增到資料庫中。。。一開始的時候,覺得好沒方向啊,到網上搜了一下,發現原來有很多前輩早在n年前就已經開始這方面的工作了。

private void button1_click(object sender, system.eventargs e)

otable.rows[1].range.font.bold = 1;

otable.rows[1].range.font.italic = 1;

//接著新增一些文字

word.paragraph opara4;

orng = odoc.bookmarks.get_item(ref oendofdoc).range;

opara4 = odoc.content.paragraphs.add(ref orng);

opara4.range.insertparagraphbefore();

opara4.range.text = "and here's another table:";

opara4.format.spaceafter = 24;

opara4.range.insertparagraphafter();

//新增乙個5行2列的表,填充資料並且改變列寬

wrdrng = odoc.bookmarks.get_item(ref oendofdoc).range;

otable = odoc.tables.add(wrdrng, 5, 2, ref omissing, ref omissing);

otable.range.paragraphformat.spaceafter = 6;

for(r = 1; r <= 5; r++)

for(c = 1; c <= 2; c++)

otable.columns[1].width = oword.inchestopoints(2); //change width of columns 1 & 2

otable.columns[2].width = oword.inchestopoints(3);

//keep inserting text. when you get to 7 inches from top of the

//document, insert a hard page break.

object opos;

double dpos = oword.inchestopoints(7);

odoc.bookmarks.get_item(ref oendofdoc).range.insertparagraphafter();

dowhile(dpos >= convert.todouble(opos));

object ocollapseend = word.wdcollapsedirection.wdcollapseend;

object opagebreak = word.wdbreaktype.wdpagebreak;

wrdrng.collapse(ref ocollapseend);

wrdrng.insertbreak(ref opagebreak);

wrdrng.collapse(ref ocollapseend);

wrdrng.insertafter("we're now on page 2. here's my chart:");

wrdrng.insertparagraphafter();

//新增乙個chart

word.inlineshape oshape;

object oclasstype = "msgraph.chart.8";

wrdrng = odoc.bookmarks.get_item(ref oendofdoc).range;

oshape = wrdrng.inlineshapes.addoleobject(ref oclasstype, ref omissing,

ref omissing, ref omissing, ref omissing,

ref omissing, ref omissing, ref omissing);

//change the chart type to line.

object parameters = new object[1];

parameters[0] = 4; //xlline = 4

ochart.gettype().invokemember("charttype", bindingflags.setproperty,

null, ochart, parameters);

//set the width of the chart.

oshape.width = oword.inchestopoints(6.25f);

oshape.height = oword.inchestopoints(3.57f);

//add text after the chart.

wrdrng = odoc.bookmarks.get_item(ref oendofdoc).range;

wrdrng.insertparagraphafter();

wrdrng.insertafter("the end.");

//close this form.

this.close();

}使用模板

如果您要使用自動化功能建立的文件都是通用格式,則利用基於預設格式的模板的新文件來開始建立過程會更加容易。與從頭建立文件相比,將某個模板與 word 自動化客戶端配合使用有兩大優點: • 您可以對整個文件中的物件的格式設定和布局施加更多控制。

• 可以使用較少的**建立文件。

通過使用模板,可以精確地調整**、段落和其他物件在文件中的布局,並可為這些物件新增格式設定。通過使用自動化功能,可以基於包含下面這樣的**的模板建立新文件: 在模板中,可以定義書籤,這樣,自動化客戶端就可以在文件的特定位置加入可變文字,如下所示: 使用模板的另乙個優點在於,您可以建立和儲存希望在執行時應用的格式樣式,如下所示: - 或者 -

c 操作word文件

最近由於工作需要,做了一些關於c 操作word文件方面的工作 主要是對word中 的操作,以下是部分 關於操作不規則 的 using system using system.collections using system.componentmodel using system.data using...

C 操作word文件

這裡給大家介紹下如何運用vba來操作word文件內容,開啟關閉word文件。一 什麼是vba 二 c 開啟word操作 定義word物件 word.document d new word.document 找到檔案 object filename 1.docx object filefullname...

C 中操作Word文件

今天,在用c 語言開發乙個word報表,在網上看到一些有用的文章,先 過來,自己屯著。以下來自 參考 http xb.hzu.edu.cn qikan manage wenzhang 200906014.pdf 在當前專案開發過程中,客戶有根據資料庫資料生成word文件的需求,在和 同事溝通的過程中...