C# WORD操作實現代碼_C#教程

來源:互聯網
上載者:User
1.先通過程式產生報表樣式的HTML頁面,然後修改HTML頁面的尾碼名為DOC。
2.定製WORD文檔的模板檔案,在C#中操作WORD模板,產生新的WORD文檔。
第一方案簡單,只需要改動檔案的副檔名就行了,但是也存在了一些問題,譬如產生的WORD文檔樣式的丟失。這樣對於客戶來說可能是一個無法通過的方案。第二方案比較複雜,需要調用OFFICE的WORD組件通過C#來操作WORD,進而產生WORD。此方法類似於我們在c#中的後台拼接資料。雖然麻煩,但是能夠靈活定製,只不過是操作WORD對象而已。
經過再三考慮:決定用第二種方法來產生WORD報告文檔。
通過自己的實踐,這個需求總算是搞定了,在實際開發的過程中,遇到了這樣那樣的問題,還好,通過不斷的尋找網路資源,結合實際開發中的情況,問題都得到瞭解決。現將本人在開發過程中的一些理解與經驗總結一下:
在VS2008平台下,引用.net-Microsoft.Office.Interop.Word.12,這樣就可以在程式用操作WORD對象了。
通過簡單執行,報了80070005錯誤,這個錯誤是因為許可權不夠,需要在DCOM配置中更改.net和IIS使用者的操作許可權,具體修改過程如下: 解決方案一:
1.控制台-》管理工具-》元件服務-》電腦-》我的電腦-》DCom配置-》找到Microsoft Word文檔之後,單擊屬性開啟此應 用程式的屬性對話方塊。
2.單擊標識選項卡,然後選擇互動式使用者。
3.單擊"安全"選項卡,分別在"啟動和啟用許可權"和"存取權限"組中選中"自訂",然後自訂->編輯->添加ASP.NET賬戶和IUSER_電腦 名。
4. 確保允許每個使用者訪問,然後單擊確定。
5. 單擊確定關閉 DCOMCNFG。
如果上述方法不能解決問題,就應該是許可權問題,請嘗試用下面的方法:
在web.config中使用身份類比,在<system.web>節中加入 <identity impersonate="true" userName="你的使用者名稱 " password="密碼 "/>
</system.web>
解決了上述問題,開始考慮如何建立WORD模板檔案,WORD的模板檔案其實就是通過書籤來新增內容的。也就是通過在WORD文檔中建立書籤,然後在程式中擷取模板檔案的所有書籤,通過給書籤賦值來進行文檔產生的。
在程式中的操作流程如下:
聲明WORD程式的對象 → 聲明一個WORD文檔對象 → 擷取當前的操作文檔對象 → 擷取文檔所有的書籤 → 將資料庫資料賦值到對應的書籤 → 將文檔另存新檔指定的檔案夾下.
下面將針對農業植物測試報告來分析具體的代碼實現:
複製代碼 代碼如下:

//產生WORD程式對象和WORD文檔對象
Microsoft.Office.Interop.Word.Application appWord = new Application();
Microsoft.Office.Interop.Word.Document doc = new Document();
object oMissing = System.Reflection.Missing.Value;//這個是什麼東西,我始終沒搞明白-_-
//開啟模板文檔,並指定doc的文件類型
object objTemplate = Server.MapPath(p_TemplatePath);
object objDocType = WdDocumentType.wdTypeDocument;
doc = (Document)appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
//擷取模板中所有的書籤
Bookmarks odf = doc.Bookmarks;
string[] testTableremarks = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker"};
string[] testTablevalues = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker",};
//迴圈所有的書籤,並給書籤賦值
for (int oIndex = 0; oIndex < testTableremarks.Length; oIndex++)
{
obDD_Name = WD + testTableremarks[oIndex];
doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = p_TestReportTable.Rows[0][testTablevalues [oIndex]].ToString();//此處Range也是WORD中很重要的一個對象,就是當前巨集指令引數所在的地區
}
//第四步 產生word,將當前的文檔對象另存新檔指定的路徑,然後關閉doc對象。關閉應用程式
object filename = Server.MapPath(p_SavePath) + "\\Testing_" + DateTime.Now.ToShortDateString() + ".doc";
object miss = System.Reflection.Missing.Value;
doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
object missingValue = Type.Missing;
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
this.Hid_ShowMessage.Value = "產生成功!";

上述代碼就是一個通過模板檔案產生WORD的過程。其實也就是一個替換書籤內容的過程。
在開發的過程中,有些資料是動態增加的,假如我要向一個表格中動態添加幾行資料,就無法用替換書籤的方式來進行操作,需要通過程式在文檔頁面的表格中添加行。
向表格中添加行,有兩種操作形式:一種是在WORD模板中已經存在了一個表格。一種是我們在程式中直接添加一個表格對象。
第一種情況下,需要注意:在WORD模板中要操作的表格中,不能有縱向合并的儲存格,不然程式無法擷取到當前要操作對象導致程式報錯.儲存格的合并,我們可以在程式中控制。
第二種情況下就需要我們通過程式去直接添加表格了。
產生表格的代碼具體如下:
1.擷取文檔中已存在的表格:
Microsoft.Office.Interop.Word.Table characterTable = doc.Tables[2];//在document對象的集合操作中,起始點是從1開始,並不是從0開始的,此處需要注意。
2.在文檔中直接產生表格,首先要擷取插入表格的位置,然後添加表格對象:
object oEndOfDoc = "\\endofdoc";//WORD中預定義的書籤,還有很多,此處就不一一列舉。
object oMissing = System.Reflection.Missing.Value;
Range wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;//擷取當前文檔的末尾位置。
wrdRng.InsertAfter(" ");//插入一行,此處不能用 wrdRng.InsertAfter(""),如果用這個,就不能換行,我也不知道為什麼。
複製代碼 代碼如下:

object oCollapseEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;//分頁符
wrdRng.Collapse(ref oCollapseEnd);
wrdRng.InsertBreak(ref oPageBreak);//插入了一頁
wrdRng.Collapse(ref oCollapseEnd);
wrdRng.InsertAfter("圖片資訊");
wrdRng.Font.Size = 20;//指定操作對象的文字大小
wrdRng.Font.Bold = 1;//指定操作對象的粗體:1為粗體,0為正常
wrdRng.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//指定操作地區的文字布局:置中對齊
//上述代碼的意思是:找到當前的末尾位置,然後插入一個分頁符,相當於跳到了一個新頁,在這個新頁的頂端寫入文字“圖片資訊”,並指定文字大小為20,粗體置中顯示。
wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
wrdRng.InsertAfter(" ");
wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
wrdRng.InsertParagraphAfter();//插入一個段落,在此段落上插入一個2行一列的表格。
Microsoft.Office.Interop.Word.Table newTable = doc.Tables.Add(wrdRng, 2, 1, ref oMissing, ref oMissing);

我們還可以對錶格進行格式設定,此處我們就不在一一列舉。
3.下面我們分析一下對錶格的儲存格的操作:合并,拆分。這個就需要我們根據實際表格來進行操作:
//擷取具體的某個儲存格(1,1),擷取第一行第一列的儲存格
Cell cell = doc.Tables[1].Cell(1,1);
cell.Range.Text="Text";//指定目前的儲存格的內容為Text
在Table的操作中,添加新行:
object beforeRow = doc.Tables[1].Rows[2];//此行是先擷取到第二行
doc.Tables[1].Rows.Add(ref beforeRow);//效果類似於在WORD中此表格的第二行上進行【插入行】操作,插入的新行將會插入到當前行的上一行中,格式也是和此行一致的。
//合併儲存格:感覺在此處合併儲存格挺傻瓜的,你只需要指定你要合并的起始儲存格和結束儲存格,然後通過Merge操作就行了
Cell cell = doc.Tables[1].Cell(iRow, 2);//列合并
cell.Merge(doc.Tables[1].Cell(iRow, 6));
Cell cell1 = doc.Tables[1].Cell(iRow - 1, 1);//行合并
cell1.Merge(doc.Tables[1].Cell(iRow + 1, 1));
上述操作就是在此程式中用到的一些知識點,還有好多的東西需要去熟悉、理解。
另外,在程式的測試過程中發現,當執行一次文檔產生後,在資源管理員中始終有winword.exe進程殺不掉,目前的解決辦法是:直接殺進程,代碼如下:
複製代碼 代碼如下:

protected void killAllProcess() // 殺掉所有winword.exe進程
{
System.Diagnostics.Process[] myPs;
myPs = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process p in myPs)
{
if (p.Id != 0)
{
string myS = "WINWORD.EXE" + p.ProcessName + " ID:" + p.Id.ToString();
try
{
if (p.Modules != null)
if (p.Modules.Count > 0)
{
System.Diagnostics.ProcessModule pm = p.Modules[0];
myS += "\n Modules[0].FileName:" + pm.FileName;
myS += "\n Modules[0].ModuleName:" + pm.ModuleName;
myS += "\n Modules[0].FileVersionInfo:\n" + pm.FileVersionInfo.ToString();
if (pm.ModuleName.ToLower() == "winword.exe")
p.Kill();
}
}
catch
{ }
finally
{

}
}
}
}

目前為止,一個WORD文檔就產生了。上述為我在這個程式開發中遇到的問題和解決方案,可能有好多地方都是考慮不全的,如果在程式開發中對WORD的操作有新的認識的話,歡迎和我溝通交流,彼此提高!
下邊是在網上一些比較好的摘抄:
建立新Word
複製代碼 代碼如下:

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

開啟文檔:
複製代碼 代碼如下:

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:CCCXCXXTestDoc.doc";
oDoc = oWord.Documents.Open(ref fileName,
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);

匯入模板
複製代碼 代碼如下:

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:XXXCCXTest.doc";
oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
ref oMissing, ref oMissing);

.添加新表
複製代碼 代碼如下:

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

.表插入行
複製代碼 代碼如下:

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);

.儲存格合并
複製代碼 代碼如下:

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));

.儲存格分離
複製代碼 代碼如下:

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add( oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
object Rownum = 2;
object Columnnum = 2;
cell.Split(ref Rownum, ref Columnnum);

通過段落控制插入
複製代碼 代碼如下:

object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\endofdoc"; /**//* endofdoc is a predefined bookmark */
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Heading 1";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
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.