小编给大家分享一下VB.NET如何使用Excel文件,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于做网站、网站建设、改则网络推广、微信小程序定制开发、改则网络营销、改则企业策划、改则品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供改则建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
一、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文件”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!