Repository navigation
Expand file tree
/
Copy pathExcelToSql.fs
More file actions
executable file
·155 lines (134 loc) · 7.16 KB
/
Copy pathExcelToSql.fs
File metadata and controls
executable file
·155 lines (134 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// This can run as an fsx script; see ExcelToSQL.bat (no EXE required, but fsi.exe and FSharp.Core.dll must exist somewhere)
// Generate a SQL script from a worksheet of an Excel file
// by Aaron W. West, 5/31/2014
// Usage: excelToSQL databaseName schemaName tableName infile.xls outfile.sql
// Name a sheet of the workbook *[tableName]* or *data* or Sheet1,
// Make sure your ActiveRange has no blank columns (delete blank columns or rows if so),
// Make sure the formatting of the cells is appropriate for the datatype
// Make sure the first row contains field names for the table
// This uses:
// https://code.google.com/p/linqtoexcel/
// http://www.nuget.org/packages/LinqToExcel ( Install-Package LinqToExcel )
#if INTERACTIVE
#I "..\packages\LinqToExcel.1.8.0\lib" //assembly search path
#r "LinqToExcel.dll"
#r "Remotion.Data.Linq.dll"
#endif
open System
open LinqToExcel
open LinqToExcel.Query
open LinqToExcel.Extensions
open LinqToExcel.Attributes
open LinqToExcel.Domain
let excelToSql databaseName schemaName tableName fn outfn =
//let ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
//let dtfi: System.Globalization.DateTimeFormatInfo = ci.DateTimeFormat
let fullTableName = sprintf "[%s].[%s].[%s]" databaseName schemaName tableName
let excel = new ExcelQueryFactory(fn)
excel.DatabaseEngine <- DatabaseEngine.Ace
let isDataSheet n = let nm = n.ToString().ToLower() in
nm.Contains(tableName) || nm.Contains("data") || nm.Equals("sheet1")
let firstWorksheetName = excel.GetWorksheetNames() |> Seq.where(isDataSheet) |> Seq.take(1) |> Seq.exactlyOne
let q = query { for row in excel.Worksheet(firstWorksheetName) do
select row }
let colNames : string[] = excel.GetColumnNames (firstWorksheetName) |> Seq.toArray //<string>
let colNamesList = colNames |> Array.toList
// create INSERT
let insertStart = sprintf "INSERT %s (%s)\n" fullTableName (String.Join(",", colNames ))
// Terminate if we find an unknown type, so the programmer can fix the problem
// However, LinqToExcel is returning a very limited set of types, not all worksheet cell types
let formatCell (v:obj) =
match v with
| :? System.String -> sprintf "'%s'" <| v.ToString().Replace("'","''")
| :? System.DateTime -> sprintf "'%s'" <| v.ToString().Replace("'","''")
| :? System.Double -> sprintf "%s" <| v.ToString()
| :? System.DBNull -> "NULL"
// | :? System.Int32 -> sprintf "%s" <| v.ToString() // Spreadsheets don't have Int32
| _ -> raise(Exception("Error: Type=" + v.GetType().ToString()
+ "???: Unknown type in formatCell"))
let formatRow (r:Row) =
sprintf "%s VALUES(%s)" insertStart
<| String.Join(",", [| for cel in r.ToArray()
-> formatCell (cel.Value) |])
let inserts = q |> Seq.map( formatRow ) |> Seq.toArray
let lengthCell (v:obj) =
match v with
| :? System.String -> v.ToString().Length
| :? System.DateTime -> 8
| :? System.Double -> v.ToString().Length
| :? System.DBNull -> 0
| _ -> raise(Exception("Error: Type=" + v.GetType().ToString()
+ "???: Unknown type in lengthCell"))
// Convert a list of lists of column lengths into a list of maximum column lengths
// incomplete pattern-match if the lists are uneven
// (this assumes rows of equal-length lists of cells)
let rec maxLists (ill : list<list<int>>) =
match List.head ill with
| hd::tl -> List.Cons ( List.max <| List.map List.head ill,
maxLists (List.map List.tail ill) )
| [] -> []
let cellLengths (r:Row) = [ for cel in r.ToArray() -> lengthCell (cel.Value) ]
let qlens = q |> Seq.toList |> List.map( cellLengths )
let maxLengths = maxLists qlens
let maxLengthStrings = List.map (sprintf "%d") maxLengths
let zipWith op x y = List.map (fun (x,y) -> op x y) <| List.zip x y
let formatter colname len = sprintf "%s\t%s" colname len
let maxLengthsString = String.Join("\n", zipWith formatter colNamesList maxLengthStrings)
let prefix = String.Format("USE {0}\nGO\nSET IDENTITY_INSERT {1}.{2} ON\nGO\n",
databaseName, schemaName, tableName)
let output = sprintf "%s%s\n/* Maximum cell lengths:\n%s\n*/"
prefix (String.Join("\n",inserts)) maxLengthsString
// Warning, side-effects below (not so functional)
if outfn = "-" then
stdout.Write(output)
else
let outfile = System.IO.File.WriteAllText(outfn, output)
printfn "Opening %s ..." outfn
// you might want to remove this statement,
// but I think it's cool to open the resulting sql file in SSMS
// so it can be run or tested immediately
System.Diagnostics.Process.Start(outfn) |> ignore
// Replace extension, eg filename.xlsx to filename.sql.
// Arg ext should contain the dot
let replaceExt (fn:string) (ext:string) = fn.Split([|'.'|]).[0] + ext
let doMain argv =
match argv with
| [|databaseName; schemaName; tableName; fn; outfn|]
-> excelToSql databaseName schemaName tableName fn outfn
| [|databaseName; schemaName; tableName; fn|]
-> excelToSql databaseName schemaName tableName fn (replaceExt fn ".sql")
| [|"DEBUGTITAN"|]
-> excelToSql "IDODS_ODS" "dbo" "ODS_Trade"
@"C:\titan\data\IDODS_TradeLoad_ODS_Trades.xls"
@"C:\titan\data\IDODS_TradeLoad_ODS_Trades.sql"
| _ -> printfn "Usage: excelToSQL databaseName schemaName tableName infile.xls outfile.sql\n%s"
", or substitute - for outfile.sql for stdout"
exit(1)
0 // return an integer exit code
#if COMPILED
[<EntryPoint>]
let main argv =
doMain argv
#else
// Three ways to run the program
// Exe: simplest, actually. But it does require Fsharp.core.dll to be in the path, I think
// alternative active patterns syntax
//let (|FileExtension|) (filePath:string) = IO.Path.GetExtension(filePath).ToLower()
//let (|IsScriptName|) (filePath:string) = filePath.ToLower().EndsWith(".fsx")
//from http://www2.lib.uchicago.edu/keith/ocaml-class/utils.html
let rec dropwhile f = function
| [] -> []
| hd::tl when f hd -> dropwhile f tl
| list -> list
let dropWhileStrings = (dropwhile : (string -> bool) -> string list -> string list)
// GetCommandLineArgs, then drop *fsi.exe (optionally), then drop *.fsx
let args = dropWhileStrings
(fun s -> not ( s.ToLower().EndsWith(".fsx")) )
// with active patterns it could be
// (fun s -> match s with | FileExtension ".fsx" -> false | _ -> true)
(Array.toList( Environment.GetCommandLineArgs() ))
|> List.tail |> List.toArray
printfn "args=%A" args
doMain args
// or // excelToSql (args.[0]) (args.[1]) (args.[2]) (args.[3]) (args.[4])
#endif