在當(dāng)前項(xiàng)目開(kāi)發(fā)過(guò)程中,客戶有根據(jù)數(shù)據(jù)庫(kù)數(shù)據(jù)生成WORD文檔的需求,在和同事溝通的過(guò)程中,找到了兩個(gè)解決方案
1.先通過(guò)程序生成報(bào)表樣式的HTML頁(yè)面,然后修改HTML頁(yè)面的后綴名為DOC。
2.定制WORD文檔的模板文件,在C#中操作WORD模板,生成新的WORD文檔。
第一方案簡(jiǎn)單,只需要改動(dòng)文件的擴(kuò)展名就行了,但是也存在了一些問(wèn)題,譬如生成的WORD文檔樣式的丟失。這樣對(duì)于客戶來(lái)說(shuō)可能是一個(gè)無(wú)法通過(guò)的方案。第二方案比較復(fù)雜,需要調(diào)用OFFICE的WORD組件通過(guò)C#來(lái)操作WORD,進(jìn)而生成WORD。此方法類似于我們?cè)赾#中的后臺(tái)拼接數(shù)據(jù)。雖然麻煩,但是能夠靈活定制,只不過(guò)是操作WORD對(duì)象而已。
經(jīng)過(guò)再三考慮:決定用第二種方法來(lái)生成WORD報(bào)告文檔。
通過(guò)自己的實(shí)踐,這個(gè)需求總算是搞定了,在實(shí)際開(kāi)發(fā)的過(guò)程中,遇到了這樣那樣的問(wèn)題,還好,通過(guò)不斷的查找網(wǎng)絡(luò)資源,結(jié)合實(shí)際開(kāi)發(fā)中的情況,問(wèn)題都得到了解決。現(xiàn)將本人在開(kāi)發(fā)過(guò)程中的一些理解與經(jīng)驗(yàn)總結(jié)一下:
在VS2008平臺(tái)下,引用.net-Microsoft.Office.Interop.Word.12,這樣就可以在程序用操作WORD對(duì)象了。
通過(guò)簡(jiǎn)單執(zhí)行,報(bào)了80070005錯(cuò)誤,這個(gè)錯(cuò)誤是因?yàn)闄?quán)限不夠,需要在DCOM配置中更改.net和IIS用戶的操作權(quán)限,具體修改過(guò)程如下: 解決方法一:
1.控制面板-》管理工具-》組件服務(wù)-》計(jì)算機(jī)-》我的電腦-》DCom配置-》找到Microsoft Word文檔之后,單擊屬性打開(kāi)此應(yīng) 用程序的屬性對(duì)話框。
2.單擊標(biāo)識(shí)選項(xiàng)卡,然后選擇交互式用戶。
3.單擊"安全"選項(xiàng)卡,分別在"啟動(dòng)和激活權(quán)限"和"訪問(wèn)權(quán)限"組中選中"自定義",然后自定義->編輯->添加ASP.NET賬戶和IUSER_計(jì)算機(jī) 名。
4. 確保允許每個(gè)用戶訪問(wèn),然后單擊確定。
5. 單擊確定關(guān)閉 DCOMCNFG。
如果上述方法不能解決問(wèn)題,就應(yīng)該是權(quán)限問(wèn)題,請(qǐng)嘗試用下面的方法:
在web.config中使用身份模擬,在<system.web>節(jié)中加入 <identity impersonate="true" userName="你的用戶名 " password="密碼 "/>
</system.web>
解決了上述問(wèn)題,開(kāi)始考慮如何創(chuàng)建WORD模板文件,WORD的模板文件其實(shí)就是通過(guò)書(shū)簽來(lái)添加內(nèi)容的。也就是通過(guò)在WORD文檔中創(chuàng)建書(shū)簽,然后在程序中獲取模板文件的所有書(shū)簽,通過(guò)給書(shū)簽賦值來(lái)進(jìn)行文檔生成的。
在程序中的操作流程如下:
聲明WORD程序的對(duì)象 → 聲明一個(gè)WORD文檔對(duì)象 → 獲取當(dāng)前的操作文檔對(duì)象 → 獲取文檔所有的書(shū)簽 → 將數(shù)據(jù)庫(kù)數(shù)據(jù)賦值到對(duì)應(yīng)的書(shū)簽 → 將文檔另存為指定的文件夾下.
下面將針對(duì)農(nóng)業(yè)植物測(cè)試報(bào)告來(lái)分析具體的代碼實(shí)現(xiàn):
復(fù)制代碼代碼如下:
//生成WORD程序?qū)ο蠛蚖ORD文檔對(duì)象
Microsoft.Office.Interop.Word.Application appWord = new Application();
Microsoft.Office.Interop.Word.Document doc = new Document();
object oMissing = System.Reflection.Missing.Value;//這個(gè)是什么東西,我始終沒(méi)搞明白-_-
//打開(kāi)模板文檔,并指定doc的文檔類型
object objTemplate = Server.MapPath(p_TemplatePath);
object objDocType = WdDocumentType.wdTypeDocument;
doc = (Document)appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
//獲取模板中所有的書(shū)簽
Bookmarks odf = doc.Bookmarks;
string[] testTableremarks = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker"};
string[] testTablevalues = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker",};
//循環(huán)所有的書(shū)簽,并給書(shū)簽賦值
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中很重要的一個(gè)對(duì)象,就是當(dāng)前操作參數(shù)所在的區(qū)域
}
//第四步 生成word,將當(dāng)前的文檔對(duì)象另存為指定的路徑,然后關(guān)閉doc對(duì)象。關(guān)閉應(yīng)用程序
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 = "生成成功!";
上述代碼就是一個(gè)通過(guò)模板文件生成WORD的過(guò)程。其實(shí)也就是一個(gè)替換書(shū)簽內(nèi)容的過(guò)程。
在開(kāi)發(fā)的過(guò)程中,有些數(shù)據(jù)是動(dòng)態(tài)增加的,假如我要向一個(gè)表格中動(dòng)態(tài)的添加幾行數(shù)據(jù),就無(wú)法用替換書(shū)簽的方式來(lái)進(jìn)行操作,需要通過(guò)程序在文檔頁(yè)面的表格中添加行。
向表格中添加行,有兩種操作形式:一種是在WORD模板中已經(jīng)存在了一個(gè)表格。一種是我們?cè)诔绦蛑兄苯犹砑右粋€(gè)表格對(duì)象。
第一種情況下,需要注意:在WORD模板中要操作的表格中,不能有縱向合并的單元格,不然程序無(wú)法獲取到當(dāng)前要操作對(duì)象導(dǎo)致程序報(bào)錯(cuò).單元格的合并,我們可以在程序中控制。
第二種情況下就需要我們通過(guò)程序去直接添加表格了。
生成表格的代碼具體如下:
1.獲取文檔中已存在的表格:
Microsoft.Office.Interop.Word.Table characterTable = doc.Tables[2];//在document對(duì)象的集合操作中,起始點(diǎn)是從1開(kāi)始,并不是從0開(kāi)始的,此處需要注意。
2.在文檔中直接生成表格,首先要獲取插入表格的位置,然后添加表格對(duì)象:
object oEndOfDoc = "//endofdoc";//WORD中預(yù)定義的書(shū)簽,還有很多,此處就不一一列舉。
object oMissing = System.Reflection.Missing.Value;
Range wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;//獲取當(dāng)前文檔的末尾位置。
wrdRng.InsertAfter(" ");//插入一行,此處不能用 wrdRng.InsertAfter(""),如果用這個(gè),就不能換行,我也不知道為什么。
復(fù)制代碼代碼如下:
object oCollapseEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;//分頁(yè)符
wrdRng.Collapse(ref oCollapseEnd);
wrdRng.InsertBreak(ref oPageBreak);//插入了一頁(yè)
wrdRng.Collapse(ref oCollapseEnd);
wrdRng.InsertAfter("圖片信息");
wrdRng.Font.Size = 20;//指定操作對(duì)象的文字大小
wrdRng.Font.Bold = 1;//指定操作對(duì)象的粗體:1為粗體,0為正常
wrdRng.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//指定操作區(qū)域的文字布局:居中對(duì)齊
//上述代碼的意思是:找到當(dāng)前的末尾位置,然后插入一個(gè)分頁(yè)符,相當(dāng)于跳到了一個(gè)新頁(yè),在這個(gè)新頁(yè)的頂端寫(xiě)入文字“圖片信息”,并指定文字大小為20,粗體居中顯示。
wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
wrdRng.InsertAfter(" ");
wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
wrdRng.InsertParagraphAfter();//插入一個(gè)段落,在此段落上插入一個(gè)2行一列的表格。
Microsoft.Office.Interop.Word.Table newTable = doc.Tables.Add(wrdRng, 2, 1, ref oMissing, ref oMissing);
我們還可以對(duì)表格進(jìn)行格式設(shè)置,此處我們就不在一一列舉。
3.下面我們分析一下對(duì)表格的單元格的操作:合并,拆分。這個(gè)就需要我們根據(jù)實(shí)際表格來(lái)進(jìn)行操作:
//獲取具體的某個(gè)單元格(1,1),獲取第一行第一列的單元格
Cell cell = doc.Tables[1].Cell(1,1);
cell.Range.Text="Text";//指定當(dāng)前單元格的內(nèi)容為T(mén)ext
在Table的操作中,添加新行:
object beforeRow = doc.Tables[1].Rows[2];//此行是先獲取到第二行
doc.Tables[1].Rows.Add(ref beforeRow);//效果類似于在WORD中此表格的第二行上進(jìn)行【插入行】操作,插入的新行將會(huì)插入到當(dāng)前行的上一行中,格式也是和此行一致的。
//合并單元格:感覺(jué)在此處合并單元格挺傻瓜的,你只需要指定你要合并的起始單元格和結(jié)束單元格,然后通過(guò)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));
上述操作就是在此程序中用到的一些知識(shí)點(diǎn),還有好多的東西需要去熟悉、理解。
另外,在程序的測(cè)試過(guò)程中發(fā)現(xiàn),當(dāng)執(zhí)行一次文檔生成后,在資源管理器中始終有winword.exe進(jìn)程殺不掉,目前的解決辦法是:直接殺進(jìn)程,代碼如下:
復(fù)制代碼代碼如下:
protected void killAllProcess() // 殺掉所有winword.exe進(jìn)程
{
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
{
}
}
}
}
目前為止,一個(gè)WORD文檔就生成了。上述為我在這個(gè)程序開(kāi)發(fā)中遇到的問(wèn)題和解決方法,可能有好多地方都是考慮不全的,如果在程序開(kāi)發(fā)中對(duì)WORD的操作有新的認(rèn)識(shí)的話,歡迎和我溝通交流,彼此提高!
下邊是在網(wǎng)上一些比較好的摘抄:
創(chuàng)建新Word
復(fù)制代碼代碼如下:
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);
打開(kāi)文檔:
復(fù)制代碼代碼如下:
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);
導(dǎo)入模板
復(fù)制代碼代碼如下:
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);
.添加新表 復(fù)制代碼代碼如下:
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);
.表插入行
復(fù)制代碼代碼如下:
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);
.單元格合并
復(fù)制代碼代碼如下:
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));
.單元格分離
復(fù)制代碼代碼如下:
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);
通過(guò)段落控制插入
復(fù)制代碼代碼如下:
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();