如何将 XLS 的特定列插入 MS SQL Server 表中?
我有一个大约 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面的代码根据 xls 中的内容创建一个包含 SQL 指令的文件。您只需添加一个宏,粘贴它,然后更改一些内容(添加一段时间以用标题行填充 ColFields 集合,其中列名称必须与表字段名称匹配,声明不匹配的内容,为 fileName 指定一个值。 ..)。
然后执行宏,您将拥有一个包含您想要的所有插入内容的文件,然后您只需在您的基础上执行该文件即可。
编辑:我知道这不是最好的方法(也不是最漂亮的方法),我把它给你是因为几周前我用它来将数据从数据库导入到另一个数据库(但我只需要执行一次,而不是每天)。
我也知道有一种方法可以用 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.
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!