❶ 如何把Excel數據轉化成SQL語句
1、例如我想把圖1的數據給添加到圖2的資料庫中;
❷ 如何將Excel中表結構數據自動生成SQL腳本的方法
一: 在本地PC新建一個Excel文件(例如:excel2007)
准備工作,左鍵選擇excel文本左上角的圖標,選擇「Excle選項」。1. 點擊「信任中心」->「信任中心設置」->「宏設置」->選擇「啟用所有宏...」選項。「開發人員宏設置」選項也勾選上。 2. 點擊「信任中心」->「信任中心設置」->選擇「個人信息選項」,將「文檔特定設置」上面默認選擇去掉,避免在保存腳本時報錯。
二: 在本地PC新建一個excel文件(例如: D:\testdate.xlsx)
按快捷鍵「ALT + F11」進入宏編輯,輸入如下代碼後保存。summary()為目標生成代碼,SQL()為生成SQL腳本文件代碼,按條件生成SQL的腳本如下:
Sub summary()
Dim i As Integer
i = 2
ThisWorkbook.Worksheets(1).Columns(2).Clear
For Each sh In ThisWorkbook.Worksheets
If sh.Name <> " " Then
ThisWorkbook.Worksheets(1).Cells(i, 2).Value = sh.Name
ThisWorkbook.Worksheets(1).Cells(i, 2).Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
sh.Name + "!A1", TextToDisplay:=sh.Name
i = i + 1
End If
Next sh
ThisWorkbook.Worksheets(1).Cells.Select
With Selection.Font
.Name = "目錄"
.Size = 9
.Strikethrough = False
.Superscript. = False
.Subscript. = False
.OutlineFont = False
.Shadow = False
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
End Sub
Sub SQL()
Dim i As Integer
i = 1
Dim ADO_Stream As Object
Dim strSQL, strDelSQL As String
Dim strTblName As String
Dim col As Long
Dim row As Long
Dim str As String
Dim PK As String
Dim cnt As Integer
PK = "PK"
Dim rowcounts As Long
rowcounts = 0
Dim filecount As Long
filecount = 0
Set ADO_Stream = CreateObject("ADODB.Stream")
ADO_Stream.Type = 2
ADO_Stream.Mode = 3
ADO_Stream.Charset = "unicode"
ADO_Stream.Open
Dim checkType As String
For Each sh In ThisWorkbook.Worksheets
cnt = 0
If sh.Name <> " " And InStr(sh.Name, "template") = 0 Then
strTblName = sh.Cells(1, 2).Value
rowcounts = 1
'Insert SQL
row = 6
Do While sh.Cells(row, 1).Value <> ""
strDelSQL = "delete from " + strTblName + " where "
strSQL = "Insert into " + strTblName + " ("
col = 1
Do While sh.Cells(3, col).Value <> ""
If col <> 1 Then
strSQL = strSQL + ", "
End If
strSQL = strSQL + sh.Cells(3, col).Value
col = col + 1
Loop
strSQL = strSQL + ") VALUES ("
col = 1
Do While sh.Cells(3, col).Value <> ""
str = Trim(CStr(sh.Cells(row, col).Value))
If InStr(Trim(CStr(sh.Cells(2, col).Value)), PK) <> 0 Then
If cnt > 0 Then
strDelSQL = strDelSQL + " and "
End If
strDelSQL = strDelSQL + Trim(CStr(sh.Cells(3, col).Value)) + " = '" + str + "'"
cnt = cnt + 1
End If
If col <> 1 Then
strSQL = strSQL + ", "
End If
If (InStr(Trim(CStr(sh.Cells(4, col).Value)), "Integer") = 0) And (InStr(Trim(CStr(sh.Cells(4, col).Value)), "Decimal") = 0) And ((InStr(Trim(CStr(sh.Cells(4, col).Value)), "DATE") = 0) Or _
((Len(str) > 0) And (InStr(Trim(CStr(sh.Cells(4, col).Value)), "DATE") > 0))) Then
If (Len(str) <= 0) And (InStr(Trim(CStr(sh.Cells(5, col).Value)), "No") = 0) Then
str = "NULL"
ElseIf InStr(Trim(CStr(sh.Cells(4, col).Value)), "DATE") > 0 Then
str = "to_date('" + str + "','yyyy-mm-dd hh24:mi:ss')"
Else
str = "'" + str + "'"
End If
strSQL = strSQL + str
ElseIf (Len(str) <= 0) And (InStr(Trim(CStr(sh.Cells(4, col).Value)), "DATE") > 0) Then
strSQL = strSQL + "NULL"
Else
If (Len(str) <= 0) And (InStr(Trim(CStr(sh.Cells(5, col).Value)), "No") = 0) Then
str = "NULL"
End If
strSQL = strSQL + str
End If
col = col + 1
Loop
strDelSQL = strDelSQL + ";" + vbCrLf
ADO_Stream.WriteText strDelSQL
strSQL = strSQL + ");" + vbCrLf
ADO_Stream.WriteText strSQL
row = row + 1
Loop
End If
i = i + 1
rowcounts = 0
filecount = 0
Next sh
ADO_Stream.SaveToFile ThisWorkbook.Path & "\MstSQL(delete by condition).txt", 2
ADO_Stream.Close
Set ADO_Stream = Nothing
End Sub
點擊「保存」宏腳本,主要的一步完成。
三: 在excel文件(test.xlsx)的首頁創建兩個圖標,分別選擇右鍵指定宏,一個指定上面的summary(),一個指定上面的SQL()。然後就可以在後續的sheet頁創建自己需要生成SQL腳本的表結構名稱了,記得每個sheet頁面對應一個表結構及數據,使用方法如下:
A,將需要更新的對象表數據整個sheet拷進工具中,數據只保留需要更新的數據。
B,在第2行標出主鍵欄位,填上「PK」即可。
看看代碼
http://www.51testing.com/html/41/195041-831346.html
❸ Excel 導入 SQL server 數據類型問題
不用修改,可以把VARCHAR(255)類型數據導入到VARCHAR(10)欄位裡面。
❹ 將SQL查詢結果導出到EXCEL文件,超過65536行,怎麼處理
Excel2003之前的最大65536,
後來的版本最大行都要更多,安裝新版本的Office試試,如2007、2010 等
❺ 2010版excel自定義排序最多255字元,我要排序的內容較長,如何解決 謝謝!
自定義序列,也是針對自己有共性的、經常使用到的一些關鍵詞進行定義。因此自定義部分往往都比較簡單,往往幾十個字元內都可以解決問題。限定255個字元,應該是不浪費資源吧。
如果需要根據已有的列中的數據來進行另一列的排序,而此時列中的數據很多,就不能用此方法,可以用輔助列加MATCH函數來生成新的序列號,然後再根據常規方法對序列號進行排序。
如上圖中,需要將C列按A列已有名單排序,可在D2輸入公式:=MATCH(C2,$A$2:$A$4,0),並下拉填充,生成序號,然後以此序列為主關鍵字進行排序即可。