如何将 XLS 的特定列插入 MS SQL Server 表中?

发布于 2024-10-20 17:24:18 字数 228 浏览 3 评论 0原文

我有一个大约 1000 行和大约 15 列的 Excel 电子表格。我想做的是将这些数据导入到表中,但有选择地导入。我想要从第 5 行(比如说)开始的数据,直到从工作表末尾开始大约 5 行。这是因为行数可能会有所不同,但我确信无论电子表格中的行数如何,我都不需要最后 4-5 行。此外,我只想插入几列,例如 Col1-Col5、Col7-Col9 和 Col12-Col15。是否有一个命令/命令序列可以用来实现这一目标?请尽快告诉我。多谢!

I have an Excel spreadsheet of about 1000 rows and about 15 columns. What I would like to do is to import this data into a table, but selectively. I want data from Row 5(say) onwards till about 5 rows from the end of the sheet. This is because the number of rows may vary, but I am sure that I will not need the last 4-5 rows irrespective of the number of rows in the spreadsheet. Additionally, I would like to insert only a few columns, as, Col1-Col5, Col7-Col9, and Col12-Col15. Is there one command/sequence of commands that I can use to achieve this? Please let me know ASAP. Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2024-10-27 17:24:18

下面的代码根据 xls 中的内容创建一个包含 SQL 指令的文件。您只需添加一个宏,粘贴它,然后更改一些内容(添加一段时间以用标题行填充 ColFields 集合,其中列名称必须与表字段名称匹配,声明不匹配的内容,为 fileName 指定一个值。 ..)。
然后执行宏,您将拥有一个包含您想要的所有插入内容的文件,然后您只需在您的基础上执行该文件即可。

Sub Macro1()
    Dim NbOfLines = Worksheets(Sheet1).Range("A65536").End(xlUp).Row - 5 'Not the 5 last lines
    Dim ColFields As New Collection 'Do a while on the title line to fill the collection with wanted columns titles (do not add ignored columns)

    Dim StartSql As String
        StartSql = "INSERT INTO " + TableName + "("
        For Each loopField In ColFields
            StartSql = StartSql + loopField + ","
        Next
        StartSql = Left(StartSql, Len(StartSql) - 1)
        StartSql = StartSql + ") SELECT "


    Dim Value As String
    For i = 1 To NbOfLines
    Sql = ""
        j = 1
        For Each loopField In ColFields
            Value = Worksheets(SheetName).Cells(i, j).Value
            Sql = Sql + IIf(Value = "", "NULL", "'" + Replace(Value, "'", "''") + "'") + ","
            j = j + 1
        Next
        Sql = Left(Sql, Len(Sql) - 1)
        Sql = StartSql + Sql + vbCrLf
        Call WriteLine(Sql, FileName)
    Next


    End Sub


    Public Sub WriteLine(Ligne As String, FileName As String)

    Open FileName For Append As #1
        Print #1, Ligne
    Close
    End Sub

编辑:我知道这不是最好的方法(也不是最漂亮的方法),我把它给你是因为几周前我用它来将数据从数据库导入到另一个数据库(但我只需要执行一次,而不是每天)。
我也知道有一种方法可以用 OpenRowSet 来做到这一点,你是对的,但我只是不知道如何做(我会经常访问此页面,希望有人能教我)。
最后,我鼓励您阅读此页面:带有解决方案的博客页面
(您可以在这里找到很棒的过程“uftReadfileAsTable”:过程代码)
祝你好运!

The code down there creates a file with SQL instructions based on what's in your xls. You just have to add a Macro, paste it, and change a few things (add a while to fill the ColFields Collection with the title line where the column names must match the table field names, declare what's not, give a value to fileName...).
Then execute the Macro and you'll have a file with all the insert you want, then you'll just have to execute this file on your base.

Sub Macro1()
    Dim NbOfLines = Worksheets(Sheet1).Range("A65536").End(xlUp).Row - 5 'Not the 5 last lines
    Dim ColFields As New Collection 'Do a while on the title line to fill the collection with wanted columns titles (do not add ignored columns)

    Dim StartSql As String
        StartSql = "INSERT INTO " + TableName + "("
        For Each loopField In ColFields
            StartSql = StartSql + loopField + ","
        Next
        StartSql = Left(StartSql, Len(StartSql) - 1)
        StartSql = StartSql + ") SELECT "


    Dim Value As String
    For i = 1 To NbOfLines
    Sql = ""
        j = 1
        For Each loopField In ColFields
            Value = Worksheets(SheetName).Cells(i, j).Value
            Sql = Sql + IIf(Value = "", "NULL", "'" + Replace(Value, "'", "''") + "'") + ","
            j = j + 1
        Next
        Sql = Left(Sql, Len(Sql) - 1)
        Sql = StartSql + Sql + vbCrLf
        Call WriteLine(Sql, FileName)
    Next


    End Sub


    Public Sub WriteLine(Ligne As String, FileName As String)

    Open FileName For Append As #1
        Print #1, Ligne
    Close
    End Sub

edit : I know it's not the best method (nor the most beautiful one), I gave it to you cos' I used it a few weeks ago to import data from a DB to another (but I needed to do it only once, not everyday).
I also know there is a way to do it with an OpenRowSet you're right about it, but I just don't know how (I'll go often on this page wishing someone'll teach me).
Finally, I encourage you to read this page : A Blog Page With A Solution
(You'll find the great procedure 'uftReadfileAsTable' here : The Procedure Code)
Good Luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文