实例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|| |
实例效果:

来源:https://www.cnblogs.com/xiehaofeng/p/12255131.html