我设法在 Excel 中使用以下 VB 宏将其转换为表格,该表格通过一些文本处理转换为 MySQL 查询以将其插入数据库
Sub ReversePivotTable()
' Before running this, make sure you have a summary table with column headers.
' The output table will have three columns.
Dim SummaryTable As Range, OutputRange As Range
Dim OutRow As Long
Dim r As Long, c As Long
On Error Resume Next
Set SummaryTable = ActiveCell.CurrentRegion
If SummaryTable.Count = 1 Or SummaryTable.Rows.Count < 3 Then
MsgBox "Select a cell within the summary table.", vbCritical
Exit Sub
End If
SummaryTable.Select
Set OutputRange = Application.InputBox(prompt:="Select a cell for the 3-column output", Type:=8)
' Convert the range
OutRow = 2
Application.ScreenUpdating = False
OutputRange.Range("A1:C3") = Array("To", "From", "Fare")
For r = 2 To SummaryTable.Rows.Count
For c = 2 To SummaryTable.Columns.Count
If SummaryTable.Cells(r, c) <> Empty And IsNumeric(SummaryTable.Cells(r, c)) Then
OutputRange.Cells(OutRow, 1) = SummaryTable.Cells(r, r)
OutputRange.Cells(OutRow, 2) = SummaryTable.Cells(c, c)
OutputRange.Cells(OutRow, 3) = SummaryTable.Cells(r, c)
OutputRange.Cells(OutRow, 3).NumberFormat = SummaryTable.Cells(r, c).NumberFormat
OutRow = OutRow + 1
End If
Next c
Next r
End Sub
这将给我 3 列说明 location_from、location_to 和 price,如 @luc-franken 所述。我将其保存为 CSV 文件,并通过一些正则表达式替换设法将其转换为 MySQL 查询。