深入剖析Office開發應用模式 文/沈炳宏 ==========程式========= 程式1 id ,name ,birthday ,score ,address 3021,王秀文 ,05/20/81 ,90,中市忠明路10號 3022,張大明 ,04/09/81 ,100,中市忠明路8號 3023,李正文 ,03/07/81 ,85,中市忠明路9號 3024,張小美 ,03/08/81 ,92.5,中市忠明路5號 ==========程式========= ==========程式========= 程式2 Imports System.IO Imports Excel = Microsoft.Office.Interop.Excel Imports OfficeCore = Microsoft.Office.Core ==========程式========= ==========程式========= 程式3 Module Module1 Sub Main(ByVal args As String()) System.Console.WriteLine("Excel Start.") Dim theApplication As New Excel.Application theApplication.Visible = True Dim myWorksheet As Microsoft.Office.Interop.Excel.Worksheet theApplication.Workbooks.Add() myWorksheet = theApplication.ActiveWorkbook.Worksheets.Add Dim reader As TextReader reader = New System.IO.StreamReader(args(0)) Dim separators(1) As String separators(0) = "," Dim rowCount As Integer = 0 Dim columnCount As Integer = 0 Dim rowList As New System.Collections.Generic.List(Of String) Dim row As String = reader.ReadLine() While row IsNot Nothing rowCount += 1 rowList.Add(row) If rowCount = 1 Then Dim splitHeaderRow As String() = _ row.Split(separators, StringSplitOptions.None) columnCount = splitHeaderRow.Length - 1 End If row = reader.ReadLine() End While Dim columnIndex As Integer = 0 Dim rowIndex As Integer = 1 For Each r As String In rowList Dim splitRow As String() = r.Split(separators, _ StringSplitOptions.None) Dim rr As Excel.Range For columnIndex = 0 To columnCount rr = myWorksheet.Cells(rowIndex, columnIndex + 1) rr.Value = splitRow(columnIndex) If rowIndex = 1 Then rr.Font.Bold = True rr.Font.Color = -16776961 End If Next rowIndex += 1 Next System.Console.WriteLine("Excel complete.") System.Console.ReadLine() theApplication.Quit() End Sub End Module ==========程式========= ==========程式========= 程式4 Public Class ThisApplication Private Sub ThisApplication_ItemSend( ByVal item As Object, _ ByRef cancel As Boolean) Handles Me.ItemSend Dim myItem As Outlook.MailItem If TypeOf item Is Outlook.MailItem Then myItem = CType(item, Outlook.MailItem) For Each recip As Outlook.Recipient In myItem.Recipients If recip.AddressEntry.Members.Count > 5 Then result = MessageBox.Show(“收件人大於5, 是否要送出?) If result = DialogResult.No Then cancel = True Exit For End If End If Next End If End Sub End Class ==========程式========= ==========程式========= 程式5 Private WithEvents myButton As New Button Private Sub ThisDocument_Startup( ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup myButton.Text = "資料繫結範例!" ActionsPane.Controls.Add(myButton) End Sub Private Sub myButton_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles myButton.Click Dim r As New Random Dim theApplication As New Word.Application Dim theDocument As Word.Document theDocument = theApplication.Documents.Add() Dim rowCount As Integer = 5 Dim columnCount As Integer = 3 Dim table As Word.Table = range.Tables.Add(range, _ rowCount, columnCount) Dim columnIndex As Integer = 1 Dim rowIndex As Integer = 1 For rowIndex = 1 To rowCount For columnIndex = 1 To columnCount Dim cell As Word.Cell = table.Cell(rowIndex, columnIndex) cell.Range.Text = r.NextDouble() Next rowIndex += 1 Next table.Rows(1).Range.Bold = 1 table.AutoFitBehavior( _ Word.WdAutoFitBehavior.wdAutoFitContent) End Sub ==========程式=========