【前置處理】
● 「加入參考」→「COM」→「Microsoft Word Object Library」
● 加入以下程式碼:
Microsoft.Office.Interop.Word.Application wrdApp;
Object oMissing = System.Reflection.Missing.Value;
Object oEndOfDoc = "\\endofdoc";
wrdApp = new Microsoft.Office.Interop.Word.Application ( );
wrdApp.Visible = false; //執行過程不在畫面上開啟 Word
【新增一個 WORD 文件然後存檔】
//建立一個空白文件
Microsoft.Office.Interop.Word._Document myDoc;
myDoc = wrdApp.Documents.Add ( ref oMissing, ref oMissing, ref oMissing, ref oMissing );
/* 相關處理 */
//另存文件
Object oSavePath = "D:\\test.doc"; //存檔路徑
Object oFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument; //格式
myDoc.SaveAs ( ref oSavePath, ref oFormat,
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 oFalse = false;
myDoc.Close ( ref oFalse, ref oMissing, ref oMissing );
【開啟既有 WORD 文件然後存檔】
//開啟一個既有的 word 檔案
Object oFilePath = "D:\\test.doc"; //檔案路徑
Microsoft.Office.Interop.Word._Document myDoc;
myDoc = wrdApp.Documents.Open ( ref oFilePath, 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
);
/* 相關處理 */
//存檔並關閉檔案
myDoc.Save ( );
Object oFalse = false;
myDoc.Close ( ref oFalse, ref oMissing, ref oMissing );
【版面設定】
myDoc.PageSetup.TopMargin = wrdApp.CentimetersToPoints( float.Parse("2") );
myDoc.PageSetup.BottomMargin = wrdApp.CentimetersToPoints( float.Parse("2") );
myDoc.PageSetup.LeftMargin = wrdApp.CentimetersToPoints( float.Parse("2") );
myDoc.PageSetup.RightMargin = wrdApp.CentimetersToPoints( float.Parse("2") );
【加入一段文字】
Microsoft.Office.Interop.Word.Paragraph oPara;
object oRng = myDoc.Bookmarks.get_Item ( ref oEndOfDoc ).Range;
oPara = myDoc.Content.Paragraphs.Add ( ref oRng );
oPara.Range.Text = "我的文字內容";
oPara.Range.Font.Bold = 1;
oPara.Format.SpaceAfter = 6;
oPara.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
oPara.Range.InsertParagraphAfter ( );
【建立表格】
注意表格的索引是由 1 開始算而非 0。
Microsoft.Office.Interop.Word.Table myTable;
Microsoft.Office.Interop.Word.Range wrdRng = myDoc.Bookmarks.get_Item( ref oEndOfDoc ).Range;
myTable = myDoc.Tables.Add( wrdRng, 2, 3, ref oMissing, ref oMissing );
myTable.Range.Font.Name = "標楷體";
myTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
myTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
myTable.Cell( 1, 1 ).Range.Font.Size = 9;
myTable.Cell( 1, 1 ).Range.Text = "姓名";
myTable.Cell( 1, 1 ).Width = 50;
myTable.Cell( 1, 2 ).Range.Font.Size = 9;
myTable.Cell( 1, 2 ).Range.Text = "電話";
myTable.Cell( 1, 2 ).Width = 60;
myTable.Cell( 1, 3 ).Range.Font.Size = 9;
myTable.Cell( 1, 3 ).Range.Text = "日期";
myTable.Cell( 1, 3 ).Width = 50;
myTable.Cell( 2, 1 ).Range.Font.Size = 9;
myTable.Cell( 2, 1 ).Range.Text = "張小明";
myTable.Cell( 2, 1 ).Width = 50;
myTable.Cell( 2, 2 ).Range.Font.Size = 9;
myTable.Cell( 2, 2 ).Range.Text = "0910111222";
myTable.Cell( 2, 2 ).Width = 60;
myTable.Cell( 2, 3 ).Range.Font.Size = 9;
myTable.Cell( 2, 3 ).Range.Text = "2008/12/14";
myTable.Cell( 2, 3 ).Width = 50;
編輯現存表格的某一欄:myDoc.Tables[1].Cell ( 2, 2 ).Range.InsertAfter ( "我的文字內容" );
【插入圖片】
Microsoft.Office.Interop.Word.Range wrdRng = myDoc.Bookmarks.get_Item( ref oEndOfDoc ).Range;
wrdRng.InlineShapes.AddPicture( "D:\\test.jpg", ref oMissing, ref oMissing, ref oMissing );
【參考資料】
http://support.microsoft.com/kb/301659
http://support.microsoft.com/kb/316384
以下內文出自: http://blog.xuite.net/hcktony/blog/15551631-%E3%80%90C%23%E3%80%91%E7%A8%8B%E5%BC%8F%E6%8E%A7%E5%88%有關翻譯的問題歡迎諮詢天成翻譯公司
留言列表