VB 将excel内容导出到指定文件中

前端之家收集整理的这篇文章主要介绍了VB 将excel内容导出到指定文件中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

FileOpen模块

  1. Private Function GetNewFile(strTitle,FileFormat) As String
  2. Dim dlgOpen As FileDialog
  3. Set dlgOpen = Application.FileDialog(3)
  4. dlgOpen.Title = strTitle
  5. dlgOpen.AllowMultiSelect = False
  6. dlgOpen.Filters.Clear
  7. dlgOpen.Filters.Add FileFormat & "Files","*." & FileFormat
  8.  
  9. Dim vrtSelectedItem As Variant
  10. If dlgOpen.Show = -1 Then
  11. For Each vrtSelectedItem In dlgOpen.SelectedItems
  12. Next vrtSelectedItem
  13. Else: End
  14. End If
  15. End Function
  16.  
  17. Public Function BeginFile(ScriptFile,UnicodeFlag,FileFormat)
  18. Dim FileName
  19. FileName = GetNewFile(“导出为” & FileFormat,FileFormat)
  20. strgetFile = FileName
  21. Dim FileSystemObj
  22. Set FileSystemObj = CreateObject("Scripting.FileSystemObject")
  23. Set ScriptFile = FileSystemObj.OpenTextFile(FileName,2,True,UnicodeFlag)
  24. End Function
  25.  
  26. public Function EndFile(ScriptFile)
  27. ScriptFile.Close
  28. End Function

uft8模块

  1. Public strgetFile As String
  2.  
  3. Public Declare Function MultiByteToWideChar Lib "kernel"32 (_
  4. ByVal CodePage AS Long,_
  5. ByVal dwFlags AS Long,_
  6. ByRef lpMultiByteStr AS Any,_
  7. ByVal cchMultiByte AS Long,_
  8. ByVal lpWideCharStr AS Long,_
  9. ByVal cchWideChar AS Long) As Long
  10.  
  11. Public Declare Function WideCharToMultiByte Lib "kernel32"(_
  12. ByVal CodePage AS Long,_
  13. ByRef lpWideCharStr AS Any,_
  14. ByVal cchWideChar AS Long,_
  15. ByVal lpMultiByteStr AS Long,_
  16. ByVal lpDefultChar As String,_
  17. ByVal lpUseDefultChr As Long) As Long
  18.  
  19. Publi Const CP UTF = 65001
  20.  
  21. Sub WritUTF8File(strInput As Strng,strFile As String,Optional bBom As Boolean = Ture)
  22. Dim bByt As Byte
  23. Dim ReturnByte() As Byte
  24. Dim lngBufferSize As Long
  25. Dim lngResult As Long
  26. Dim TLe As Long
  27.  
  28. If Len(strInput) = 0 Then Exit Sub
  29. On Error GoTo errHandle
  30. If Dir(strFilr) <> "" Then Kill strFile
  31.  
  32. TLen = Len(strInput)
  33. lngBufferSize = TLen * 3 +1
  34. ReDim ReturnByte(lngBufferSize - 1)
  35. lngResul = WideCharToMultiByte(CP_UTF8,0,StrPtr(strInput),TLen,_
  36. ReturnByte(0),lngBufferSize,vbNullString,0)
  37. If lngResult Then
  38. lngResult = lngResul-1
  39. ReDim Preserve ReturnByte(lngResult)
  40. Open strFile For Binary As #1
  41. If bBom = True Then
  42. bByte = 239
  43. Put #1,bByte
  44. bByte = 187
  45. Put #1,bByte
  46. bByt = 191
  47. Put #1,bByte
  48. End If
  49. Put #1,ReturnByte
  50. Close #1
  51. End If
  52. Exit Sub
  53.  
  54. errHandle :
  55. MsgBox Err.Description,"错误" & Err.Number
  56. End Sub
  57.  
  58. Sub

猜你在找的VB相关文章