需求:
需要把數據做成這樣的效果、一個頁面展示一個r單子數據。
object filename = "";
Object Nothing = System.Reflection.Missing.Value;
Word.Application WordApp = new Word.ApplicationClass();//實例化word
HY.Common.Utils.Logger.Log.Info("WordApp:" + WordApp);
Word._Application oWord = new Word.Application();
//創建Word文檔
Word._Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
try
{
SetPage(WordApp, WordDoc, "橫板", 21, 29.7, 1, 1, 1, 1);
HY.Common.Utils.Logger.Log.Info("WordDoc:" + WordDoc);
string name = "審批表" + DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.ToLongTimeString().Replace(":", "") + ".docx";//創建生成文件名字
HY.Common.Utils.Logger.Log.Info("name" + name);
string CreateFilePath = ConfigurationManager.AppSettings["CreateFilePath"];
HY.Common.Utils.Logger.Log.Info("CreateFilePath" + CreateFilePath);
filename = CreateFilePath + name; //文件保存路徑
HY.Common.Utils.Logger.Log.Info("filename" + filename);
string[] SerialNumberArr = Context.Request["SerialNumber"].Substring(0, Context.Request["SerialNumber"].ToString().Length - 1).Split(',');
string[] ProcInstIDArr = Context.Request["ProcInstID"].Substring(0, Context.Request["ProcInstID"].ToString().Length - 1).Split(',');//掛賬Id
string[] PaymentProcInstIDArr = Context.Request["PaymentProcInstID"].Substring(0, Context.Request["PaymentProcInstID"].ToString().Length - 1).Split(',');//付款Id
for (int i = 0; i < ProcInstIDArr.Length; i++)
{
#region 打印
InsertText(WordDoc, " 審批表 ", new System.Drawing.Font("微軟雅黑", 12, System.Drawing.FontStyle.Bold), Word.WdParagraphAlignment.wdAlignParagraphLeft, false);
InsertText(WordDoc, "", new System.Drawing.Font("微軟雅黑", 12, System.Drawing.FontStyle.Bold), Word.WdParagraphAlignment.wdAlignParagraphLeft, true);
InsertText(WordDoc, "申請人信息", new System.Drawing.Font("宋體", 12, System.Drawing.FontStyle.Bold), Word.WdParagraphAlignment.wdAlignParagraphLeft, false);
if (ProcInstIDArr.Length - 1 != i)
{
InsertBreak(WordDoc, Nothing);
}
#endregion
}
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);
WordApp = null;
HY.Common.Utils.Logger.Log.Info("SaveAs" + filename);
}
catch (Exception ex)
{
HY.Common.Utils.Logger.Log.Info("error" + e.ToString());
}
finally
{
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
oWord.Quit(ref Nothing, ref Nothing, ref Nothing);
KillWordProcess();
}




遇到的問題:
1.由於需要每一個頁面顯示一個單子、所以每次根據ProcInstIDArr[] 循環創建完以後、調用InsertBreak()生成下一個空白頁面、導致最後會出現一個空白頁面、
當時想到的是怎麼刪除最後一個空白頁、通過在網上查詢,說通過以下代碼可以刪除、最後拿下來試驗發現無法實現。

中午跟同事請教:理理思路。發現完全是我想復雜了、只想著怎麼刪除,沒有想過不去生成。最後通過一個if判斷輕松解決問題。
遇到問題不要想的太復雜。
2.還有一個關於winword.exe進程未殺死的問題、 這個是今天早上發現的,早上打開服務器,發現服務器上出現N個word文檔。進程裡還有不下30個winword.exe進 程、詳情點擊