? ? ?參考:http://blog.163.com/zhouchunping_99/blog/static/7837998820085114394716/
- 生成word文檔??
?
生成word文檔
view plain copy to clipboard print ?
?
- public?class?BiultReportForm ??
- ???{ ??
- ???????///?<SUMMARY></SUMMARY> ??
- ???????///?word?應用對象 ??
- ???????///? ??
- ???????private?Microsoft.Office.Interop.Word.Application?_wordApplication; ??
- ??
- ???????///?<SUMMARY></SUMMARY> ??
- ???????///?word?文件對象 ??
- ???????///? ??
- ???????private?Microsoft.Office.Interop.Word.Document?_wordDocument; ??
- ??
- ??
- ??
- ???????///?<SUMMARY></SUMMARY> ??
- ???????///?創建文檔 ??
- ???????///? ??
- ???????public?void?CreateAWord() ??
- ???????{ ??
- ???????????//實例化word應用對象 ??
- ???????????this._wordApplication?=?new?Microsoft.Office.Interop.Word.ApplicationClass(); ??
- ???????????Object?myNothing?=?System.Reflection.Missing.Value; ??
- ??
- ???????????this._wordDocument?=?this._wordApplication.Documents.Add(ref?myNothing,?ref?myNothing,?ref?myNothing,?ref?myNothing); ??
- ???????} ??
- ??
- ??
- ???????///?<SUMMARY></SUMMARY> ??
- ???????///?添加頁眉 ??
- ???????///? ??
- ???????///?<PARAM?name="pPageHeader"?/> ??
- ???????public?void?SetPageHeader(string?pPageHeader) ??
- ???????{ ??
- ???????????//添加頁眉 ??
- ???????????this._wordApplication.ActiveWindow.View.Type?=Microsoft?.Office?.Interop?.Word.WdViewType.wdOutlineView; ??
- ???????????this._wordApplication.ActiveWindow.View.SeekView?=?Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader; ??
- ???????????this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader); ??
- ???????????//設置中間對齊 ??
- ???????????this._wordApplication.Selection.ParagraphFormat.Alignment?=Microsoft?.Office?.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; ??
- ???????????//跳出頁眉設置 ??
- ???????????this._wordApplication.ActiveWindow.View.SeekView?=?Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; ??
- ???????} ??
- ??
- ??
- ??
- ???????///?<SUMMARY></SUMMARY> ??
- ???????///?插入文字 ??
- ???????///? ??
- ???????///?<PARAM?name="pText"?/>文本信息 ??
- ???????///?<PARAM?name="pFontSize"?/>字體打小 ??
- ???????///?<PARAM?name="pFontColor"?/>字體顏色 ??
- ???????///?<PARAM?name="pFontBold"?/>字體粗體 ??
- ???????///?<PARAM?name="ptextAlignment"?/>方向 ??
- ???????public?void?InsertText(string?pText,?int?pFontSize,?Microsoft.Office.Interop.Word.WdColor?pFontColor,?int?pFontBold,?Microsoft.Office.Interop.Word.WdParagraphAlignment?ptextAlignment) ??
- ???????{ ??
- ???????????//設置字體樣式以及方向 ??
- ???????????this._wordApplication.Application.Selection.Font.Size?=?pFontSize; ??
- ???????????this._wordApplication.Application.Selection.Font.Bold?=?pFontBold; ??
- ???????????this._wordApplication.Application.Selection.Font.Color=?pFontColor; ??
- ???????????this._wordApplication.Application.Selection.ParagraphFormat.Alignment?=?ptextAlignment; ??
- ???????????this._wordApplication.Application.Selection.TypeText(pText); ??
- ???????} ??
- ??
- ??
- ???????///?<SUMMARY></SUMMARY> ??
- ???????///?換行 ??
- ???????///? ??
- ???????public?void?NewLine() ??
- ???????{ ??
- ???????????//換行 ??
- ???????????this._wordApplication.Application.Selection.TypeParagraph(); ??
- ???????} ??
- ??
- ??
- ??
- ???????///?<SUMMARY></SUMMARY> ??
- ???????///?插入一個圖片 ??
- ???????///? ??
- ???????///?<PARAM?name="pPictureFileName"?/> ??
- ???????public?void?InsertPicture(string?pPictureFileName) ??
- ???????{ ??
- ???????????object?myNothing?=?System.Reflection.Missing.Value; ??
- ???????????//圖片居中顯示 ??
- ???????????this._wordApplication.Selection.ParagraphFormat.Alignment?=?Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; ??
- ???????????this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName,?ref?myNothing,?ref?myNothing,?ref?myNothing); ??
- ???????} ??
- ??
- ??
- ??
- ???????///?<SUMMARY></SUMMARY> ??
- ???????///?保存文件? ??
- ???????///? ??
- ???????///?<PARAM?name="pFileName"?/>保存的文件名 ??
- ???????public?void?SaveWord(string?pFileName) ??
- ???????{ ??
- ???????????object?myNothing?=?System.Reflection.Missing.Value; ??
- ???????????object?myFileName?=?pFileName; ??
- ???????????object?myWordFormatDocument?=Microsoft?.Office?.Interop?.Word.WdSaveFormat.wdFormatDocument; ??
- ???????????object?myLockd?=?false; ??
- ???????????object?myPassword?=?""; ??
- ???????????object?myAddto?=?true; ??
- ??
- ???????????try??
- ???????????{ ??
- ???????????????this._wordDocument.SaveAs(ref?myFileName,?ref?myWordFormatDocument,?ref?myLockd,?ref?myPassword,?ref?myAddto,?ref?myPassword, ??
- ???????????????????ref?myLockd,?ref?myLockd,?ref?myLockd,?ref?myLockd,?ref?myNothing,?ref?myNothing,?ref?myNothing,? ??
- ???????????????????ref?myNothing,?ref?myNothing,?ref?myNothing); ??
- ???????????} ??
- ???????????catch??
- ???????????{ ??
- ???????????????throw?new?Exception("導出word文檔失敗!"); ??
- ???????????} ??
- ???????} ??
- ???} ?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
