VBS 读取Excel并写文件

VBS 读取Excel并写文件

Private Sub CommandButton1_Click()

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

Dim f
Set f = fso.CreateTextFile("a.txt", True)

For i = 2 To 40
If Cells(i, 1) <> "" Then

Dim sql As String
sql = ""
sql = sql + "update aceplus.costedassembly ca set ca.end_period_oid = (select period_oid from aceplus.period where month_date = '"
sql = sql + GetExtendDate(i)
sql = sql + "' and type = 'MONTH') where ca.costedassemblyoid in (select ca2.costedassemblyoid from aceplus.costedassembly ca2, aceplus.product_family pf, aceplus.structuredassembly sa where ca2.product_family_oid = pf.product_family_oid and rtrim(pf.name_family) in ("
sql = sql + GetExtendFamilies(i)
sql = sql + ") and ca2.structassemblyoid = sa.structassemblyoid and sa.cycle_oid in (select cycle_oid from aceplus.cycle where cycle_name = 'CURRENT'));"

f.WriteLine (sql)
f.WriteLine ("")
End If

Next

f.WriteLine ("update aceplus.cost_element ce set ce.to_period_oid = (select end_period_oid from aceplus.costedassembly ca where ce.costedassemblyoid = ca.costedassemblyoid) where cost_element_oid in (select cost_element_oid from aceplus.cost_element ce, aceplus.costedassembly ca, aceplus.structuredassembly sa, aceplus.ace_element_spec ces where sa.cycle_oid = 'CURRENT' and ca.structassemblyoid = sa.structassemblyoid and ce.costedassemblyoid = ca.costedassemblyoid and ce.element_spec_oid = ces.element_spec_oid and ces.builder_class is NULL  );")

f.Close

 

End Sub


Private Function GetExtendDate(ByVal rowNum As Integer) As String

    GetExtendDate = Cells(rowNum, 1)

End Function

Private Function GetExtendFamilies(ByVal rowNum As Integer) As String

Dim fsot As Object
Set fsot = CreateObject("Scripting.FileSystemObject")

'Open family test file as appending mode
Dim ft
Set ft = fsot.OpenTextFile(GetFamilyTesteFileName(), 8)

'The main sql script of family test script file
Dim sql As String
sql = "select name_family from aceplus.product_family where name_family="

    Dim families As String
    families = "'" + Cells(rowNum, 2) + "'"
   
    'Write script to family test script file
    ft.WriteLine (sql + "'" + Cells(rowNum, 2) + "'")
   
    For iCol = 3 To 100
   
        If Cells(rowNum, iCol) <> "" Then
            families = families + ", '" + Cells(rowNum, iCol) + "'"
           
            'Write script to family test script file
            ft.WriteLine (sql + "'" + Cells(rowNum, iCol) + "'")
           
        End If
       
    Next
   
    'Close family test script file
    ft.Close
   
    GetExtendFamilies = families

End Function

Private Function GenerateFamilyTestScriptFile()

    Dim fsot As Object
    Set fsot = CreateObject("Scripting.FileSystemObject")
   
    Dim ft
    Set ft = fsot.CreateTextFile(GetFamilyTesteFileName(), True)
   
    ft.Close

End Function

Private Function GetExtendHorizonDateFileName() As String

    GetExtendHorizonDateFileName = Cells(41, 2)

End Function

Private Function GetFamilyTesteFileName() As String

    GetFamilyTesteFileName = Cells(42, 2)

End Function

你可能感兴趣的:(VBS 读取Excel并写文件)