2 维度 Excel Table 到 CSV 用于 SQL 插入

2 Dimensional Excel Table to CSV for SQL Insert

这可能是个愚蠢的问题,但我是数据库设计的新手,正在努力将复杂的 excel 电子表格移动到 SQL 数据库中。

以下是我的摘录:

这就是我想要将其转换成的内容,使用包含 R 的任何单元格的 X、Y 坐标创建记录:

我在第一张图片中圈出的单元格是我插入到第二张图片中的示例记录。这些有很多,所以我想找到最有效的方法而不是手动输入每一个。谢谢你的帮助;非常感谢!

这里有一些快速 VBA 可以满足您的需要。

Sub flatten()
    Dim rng2Dim As Range
    Dim rngCol As Range
    Dim rngRow As Range
    Dim intRow As Integer
    Dim outPutSheet As Worksheet


    'Set this to the range that has your two dim data.
    Set rng2Dim = Sheets("Sheet1").Range("A1:G6")

    'Set this to the sheet that you will write this data to
    Set outPutSheet = Sheets("Sheet2")

    'This is the first row we will write to in the outputsheet
    intRow = 1

    'Loop through each column in the range
    For Each rngCol In rng2Dim.Columns

        'Loop through each Row in the range (so Column 1, Row 1. Then Row 2. Then Row 3...)
        For Each rngRow In rng2Dim.Rows

                'See if the Row/Column has an "R"
                If rng2Dim.Parent.Cells(rngRow.Row, rngCol.Column).Value = "R" Then

                    'Save the Row and Column values from the sheet and the "R"
                    outPutSheet.Cells(intRow, 1).Value = rngCol.Cells(1, 1).Value
                    outPutSheet.Cells(intRow, 2).Value = rngRow.Cells(1, 1).Value
                    outPutSheet.Cells(intRow, 3).Value = "R"

                    'increment this so we write to the next row when we find another "R"
                    intRow = intRow + 1
                End If
        Next
    Next rngCol
End Sub