Excel VBA数据导出

游戏中很多配置文件都采用Excel保存数据。但实际使用时,可能需要对Excel数据再处理成我们需要的格式。基于此需求,学习了Excel的VBA来导出数据到文件中。

开启VBA

Excel中的开发工具默认都没有打开,此处需要百度一下如何打开对应版本的Excel。(注:高版本的开发工具需要专业版或商业版才能使用,最好不要使用过高版本,最低office excel 2007版)

宏设置

Excel默认禁用所有宏,在宏安全中可以设启用所有宏

新建模块

打开Visual Basic面板,选中工程面板下任意选项右键-插入-模块。

代码示例

Option Explicit
Sub parseAndOutputData()
    Dim result As String
    Dim file_name As String
    Dim file_path As String
    Dim info_row As Integer
    Dim sheets_count As Integer
    
    If Len(Worksheets(1).Cells(2, 1).Value) > 0 Then
        file_name = Worksheets(1).Cells(2, 1)
    End If
    If Len(Worksheets(1).Cells(2, 2).Value) > 0 Then
        file_path = Worksheets(1).Cells(2, 2)
    End If
    If Len(Worksheets(1).Cells(2, 3).Value) > 0 Then
        info_row = Worksheets(1).Cells(2, 3)
    End If
    sheets_count = Worksheets.Count
    sheets_count = 0
    While Len(Worksheets(1).Cells(2, sheets_count + 4)) > 0
            sheets_count = sheets_count + 1
    Wend

    If MsgBox("It will Clear File!",VbOKCancel,"提示") = vbCancel Then
        Exit Sub
    End If

    Open ThisWorkbook.Path & "\" & file_name & ".txt" For Output As #1
        Write #1, ""
    Close #1
    
    
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim max_row As Integer
    Dim max_col As Integer

    Open ThisWorkbook.Path & "\" & file_name & ".txt" For Binary As #1
    result = "return {"
    For i = 2 To sheets_count
        max_col = 0
        While Len(Worksheets(i).Cells(info_row, (max_col + 1)).Value) > 0
            max_col = max_col + 1
        Wend

        max_row = info_row
        While Len(Worksheets(i).Cells((max_row + 1), 1).Value) > 0
            max_row = max_row + 1
        Wend

        If i <> 2 Then
            result = result & "," & Chr(10)
        Else
            result = result & Chr(10)
        End If
        result = result & Chr(9) & Worksheets(1).Cells(2, (i + 2)).Value & " = {"

        For j = (info_row + 1) To max_row
            If j <> (info_row + 1) Then
                result = result & "," & Chr(10) & Chr(9) & Chr(9) & "{"
            Else
                result = result & Chr(10) & Chr(9) & Chr(9) & "{"
            End If
            For k = 1 To max_col
                If Len(Worksheets(i).Cells(j, k).Value) > 0 Then
                    If k <> 1 Then
                        result = result & ","
                    End If
                    result = result & Chr(10) & Chr(9) & Chr(9) & Chr(9) & Worksheets(i).Cells(info_row, k) & "=" & Worksheets(i).Cells(j, k).Value
                End If
            Next
            result = result & Chr(10) & Chr(9) & Chr(9) & "}"

            Put #1, , result
            result = ""
        Next
        result = result & Chr(10) & Chr(9) & "}"
    Next
    
    result = result & Chr(10) & "}"
    
    Put #1, , result    
    Close #1
    MsgBox "OutPut Success!"
End Sub

Worksheets(1).Cells(2, 3)代表第一个表的第2行第3列单元格   其中Worksheets(1)代表第一个表,其指多个表中排最前面的表,只与位置有关

Worksheets.Count代表当前xls文件中表的个数

<> 表示不等于 &  表示连接         Chr(9)表示转义字符Tab

在条件语句中 = 表示判断是否相等

ThisWorkbook.Path代表当前文件路径  Output 和 Binary 代表文件打开模式

其中Output 对应用Write写入数据  其会覆盖到文件所有内容。

Binary对应用Put写入数据  其有三个参数,第二个代表可选字节数 ,第三个代表内容

Binary其会覆盖掉最初写入的部分,不会修改其他数据。所以采用Output清除文件内容。

Binary一次最多读写32kb,可以不关闭文件多次顺序写入数据。

#1代表文件号  类似文件句柄吧

MsgBox 弹出框控件   具体使用细节可根据需求百度


本示例代码主要是读取第一个表,获取一些配置信息,比如需要读取多少个表,从第几行开始读取。之后的逻辑主要是为了组织数据的布局

最后在Excel中的开发者面板中点击宏,选择模块再执行。导出文件成功

新建一个空xlsx文件,为其开启宏并添加模块。只要与该xlsx文件在同一文件夹的,其它xlsx文件就可以直接调用其模块导出数据


中文显示问题:

二进制文本输出后的编码格式是ANSI

一般代码文件都采用的是UTF-8    无BOM格式

最简单的方法:

Notepad++可以显示ANSI的中文,但直接将其转换编码格式,其新的格式不能显示原有中文

使用Notepad++新建文件将其编码格式修改为UTF-8    无BOM格式   在将原来的内容拷贝到新文件中即可显示原有中文

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值