首先在項(xiàng)目的VB.NET界面,使用菜單【項(xiàng)目】--【添加引用】--【COM】
創(chuàng)新互聯(lián)建站致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營(yíng)銷,包括網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、SEO優(yōu)化、網(wǎng)絡(luò)推廣、整站優(yōu)化營(yíng)銷策劃推廣、電子商務(wù)、移動(dòng)互聯(lián)網(wǎng)營(yíng)銷等。創(chuàng)新互聯(lián)建站為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制及解決方案,創(chuàng)新互聯(lián)建站核心團(tuán)隊(duì)10年專注互聯(lián)網(wǎng)開(kāi)發(fā),積累了豐富的網(wǎng)站經(jīng)驗(yàn),為廣大企業(yè)客戶提供一站式企業(yè)網(wǎng)站建設(shè)服務(wù),在網(wǎng)站建設(shè)行業(yè)內(nèi)樹(shù)立了良好口碑。
選擇 ?Microsoft ADO Ext. 2.x for DDL and Security
然后單擊【確定】,完成引用。
完整代碼如下:
Imports?ADOX
Public?Class?Form1
Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click
'創(chuàng)建空的access數(shù)據(jù)庫(kù)文件--數(shù)據(jù)庫(kù)文件.mdb,密碼為123
Dim?Mycat?As?Catalog?=?New?Catalog()
Mycat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Jet?OLEDB:Engine?Type=5;Data?Source=?數(shù)據(jù)庫(kù)文件.mdb;Jet?OLEDB:Database?Password=123")
'以下代碼創(chuàng)建一個(gè)名為“實(shí)驗(yàn)數(shù)據(jù)表”
Dim?MyTable?As?ADOX.Table?=?New?ADOX.Table?????????'定義新表
MyTable.Name?=?"實(shí)驗(yàn)數(shù)據(jù)表"????'表命名
'給表“實(shí)驗(yàn)數(shù)據(jù)表”?創(chuàng)建一個(gè)字符串字段,字段名“姓名”
MyTable.Columns.Append("姓名",?,?ADOX.DataTypeEnum.adWChar)
'給表“實(shí)驗(yàn)數(shù)據(jù)表”?創(chuàng)建一個(gè)整數(shù)字段,字段名“學(xué)號(hào)”
MyTable.Columns.Append("學(xué)號(hào)",?ADOX.DataTypeEnum.adInteger)????'追加一個(gè)數(shù)字型字段
'給字段“學(xué)號(hào)”創(chuàng)建一個(gè)主鍵“PimaryKey_Field”????????
MyTable.Keys.Append("學(xué)號(hào)",?ADOX.KeyTypeEnum.adKeyPrimary,?"學(xué)號(hào)")
Mycat.Tables.Append(MyTable)?'把所有的新字段追加到表
MyTable?=?Nothing
Mycat?=?Nothing
End?Sub
End?Class
接收數(shù)據(jù)時(shí),使用一個(gè) Label 顯示就行了。而且跳過(guò)一些數(shù)據(jù)再顯示,例如一秒鐘才顯示一個(gè)數(shù)據(jù)。
你應(yīng)該把精力放到領(lǐng)導(dǎo)(或者用戶)的意圖上,不要糾結(jié)技術(shù)上的 ListBox。
Dim TestItem() As String = New String() {"Pickle", "Pineapple", "Papaya"} '創(chuàng)建數(shù)組 并賦值
Dim TestShoppingList As String = Join(TestItem, " = ") 'Join 就是字符聯(lián)接函數(shù) 后面的 =號(hào) 可選 為空值就把所有的字符串在一起 本例用=號(hào) 就是把 所有數(shù)組的值用 =號(hào)聯(lián)接起來(lái)
MsgBox(TestShoppingList) '顯示結(jié)果值
'這例子 就一個(gè)函數(shù)可以解決 就不發(fā)圖片了
希望能幫到你 你慢慢研究吧 有問(wèn)題可以找我
看到的 愛(ài)好者 多頂我一下 謝謝呵呵
剛好寫(xiě)了個(gè)Helper類,你試驗(yàn)一下DataTable2Exce(這個(gè)方法代碼如下:
Imports System.IO
Imports System.Data
Imports System.Data.OleDb
Public MustInherit Class ExcelHelper
Private Shared Function buildConnStr(excelFilePath As String) As String
Dim excelFileInfo As New System.IO.FileInfo(excelFilePath)
Dim constr As String
If excelFileInfo.Extension = ".xlsx" Then
constr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1'", excelFilePath)
Else
constr = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'", excelFilePath)
End If
Return constr
End Function
'將datatable導(dǎo)入到excel
Public Shared Function DataTable2Excel(dt As DataTable, excelFilePath As String) As Boolean
If File.Exists(excelFilePath) Then
Throw New Exception("該文件已經(jīng)存在!")
End If
If dt.TableName.Trim.Length = 0 Or dt.TableName.ToLower = "table" Then
dt.TableName = "Sheet1"
End If
Dim colCount As Integer = dt.Columns.Count
Dim pa(colCount - 1) As OleDb.OleDbParameter
Dim tableStructStr As String = "Create Table " dt.TableName "("
Dim connString As String = buildConnStr(excelFilePath)
Dim objconn As New OleDbConnection(connString)
Dim objcmd As New OleDbCommand
objcmd.Connection = objconn
Dim dataTypeList As New ArrayList
dataTypeList.Add("System.Decimal")
dataTypeList.Add("System.Double")
dataTypeList.Add("System.Int16")
dataTypeList.Add("System.Int32")
dataTypeList.Add("System.Int64")
dataTypeList.Add("System.Single")
Dim i As Integer = 0
For Each col As DataColumn In dt.Columns
If dataTypeList.IndexOf(col.GetType.ToString) 0 Then
pa(i) = New OleDbParameter("@" col.ColumnName, OleDbType.Double)
objcmd.Parameters.Add(pa(i))
If i + 1 = colCount Then
tableStructStr += col.ColumnName + " double)"
Else
tableStructStr += col.ColumnName + " double,"
End If
Else
pa(i) = New OleDbParameter("@" col.ColumnName, OleDbType.VarChar)
objcmd.Parameters.Add(pa(i))
If i + 1 = colCount Then
tableStructStr += col.ColumnName + " VarChar)"
Else
tableStructStr += col.ColumnName + " VarChar,"
End If
End If
i += 1
Next
Try
objcmd.CommandText = tableStructStr
If objconn.State = ConnectionState.Closed Then objconn.Open()
objcmd.ExecuteNonQuery()
Catch ex As Exception
Throw ex
End Try
Dim InsertSql_1 As String = "Insert into " + dt.TableName + " ("
Dim InsertSql_2 As String = " Values ("
Dim InsertSql As String = ""
For colID As Integer = 0 To colCount - 1 Step 1
If colID + 1 = colCount Then
InsertSql_1 += dt.Columns(colID).ColumnName ")"
InsertSql_2 += "@" + dt.Columns(colID).ColumnName + ")"
Else
InsertSql_1 += dt.Columns(colID).ColumnName + ","
InsertSql_2 += "@" + dt.Columns(colID).ColumnName + ","
End If
Next
InsertSql = InsertSql_1 + InsertSql_2
For rowID As Integer = 0 To dt.Rows.Count - 1 Step 1
For colID = 0 To dt.Columns.Count - 1
If pa(colID).DbType = DbType.Double And dt.Rows(rowID)(colID).ToString.Trim = "" Then
pa(colID).Value = 0
Else
pa(colID).Value = dt.Rows(rowID)(colID).ToString.Trim
End If
Next
Try
objcmd.CommandText = InsertSql
objcmd.ExecuteNonQuery()
Catch ex As Exception
Throw ex
End Try
Next
Try
If objconn.State = ConnectionState.Open Then objconn.Close()
Catch exp As Exception
Throw exp
End Try
Return True
End Function
' 獲取Excel文件數(shù)據(jù)表列表Sheets
Public Shared Function GetExcelTables(ExcelFileName As String) As ArrayList
'Dim sheets As New List(Of String)
'conn.Open()
'Dim dt As DataTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
'For Each r In dt.Rows
' sheets.Add(r("TABLE_NAME"))
'Next
'conn.Close()
'Return sheets
Dim dt As DataTable
If Not File.Exists(ExcelFileName) Then
Throw New Exception("指定的Excel文件不存在")
Return Nothing
End If
Dim tableList As New ArrayList
Using conn As OleDbConnection = New OleDbConnection(buildConnStr(ExcelFileName))
Try
conn.Open()
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
Catch ex As Exception
Throw ex
End Try
For i As Integer = 0 To dt.Rows.Count - 1
Dim tableName As String = dt.Rows(i)(2).ToString.Trim.TrimEnd("$")
If tableList.IndexOf(tableName) 0 Then tableList.Add(tableName)
Next
End Using
Return tableList
End Function
'將Excel文件導(dǎo)出至DataTable(第一行作為表頭)
Public Shared Function InputFromExcel(ExcelFileName As String, TableName As String) As DataTable
If Not File.Exists(ExcelFileName) Then
Throw New Exception("指定的Excel文件不存在")
End If
Dim tableList As ArrayList = GetExcelTables(ExcelFileName)
If tableList.IndexOf(TableName) 0 Then
TableName = tableList(0).ToString.Trim
End If
Dim dt As New DataTable
Dim conn As New OleDbConnection(buildConnStr(ExcelFileName))
Dim cmd As New OleDbCommand("select * from [" TableName "$]", conn) '調(diào)試是否需要$
Dim adapter As New OleDbDataAdapter(cmd)
Try
If conn.State = ConnectionState.Closed Then conn.Open()
adapter.Fill(dt)
Catch ex As Exception
Throw ex
Finally
If conn.State = ConnectionState.Open Then conn.Close()
End Try
Return dt
End Function
'查詢excel文件中的一個(gè)數(shù)據(jù)
Public Shared Function ReadOneDataFromExcel(ExcelFileName As String, TableName As String, sql As String) As Object
If Not File.Exists(ExcelFileName) Then
Throw New Exception("指定的Excel文件不存在")
End If
Dim tableList As ArrayList = GetExcelTables(ExcelFileName)
If tableList.IndexOf(TableName) 0 Then
TableName = tableList(0).ToString.Trim
End If
Dim dt As New DataTable
Dim conn As New OleDbConnection(buildConnStr(ExcelFileName))
Dim cmd As New OleDbCommand(sql, conn) '調(diào)試是否需要$
Dim ret As Object
Try
If conn.State = ConnectionState.Closed Then conn.Open()
ret = cmd.ExecuteScalar()
Catch ex As Exception
Throw ex
Finally
If conn.State = ConnectionState.Open Then conn.Close()
End Try
Return ret
End Function
'獲取Excel文件指定數(shù)據(jù)表的數(shù)據(jù)列表columnNames
Public Shared Function GetExcelTableColumns(ExcelFileName As String, TableName As String) As ArrayList
Dim dt As DataTable
If Not File.Exists(ExcelFileName) Then
Throw New Exception("指定的Excel文件不存在")
Return Nothing
End If
Dim ColList As New ArrayList
Using conn As OleDbConnection = New OleDbConnection(buildConnStr(ExcelFileName))
Try
conn.Open()
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, New Object() {Nothing, Nothing, TableName, Nothing})
Catch ex As Exception
Throw ex
End Try
For i As Integer = 0 To dt.Rows.Count - 1
Dim ColName = dt.Rows(i)("Column_Name").ToString().Trim()
ColList.Add(ColName)
Next
End Using
Return ColList
End Function
End Class
list1.additem j 意思就是把j添加至list1列表中,這是在一個(gè)for 循環(huán)中,循環(huán)了10次,添加了10次 j,即 list1 中 有10列分別為:1,3,6,10,15,21,28,36,45,55
下面又來(lái)了一個(gè)循環(huán), list1.removeitem 刪除列,刪除的是 list1.listcount - i ,list1.listcount 就是總列數(shù)為10,減去 i ,i 是1到4,即10-1,9-2,8-3,7-4 每減掉一次,list1.listcount 就少了一條記錄。
(注意,list1.listcount-1 是最后一條記錄)
減掉了第 9,7,5,3列
剩余為 1,3,6,15,28,45
結(jié)果為:3,15,28
'ListCount返回列表框中的項(xiàng)目總數(shù)
'ListCount-1是列表框中最后一個(gè)項(xiàng)目的索引號(hào)
'虧你提醒了,我寫(xiě)錯(cuò)了,上面修改了,第一個(gè)索引為0,最后一個(gè)為9.
'也就是,問(wèn)題所問(wèn)的第二列的索引為1,第四列的索引為3...
你可以在SQL語(yǔ)句里面把列名變?yōu)橹形拿?再放到DATAGRID里面啊
如 SELECT 字段1 AS 某某1 ,字段2 AS 某某2 FROM 某表
你的串號(hào)我已經(jīng)記下,采納后我會(huì)幫你制作