您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
小编给大家分享一下VB.NET如何使用Excel文件,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
一、VB.NET Excel文件函数代码:
view plaincopy to clipboardprint? Option Explicit Public Function MergeXlsFile(ByVal strPath As String, Optional ByVal SheetCount As Byte = 1) As Boolean Dim i As Integer Dim strSrcFile As String Dim nRows As Long, nCols As Long, nSheets As Byte, nNewRows() As Integer Dim xlApp As Object, xlSrcBook As Object, xlNewBook As Object, xlSheet As Object, xlRange As Object On Error Resume Next If Right(strPath, 1) <> "\" Then strPathstrPath = strPath & "\" '如果需要合并文件中的工作表数量小于1则退出 If SheetCount < 1 Then Exit Function '删除掉该路径下原来的合并文件 If Dir(strPath & "合并后的文件.xls") <> "" Then Kill strPath & "合并后的文件.xls" '获得第1个XLS文件 strSrcFile = Dir(strPath & "*.xls") '如果文件不存在则退出 If Len(strSrcFile) = 0 Then Exit Function '创建一个Excel实例 Set xlApp = CreateObject("Excel.Application") '新建一个工作簿 Set xlNewBook = xlApp.Workbooks.Add '调整新建工作簿里工作表的数量 ReDim nNewRows(1 To SheetCount) For i = 1 To SheetCount - xlNewBook.Sheets.Count xlNewBook.Sheets.Add , xlNewBook.Sheets(xlNewBook.Sheets.Count) Next '循环查找当前路径下的所有XLS文件 Do '打开找到的XLS文件 Set xlSrcBook = xlApp.Workbooks.Open(strPath & strSrcFile) '循环复制源XLS文件里的工作表 nSheets = IIf(xlSrcBook.Sheets.Count < SheetCount, xlSrcBook.Sheets.Count, SheetCount) For i = 1 To nSheets Set xlSheet = xlSrcBook.Sheets(i) '获得源XLS文件中第i个工作表实际数据的行列数 nRows = xlSheet.UsedRange.Rows.Count nCols = xlSheet.UsedRange.Columns.Count '使用范围对象粘贴源XLS文件数据到合并结果文件中 Set xlRange = xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(nRows, nCols)) xlRange.Select xlRange.Copy xlNewBook.Sheets(i).Cells(nNewRows(i) + 1, 1).PasteSpecial &HFFFFEFF8 '保存合并结果文件中第i个工作表的行数 nNewRows(i) = xlNewBook.Sheets(1).UsedRange.Rows.Count Next '关闭打开的源XLS文件 xlSrcBook.Close '继续查找下一个XLS文件 strSrcFile = Dir() Loop Until Len(strSrcFile) = 0 '保存并关闭合并结果文件 xlNewBook.SaveAs strPath & "合并后的文件.xls" xlNewBook.Close '退出Excel实例 xlApp.Quit '释放资源 Erase nNewRows Set xlRange = Nothing Set xlSheet = Nothing Set xlNewBook = Nothing Set xlSrcBook = Nothing If Err.Number = 0 Then MergeXlsFile = True End Function Option Explicit Public Function MergeXlsFile(ByVal strPath As String, Optional ByVal SheetCount As Byte = 1) As Boolean Dim i As Integer Dim strSrcFile As String Dim nRows As Long, nCols As Long, nSheets As Byte, nNewRows() As Integer Dim xlApp As Object, xlSrcBook As Object, xlNewBook As Object, xlSheet As Object, xlRange As Object On Error Resume Next If Right(strPath, 1) <> "\" Then strPathstrPath = strPath & "\" '如果需要合并文件中的工作表数量小于1则退出 If SheetCount < 1 Then Exit Function '删除掉该路径下原来的合并文件 If Dir(strPath & "合并后的文件.xls") <> "" Then Kill strPath & "合并后的文件.xls" '获得第1个XLS文件 strSrcFile = Dir(strPath & "*.xls") '如果文件不存在则退出 If Len(strSrcFile) = 0 Then Exit Function '创建一个Excel实例 Set xlApp = CreateObject("Excel.Application") '新建一个工作簿 Set xlNewBook = xlApp.Workbooks.Add '调整新建工作簿里工作表的数量 ReDim nNewRows(1 To SheetCount) For i = 1 To SheetCount - xlNewBook.Sheets.Count xlNewBook.Sheets.Add , xlNewBook.Sheets(xlNewBook.Sheets.Count) Next '循环查找当前路径下的所有XLS文件 Do '打开找到的XLS文件 Set xlSrcBook = xlApp.Workbooks.Open(strPath & strSrcFile) '循环复制源XLS文件里的工作表 nSheets = IIf(xlSrcBook.Sheets.Count < SheetCount, xlSrcBook.Sheets.Count, SheetCount) For i = 1 To nSheets Set xlSheet = xlSrcBook.Sheets(i) '获得源XLS文件中第i个工作表实际数据的行列数 nRows = xlSheet.UsedRange.Rows.Count nCols = xlSheet.UsedRange.Columns.Count '使用范围对象粘贴源XLS文件数据到合并结果文件中 Set xlRange = xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(nRows, nCols)) xlRange.Select xlRange.Copy xlNewBook.Sheets(i).Cells(nNewRows(i) + 1, 1).PasteSpecial &HFFFFEFF8 '保存合并结果文件中第i个工作表的行数 nNewRows(i) = xlNewBook.Sheets(1).UsedRange.Rows.Count Next '关闭打开的源XLS文件 xlSrcBook.Close '继续查找下一个XLS文件 strSrcFile = Dir() Loop Until Len(strSrcFile) = 0 '保存并关闭合并结果文件 xlNewBook.SaveAs strPath & "合并后的文件.xls" xlNewBook.Close '退出Excel实例 xlApp.Quit '释放资源 Erase nNewRows Set xlRange = Nothing Set xlSheet = Nothing Set xlNewBook = Nothing Set xlSrcBook = Nothing If Err.Number = 0 Then MergeXlsFile = True End Function
二、VB.NET Excel文件调用方法:
view plaincopy to clipboardprint? Sub main() If MergeXlsFile("c:\temp", 1) Then MsgBox "数据已成功合并!", vbInformation, "提示" Else MsgBox "数据合并失败!", vbCritical, "提示" End If End Sub
以上是“VB.NET如何使用Excel文件”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。