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

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

FileOpen模块

Private Function@H_404_5@ GetNewFile(strTitle,FileFormat@H_404_5@) As String@H_404_5@
    Dim dlgOpen As FileDialog
    Set@H_404_5@ dlgOpen = Application.FileDialog(3@H_404_5@)
    dlgOpen.Title = strTitle
    dlgOpen.AllowMultiSelect = False@H_404_5@
    dlgOpen.Filters.Clear@H_404_5@
    dlgOpen.Filters.Add FileFormat@H_404_5@ & "Files"@H_404_5@,"*."@H_404_5@ & FileFormat@H_404_5@

    Dim vrtSelectedItem As Variant
    If@H_404_5@ dlgOpen.Show@H_404_5@ = -1@H_404_5@ Then
        For@H_404_5@ Each vrtSelectedItem In@H_404_5@ dlgOpen.SelectedItems
        Next@H_404_5@ vrtSelectedItem
        Else: End@H_404_5@
    End@H_404_5@ If@H_404_5@
End@H_404_5@ Function@H_404_5@

Public Function@H_404_5@ BeginFile(ScriptFile,UnicodeFlag,FileFormat@H_404_5@)
    Dim FileName@H_404_5@
    FileName@H_404_5@ = GetNewFile(“导出为” & FileFormat@H_404_5@,FileFormat@H_404_5@)
    strgetFile = FileName@H_404_5@
    Dim FileSystemObj
    Set@H_404_5@ FileSystemObj = CreateObject("Scripting.FileSystemObject"@H_404_5@)
    Set@H_404_5@ ScriptFile = FileSystemObj.OpenTextFile(FileName@H_404_5@,2@H_404_5@,True@H_404_5@,UnicodeFlag)
End@H_404_5@ Function@H_404_5@

public Function@H_404_5@ EndFile(ScriptFile)
    ScriptFile.Close@H_404_5@
End@H_404_5@ Function@H_404_5@

uft8模块

Public@H_404_5@ strgetFile As@H_404_5@ String@H_404_5@

Public@H_404_5@ Declare@H_404_5@ Function@H_404_5@ MultiByteToWideChar Lib@H_404_5@ "kernel"@H_404_5@32@H_404_5@ (_
    ByVal@H_404_5@ CodePage AS@H_404_5@ Long@H_404_5@,_
    ByVal@H_404_5@ dwFlags AS@H_404_5@ Long@H_404_5@,_
    ByRef@H_404_5@ lpMultiByteStr AS@H_404_5@ Any,_
    ByVal@H_404_5@ cchMultiByte AS@H_404_5@ Long@H_404_5@,_
    ByVal@H_404_5@ lpWideCharStr AS@H_404_5@ Long@H_404_5@,_
    ByVal@H_404_5@ cchWideChar  AS@H_404_5@ Long@H_404_5@) As@H_404_5@ Long@H_404_5@

Public@H_404_5@ Declare@H_404_5@ Function@H_404_5@ WideCharToMultiByte Lib@H_404_5@ "kernel32"@H_404_5@(_
    ByVal@H_404_5@ CodePage AS@H_404_5@ Long@H_404_5@,_
    ByRef@H_404_5@ lpWideCharStr AS@H_404_5@ Any,_
    ByVal@H_404_5@ cchWideChar AS@H_404_5@ Long@H_404_5@,_
    ByVal@H_404_5@ lpMultiByteStr AS@H_404_5@ Long@H_404_5@,_
    ByVal@H_404_5@ lpDefultChar As@H_404_5@ String@H_404_5@,_
    ByVal@H_404_5@ lpUseDefultChr As@H_404_5@ Long@H_404_5@) As@H_404_5@ Long@H_404_5@

Publi Const@H_404_5@ CP UTF = 65001@H_404_5@

Sub@H_404_5@ WritUTF8File(strInput As@H_404_5@ Strng,strFile As@H_404_5@ String@H_404_5@,Optional@H_404_5@ bBom As@H_404_5@ Boolean@H_404_5@ = Ture)
    Dim@H_404_5@ bByt As@H_404_5@ Byte@H_404_5@
    Dim@H_404_5@ ReturnByte() As@H_404_5@ Byte@H_404_5@
    Dim@H_404_5@ lngBufferSize As@H_404_5@ Long@H_404_5@
    Dim@H_404_5@ lngResult As@H_404_5@ Long@H_404_5@
    Dim@H_404_5@ TLe As@H_404_5@ Long@H_404_5@

    If@H_404_5@ Len(strInput) = 0@H_404_5@ Then@H_404_5@ Exit@H_404_5@ Sub@H_404_5@
    On@H_404_5@ Error@H_404_5@ GoTo@H_404_5@ errHandle
    If@H_404_5@ Dir(strFilr) <> ""@H_404_5@ Then@H_404_5@ Kill strFile

    TLen = Len(strInput)
    lngBufferSize = TLen * 3@H_404_5@ +1@H_404_5@
    ReDim@H_404_5@ ReturnByte(lngBufferSize - 1@H_404_5@)
    lngResul = WideCharToMultiByte(CP_UTF8,0@H_404_5@,StrPtr(strInput),TLen,_
        ReturnByte(0@H_404_5@),lngBufferSize,vbNullString,0@H_404_5@)
    If@H_404_5@ lngResult Then@H_404_5@
        lngResult = lngResul-1@H_404_5@
        ReDim@H_404_5@ Preserve@H_404_5@ ReturnByte(lngResult)
        Open strFile For@H_404_5@ Binary@H_404_5@ As@H_404_5@ #1@H_404_5@
        If@H_404_5@ bBom = True@H_404_5@ Then@H_404_5@
            bByte = 239@H_404_5@
            Put #1,bByte@H_404_5@
            bByte = 187@H_404_5@
            Put #1,bByte@H_404_5@
            bByt = 191@H_404_5@
            Put #1,bByte@H_404_5@
        End@H_404_5@ If@H_404_5@
        Put #1,ReturnByte@H_404_5@
        Close #1@H_404_5@
    End@H_404_5@ If@H_404_5@
    Exit@H_404_5@ Sub@H_404_5@

    errHandle :
        MsgBox Err.Description,"错误"@H_404_5@ & Err.Number
End@H_404_5@ Sub@H_404_5@

Sub@H_404_5@

猜你在找的VB相关文章