VB.NET的一些应用

 

1. Windows API 的应用

 

● 声明动态链接库中的程序引用

 1 Public Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" _
 2     (ByVal lpAppName As String,
 3      ByVal lpReturnedString As String,
 4      ByVal nSize As Integer,
 5      ByVal lpFileName As String) As Integer
 6 ' 指向包含 Section 名称的字符串地址
 7 ' 返回数据的缓冲区地址
 8 ' 返回数据的缓冲区长度
 9 ' ini 文件的文件名
10 ' 返回实际读取到的长度

 

● 调用API方法

 1 Dim sPath As String = Application.StartupPath & "/" & USER_FILE_NAME
 2 Dim i As Integer
 3 Dim sRetUserFile As String = Nothing
 4 
 5 sRetUserFile = Space(READ_LENTH)
 6 i = GetPrivateProfileSection("SAB0806M01",
 7                              sRetUserFile,
 8                              sRetUserFile.Length,
 9                              sPath)
10 
11 If i > 0 Then
12 13 End If

 

2. 用正则表达式分割字符串

 

 1 Dim lstFuncs() As String
 2 Dim mclMatches As MatchCollection
 3 Dim IElements As IEnumerator
 4 Dim j As Integer
 5 Dim lstItems As List(Of String)
 6 
 7 lstFuncs = Regex.Split(sReturnedString.Trim(), "\D{3}\d{4}\D{1}\d{2}=")
 8 mclMatches = Regex.Matches(sReturnedString.Trim(), "\D{3}\d{4}\D{1}\d{2}=")
 9 
10 IElements = mclMatches.GetEnumerator
11 j = 1
12 lstItems = New List(Of String)
13 
14 While IElements.MoveNext()
15     lstItems.Add(IElements.Current.ToString() & lstFuncs(j))
16     j += 1
17 End While

 

3. 获取环境变量

 

1 Dim systemPath As String
2 systemPath = Environment.GetEnvironmentVariable("USERPROFILE") & "/AppData/Local/Esa/"

 

4. 运行Excel中的宏(生成报表)

 

 1     Imports Excel = Microsoft.Office.Interop.Excel
 2 
 3     ''' <summary>
 4     ''' 运行Excel中的宏(生成报表)
 5     ''' </summary>
 6     ''' <param name="sFilePath">Excel文件位置</param>
 7     ''' <param name="sCsvPath">Csv文件位置</param>
 8     ''' <param name="sSavePath">生成的报表保存位置</param>
 9     Private Sub RunExcelVba(
10                            ByVal sFilePath As String,
11                            ByVal sCsvPath As String,
12                            ByVal sSavePath As String
13                            )
14 
15         Dim oExcel As Excel.Application = New Excel.Application
16 
17         ' 不打开Excel窗口,后台运行
18         oExcel.Visible = False
19 
20         Dim oBooks As Excel.Workbooks = oExcel.Workbooks
21 
22         ' 打开Excel
23         Dim oBook As Excel.Workbook = oBooks.Open(sFilePath)
24 
25         oExcel.Run("createReport", sCsvPath)
26 
27         ' 文件保存
28         oExcel.ActiveWorkbook.SaveAs(sSavePath,
29                                      FileFormat:=Excel.XlFileFormat.xlExcel8)
30 
31         ' 操作完成后
32         oBook.Close(False)
33         Runtime.InteropServices.Marshal.ReleaseComObject(oBook)
34         oBook = Nothing
35         Runtime.InteropServices.Marshal.ReleaseComObject(oBooks)
36         oBooks = Nothing
37         oExcel.Quit()
38         Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
39         oExcel = Nothing
40 
41     End Sub

 

posted @ 2012-11-03 15:49  静水无踪  阅读(247)  评论(0编辑  收藏  举报