實例2.1 通過控制台實現對Excel的自動化處理 書本第32頁 註:添加兩個引用: 第一個:程式集—框架—“System.Windows.Forms 4.0.0.0”第二個:程式集—擴展—“Microsoft.Office.Interop.Excel 14.0.0.0” 程式清單2.1通過控制台程 ...
實例2.1 通過控制台實現對Excel的自動化處理 書本第32頁
註:添加兩個引用:
第一個:程式集—框架—“System.Windows.Forms 4.0.0.0
”
第二個:程式集—擴展—“Microsoft.Office.Interop.Excel 14.0.0.0”
程式清單2.1通過控制台程式對Excel自動化處理
Imports Excel = Microsoft.Office.Interop.Excel Module Module1 Private exitXL As Boolean = False Dim WithEvents myExcelApp As Excel.Application Sub Main() myExcelApp = New Excel.Application myExcelApp.Visible = True myExcelApp.StatusBar = "Hello World" myExcelApp.Workbooks.Add() While exitXL = False System.Windows.Forms.Application.DoEvents() End While End Sub Private Sub myExcelApp_SheetBeforeDoubleClick(ByVal sheet _ As Object, ByVal target As Excel.Range, ByRef cancel _ As Boolean) Handles myExcelApp.SheetBeforeDoubleClick exitXL = True End Sub End Module
實例代碼:
Imports Excel = Microsoft.Office.Interop.Excel Module Module1 Private exitXL As Boolean = False Dim WithEvents myExcelApp As Excel.Application '有這句需添加引用“Microsoft.Office.Interop.Excel 14.0.0.0” Sub Main() myExcelApp = New Excel.Application myExcelApp.Visible = True myExcelApp.StatusBar = "Hello World" myExcelApp.Workbooks.Add() While exitXL = False System.Windows.Forms.Application.DoEvents() '有這句需添加引用“System.Windows.Forms 4.0.0.0” End While End Sub Private Sub myExcelApp_SheetBeforeDoubleClick(ByVal sheet _ As Object, ByVal target As Excel.Range, ByRef cancel _ As Boolean) Handles myExcelApp.SheetBeforeDoubleClick exitXL = True End Sub End Module '************************************************************************** '*雙擊單元格Office控制權會轉回到自動化程式事件處理中, '*若沒有System.Windows.Forms.Application.DoEvents(),控制台視窗將自動關閉, '*System.Windows.Forms.Application.DoEvents()方法可以使窗體處理其他事件, '*所以窗體能夠進行重繪。不至於出現假死現象。 '**************************************************************************
實例效果:
實例2.2 wiki文本表示形式 書本第33頁
程式清單2.2 表2.1的wiki文本表示形式
||Property or Method||Name||Return Type|| ||Property||Application||Application|| ||Property||Autoload||Boolean|| ||Property||Compiled||Boolean|| ||Property||Creator||Int32|| ||Method||Delete||Void|| ||Property||Index||Int32|| ||Property||Installed||Boolean|| ||Property||Name||String|| ||Property||Parent||Object|| ||Property||Path||String||
實例2.3 將文本文件中的wiki形式的文本以表格的形式輸出到Word中 書本37頁
程式清單2.3 完整的WordWiki實現
Imports System.Collections.Generic Imports System.Text Imports System.IO Imports Office = Microsoft.Office.Core Imports Word = Microsoft.Office.Interop.Word Module Module1 Sub Main(ByVal args As String()) Dim theApplication As New Word.Application theApplication.Visible = True Dim theDocument As Word.Document theDocument = theApplication.Documents.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 ' Read rows and calculate number of rows and columns 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 this is the first row, ' calculate the number of columns If rowCount = 1 Then Dim splitHeaderRow As String() = _ row.Split(separators, StringSplitOptions.None) ' Ignore the first and last separator columnCount = splitHeaderRow.Length - 2 End If row = reader.ReadLine() End While ' Create a table Dim range As Word.Range = theDocument.Range() Dim table As Word.Table = range.Tables.Add(range, _ rowCount, columnCount) ' Populate table Dim columnIndex As Integer = 1 Dim rowIndex As Integer = 1 For Each r As String In rowList Dim splitRow As String() = r.Split(separators, _ StringSplitOptions.None) For columnIndex = 1 To columnCount Dim cell As Word.Cell = table.Cell(rowIndex, columnIndex) cell.Range.Text = splitRow(columnIndex) Next rowIndex += 1 Next ' Format table table.Rows(1).Range.Bold = 1 table.AutoFitBehavior( _ Word.WdAutoFitBehavior.wdAutoFitContent) ' Wait for input from the command line before exiting System.Console.WriteLine("Table complete.") System.Console.ReadLine() ' Quit without saving changes theApplication.Quit(False) End Sub End Module
實例代碼:
Imports System.Collections.Generic '預設 Imports System.Text '預設 Imports System.IO '預設 Imports Office = Microsoft.Office.Core '添加引用“Microsoft Office 14.0 Object Library 2.5 Imports Word = Microsoft.Office.Interop.Word '添加引用"Microsoft.Office.Interop.word 14.0.0.0" Module Module1 Sub Main(ByVal args As String()) Dim theApplication As New Word.Application '定義word程式 theApplication.Visible = True '使word程式可視 Dim theDocument As Word.Document '定義word文檔 theDocument = theApplication.Documents.Add() '為程式添加word文檔 Dim reader As TextReader '定義Txt文本讀取器 reader = New System.IO.StreamReader(My.Application.Info.DirectoryPath & "/test.txt") '實例化讀取文本介面,My.Application.Info.DirectoryPath指的是本程式的\bin\Debug目錄 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) 'StringSplitOptions.None,就是分開的數組元素包括空元素 columnCount = splitHeaderRow.Length - 2 ' 忽略第一和最後一個分隔符 End If row = reader.ReadLine() End While ' 在word中創建一個表 Dim range As Word.Range = theDocument.Range() '定義文檔單元格 Dim table As Word.Table = range.Tables.Add(range, rowCount, columnCount) '創建一個rowCount行columnCount列的表格 ' 操作word中所創建的表 Dim columnIndex As Integer = 1 Dim rowIndex As Integer = 1 For Each r As String In rowList Dim splitRow As String() = r.Split(separators, StringSplitOptions.None) 'StringSplitOptions.None,就是分開的數組元素包括空元素 For columnIndex = 1 To columnCount Dim cell As Word.Cell = table.Cell(rowIndex, columnIndex) '\bin\Debug目錄中test.txt文件中的結尾不能有多餘的空行,不然會提示超出索引範圍而出現錯誤 cell.Range.Text = splitRow(columnIndex) Next rowIndex += 1 Next ' 格式化表格 table.Rows(1).Range.Bold = 1 table.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent) 'AutoFitBehavior()方法的作用就是以某種方法調整表格,ord.WdAutoFitBehavior.wdAutoFitContent表示表格根據內容來調節 ' 退出前等待命令輸入 System.Console.WriteLine("Table complete.") System.Console.ReadLine() ' 沒有保存更改而退出 theApplication.Quit(False) End Sub End Module
test.txt文檔中的內容
||Property or Method||Name||Return Type||
||Property||Application||Application||
||Property||Autoload||Boolean||
||Property||Compiled||Boolean||
||Property||Creator||Int32||
||Method||Delete||Void||
||Property||Index||Int32||
||Property||Installed||Boolean||
||Property||Name||String||
||Property||Parent||Object||
||Property||Path||String||
|
實例效果: