1
'''---------将上传文件存为EXCEL
2
Dim aa As New Excel.Application
3
Dim bb As String = lstrFileNamePath
4
Try
5
Dim cc As Excel.Workbook = aa.Workbooks.Open(lstrFileNamePath)
6
If lstrFileNamePath.EndsWith(".CSV") = True Then
7
bb = lstrFileNamePath.Replace(".CSV", ".XLS")
8
End If
9
cc.SaveAs(bb, Excel.XlFileFormat.xlExcel9795)
10
cc.Close()
11
Catch ex As Exception
12
Throw ex
13
Finally
14
aa.Quit()
15
End Try
16
17
'将上传的文件导入到对应数据库表中==采用OLEDB方式读取EXCEL文件
18
'Dim strConn As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & lstrFileFolder ' //filePath, For example: C:\"
19
'strConn += ";Extensions=asc,csv,tab,txt;HDR=Yes;FMT=Delimited;"
20
Dim strConn As String = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" & lstrFileNamePath & ";Extended Properties='text;HDR=Yes;'"
21
Dim objConn As OleDbConnection = New OleDbConnection(strConn)
22
'Dim objConn As OdbcConnection = New OdbcConnection(strConn)
23
Dim dsCSV As New DataSet
24
Try
25
Dim strSql As String = "select * from [1address$]" 'fileName, For example: 1.csv
26
'Dim odbcCSVDataAdapter As New OdbcDataAdapter(strSql, objConn)
27
Dim oleCSVDataAdapter As New OleDbDataAdapter(strSql, objConn)
28
oleCSVDataAdapter.Fill(dsCSV)
29
'odbcCSVDataAdapter.Fill(dsCSV)
30
Catch ex As Exception
31
Throw ex
32
'Finally
33
' objConn.Close()
34
End Try
35
36
37
'采用OLEDB方式直接读取CSV文件
38
Dim FilePath As String = lstrFileFolder
39
Dim conn1 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & ";Extended Properties=""text;HDR=no;FMT=Delimited""")
40
41
Dim command1 As New OleDbCommand("", conn1)
42
Dim dataadapter1 As New OleDbDataAdapter(command1)
43
Dim dt_data As New DataTable
44
command1.CommandText = "SELECT * FROM " & lstrFileName
45
dataadapter1.Fill(dt_data)
46
47
48
'=======采用IO.StreamReader直接读取CSV
49
Dim intColCount As Integer = 0
50
Dim blnFlag As Boolean = True
51
Dim mydt As New DataTable
52
53
Dim mydc As DataColumn
54
Dim mydr As DataRow
55
56
Dim strpath As String = lstrFileNamePath
57
Dim strline As String
58
Dim aryline() As String
59
60
Dim i As Integer
61
Dim rowNum As Integer
62
63
Dim mysr As System.IO.StreamReader = New System.IO.StreamReader(strpath, System.Text.Encoding.GetEncoding("GB2312"))
64
' dim aa as String = mysr.ReadBlock
65
strline = mysr.ReadLine()
66
rowNum = 1
67
While (strline <> Nothing)
68
aryline = Split(strline, ",")
69
If rowNum = 1 Then
70
If (blnFlag = True) Then
71
blnFlag = False
72
intColCount = aryline.Length
73
For i = 0 To aryline.Length - 1
74
mydc = New DataColumn(aryline(i))
75
mydt.Columns.Add(mydc)
76
Next
77
End If
78
Else
79
mydr = mydt.NewRow()
80
For i = 0 To intColCount - 1
81
mydr(i) = aryline(i)
82
Next
83
mydt.Rows.Add(mydr)
84
End If
85
strline = mysr.ReadLine()
86
rowNum = rowNum + 1
87
End While
'''---------将上传文件存为EXCEL2
Dim aa As New Excel.Application3
Dim bb As String = lstrFileNamePath4
Try5
Dim cc As Excel.Workbook = aa.Workbooks.Open(lstrFileNamePath)6
If lstrFileNamePath.EndsWith(".CSV") = True Then7
bb = lstrFileNamePath.Replace(".CSV", ".XLS")8
End If9
cc.SaveAs(bb, Excel.XlFileFormat.xlExcel9795)10
cc.Close()11
Catch ex As Exception12
Throw ex13
Finally14
aa.Quit()15
End Try16

17
'将上传的文件导入到对应数据库表中==采用OLEDB方式读取EXCEL文件18
'Dim strConn As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & lstrFileFolder ' //filePath, For example: C:\"19
'strConn += ";Extensions=asc,csv,tab,txt;HDR=Yes;FMT=Delimited;"20
Dim strConn As String = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" & lstrFileNamePath & ";Extended Properties='text;HDR=Yes;'"21
Dim objConn As OleDbConnection = New OleDbConnection(strConn)22
'Dim objConn As OdbcConnection = New OdbcConnection(strConn)23
Dim dsCSV As New DataSet24
Try25
Dim strSql As String = "select * from [1address$]" 'fileName, For example: 1.csv26
'Dim odbcCSVDataAdapter As New OdbcDataAdapter(strSql, objConn)27
Dim oleCSVDataAdapter As New OleDbDataAdapter(strSql, objConn)28
oleCSVDataAdapter.Fill(dsCSV)29
'odbcCSVDataAdapter.Fill(dsCSV)30
Catch ex As Exception31
Throw ex32
'Finally33
' objConn.Close()34
End Try35

36

37
'采用OLEDB方式直接读取CSV文件38
Dim FilePath As String = lstrFileFolder39
Dim conn1 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & ";Extended Properties=""text;HDR=no;FMT=Delimited""")40

41
Dim command1 As New OleDbCommand("", conn1)42
Dim dataadapter1 As New OleDbDataAdapter(command1)43
Dim dt_data As New DataTable44
command1.CommandText = "SELECT * FROM " & lstrFileName45
dataadapter1.Fill(dt_data)46

47

48
'=======采用IO.StreamReader直接读取CSV49
Dim intColCount As Integer = 050
Dim blnFlag As Boolean = True51
Dim mydt As New DataTable52

53
Dim mydc As DataColumn54
Dim mydr As DataRow55

56
Dim strpath As String = lstrFileNamePath57
Dim strline As String58
Dim aryline() As String59

60
Dim i As Integer61
Dim rowNum As Integer62

63
Dim mysr As System.IO.StreamReader = New System.IO.StreamReader(strpath, System.Text.Encoding.GetEncoding("GB2312"))64
' dim aa as String = mysr.ReadBlock65
strline = mysr.ReadLine()66
rowNum = 167
While (strline <> Nothing)68
aryline = Split(strline, ",")69
If rowNum = 1 Then70
If (blnFlag = True) Then71
blnFlag = False72
intColCount = aryline.Length73
For i = 0 To aryline.Length - 174
mydc = New DataColumn(aryline(i))75
mydt.Columns.Add(mydc)76
Next77
End If78
Else79
mydr = mydt.NewRow()80
For i = 0 To intColCount - 181
mydr(i) = aryline(i)82
Next83
mydt.Rows.Add(mydr)84
End If85
strline = mysr.ReadLine()86
rowNum = rowNum + 187
End While

浙公网安备 33010602011771号