用excel导出文件有很多种方法,用python也是一种方法。有一种方法更加简单,那就是用excel自带的编程vba编程,可以直接对excel进行数据操作。
Sub exportJosn()
Dim s As String
Dim fullName As String
Dim Data1 As String
Dim rng As Range
Dim xLen As Integer
Dim yLen As Integer
Dim r1 As Integer
Dim c1 As Integer
fullName = Replace(ActiveWorkbook.fullName, ".xlsm", ".lua")
xLen = Range("a1").CurrentRegion.Columns.Count
yLen = Range("a1").CurrentRegion.Rows.Count
Open fullName For Output As #1
Print #1, "_G.FileName= "
Print #1, "{"
For r1 = 2 To yLen
s = ""
For c1 = 1 To xLen
If (Application.IsNumber(Cells(r1, c1).Value)) Or Left(Cells(r1, c1).Value, 1) = Chr(34) Then
s = s & Cells(1, c1).Value & " = " & Cells(r1, c1).Value
Else
s = s & Cells(1, c1).Value & " = " & Chr(39) & Cells(r1, c1).Value & Chr(39)
End If
If c1 < xLen Then
s = s & ", "
End If
Next
If r1 < yLen Then
Print #1, "[" & Cells(r1, 1).Value & "]" & " = {" & s & "}, "
Else
Print #1, "[" & Cells(r1, 1).Value & "]" & " = {" & s & "}, "
End If
Next
Print #1, "}"
Close #1
MsgBox ("ok!")
其实就是在excel里面画一个按钮,然后点击一下就能执行代码。上面的代码Print就是打印部分,将内容打印到lua里面去。Cells(r1, c1).Value是取得excel表的准确位置的数据。我们只需要按照需求编写好代码,直接让策划配置好数据,只要一键就能把数据导出到lua。导出完之后直接用MsgBox提示一下就行了!!
全部的代码如上,也并没有想象中那么难。欢迎和我讨论有关程序的问题,也可以答疑。关注公众号:诗一样的代码,交一个朋友。