博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使EXCEL中使用宏生成T-SQL实现数据导入
阅读量:6329 次
发布时间:2019-06-22

本文共 2712 字,大约阅读时间需要 9 分钟。

     有时我们需要从EXCEL文档中把数据导入到数据库,这时我们可以使用Excel的宏功能。假设如下图这样的DEMO数据:

    然后通过“视图”找到如下选项:

  接着我们创建一个宏,在编辑器中输入(Vbscript):

Sub CreateInsertScript()
Dim Row As Integer
Dim Col As Integer
 
'To store all the columns available in the current active sheet
Dim ColNames(100) As String
 
Col = 1
Row = 1
Dim ColCount As Integer
ColCount = 0
'Get Columns from the sheet
Do Until ActiveSheet.Cells(Row, Col) = "" 'Loop until you find a blank.
ColNames(ColCount) = "[" + ActiveSheet.Cells(Row, Col) + "]"
ColCount = ColCount + 1
Col = Col + 1
Loop
ColCount = ColCount - 1
 
'Inputs for the starting and ending point for the rows
Row = InputBox("Give the starting Row No.")
Dim MaxRow As Integer
MaxRow = InputBox("Give the Maximum Row No.")
 
'File to save the generated insert statements
File = "c:\\InsertCode.txt"
fHandle = FreeFile()
Open File For Output As fHandle
 
Dim CellColCount As Integer
Dim StringStore As String 'Temporary variable to store partial statement
 
Do While Row <= MaxRow
StringStore = ""
CellColCount = 0
'ActiveSheet.Name will give the current active sheet name
'this can be treated as table name in the database
StringStore = StringStore + "insert into [" + ActiveSheet.Name + "] ( "
Do While CellColCount <= ColCount
StringStore = StringStore + ColNames(CellColCount)
'To avoid "," after last column
If CellColCount <> ColCount Then
StringStore = StringStore + " , "
End If
CellColCount = CellColCount + 1
Loop
'Here it will print "insert into [TableName] ( [Col1] , [Col2] , ..."
Print #fHandle, StringStore + " ) "
 
'For printing the values for the above columns
StringStore = " values( "
CellColCount = 0
Do While CellColCount <= ColCount
StringStore = StringStore + " '" + CStr(ActiveSheet.Cells(Row, CellColCount + 1)) + "'"
If CellColCount <> ColCount Then
StringStore = StringStore + ", "
End If
CellColCount = CellColCount + 1
Loop
 
'Here it will print "values( 'value1', 'value2', ..."
Print #fHandle, StringStore + ");"
Print #fHandle, " "
Row = Row + 1
 
Loop
 
Close #fHandle
MsgBox ("Successfully Done")
End Sub
 
 
接着点击运行,好了弹出两个对话框,输入起始行2,结束行5,确定后在生成一个文本文件,这些参数你可以修改的 c:\\InsertCode.txt
内容是我们最终想要的T-SQL:
insert into [Person] ( [Name ] , [Age] , [EnterTime] , [Salary] )
values(  'Peter',  '23',  '2009-01-01',  '3003.5');
 
insert into [Person] ( [Name ] , [Age] , [EnterTime] , [Salary] )
values(  'Lucy',  '21',  '2003-10-01',  '2087.65');
 
insert into [Person] ( [Name ] , [Age] , [EnterTime] , [Salary] )
values(  'Max',  '29',  '2011-01-01',  '1989.11');
 
insert into [Person] ( [Name ] , [Age] , [EnterTime] , [Salary] )
values(  'Eric',  '35',  '1999-05-01',  '5043.2');
 
很简单,您可以自己动手试一下。
希望对您开发有帮助。

作者:
出处:
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-。

你可能感兴趣的文章
html img Src base64 图片显示
查看>>
[Spring学习笔记 7 ] Spring中的数据库支持 RowMapper,JdbcDaoSupport 和 事务处理Transaction...
查看>>
FFMPEG中关于ts流的时长估计的实现(转)
查看>>
Java第三次作业
查看>>
【HDOJ 3652】B-number
查看>>
android代码混淆笔记
查看>>
Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 并查集
查看>>
BMP文件的读取与显示
查看>>
Flash文字效果
查看>>
各种排序算法总结篇(高速/堆/希尔/归并)
查看>>
使用c#訪问Access数据库时,提示找不到可安装的 ISAM
查看>>
Highcharts X轴纵向显示
查看>>
windows 注册表讲解
查看>>
【算法】论平衡二叉树(AVL)的正确种植方法
查看>>
基于DDD的现代ASP.NET开发框架--ABP系列之1、ABP总体介绍
查看>>
【原】东拼西凑PBR(1):PBR基础
查看>>
react 从零开始搭建开发环境
查看>>
scala recursive value x$5 needs type
查看>>
ps -ef |grep 输出的具体含义
查看>>
markdown编辑
查看>>