一个可以从excel中读取数据并生成xml的vba程序

NND EXCEL2007,让老子找了半天才把button添加上去。

Sub 按钮1_Click()
 
    Dim sFileName As String          
    Dim ws As Worksheet                             '存储文件路径名和文件名的工作表
    Set ws = Worksheets("english")                  '设置工作表
    sFileName = "d:/text.xml"
   
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set orgFile = fso.OpenTextFile(sFileName, 2, True, -1)
   
    'dim head and tail
    Dim headline1, headline2, tailline, span_bef_english, span_aft_english, span_bef_foreign, span_aft_foreign
   
    headline1 = ""
    headline2 = ""
    tailline = "
"
   
    'dim span before english
    span_bef_english = "" & Chr$(13) & ""
    'dim span after english
    span_aft_english = "
"
    'dim span  before foreign language
    span_bef_foreign = ""
    'dim span after forergn language
    span_aft_foreign = "
" & Chr$(13) & "
"
   
   
    'write_xml_head()
    orgFile.Writeline (headline1)
    orgFile.Writeline (headline2)
   
    'write body
    Dim i%, line_data, line_text
    For i = 2 To 810
        line_data = ws.Cells(i, 2).Value
        line_text = span_bef_english & line_data & span_aft_english
        orgFile.Writeline (line_text) 'write english head
        line_data = ws.Cells(i, 5).Value
        line_text = span_bef_foreign & line_data & span_aft_foreign
        orgFile.Writeline (line_text) 'write foreign language data and tail
    Next
   
    'write_xml_tail()
    orgFile.Writeline (tailline)
    orgFile.Close

End Sub

你可能感兴趣的:(excel,xml,vba,encoding,button,工作)