ASP.NET VB读取EXCEL表格

ASP.NET VB读取EXCEL表格

//定义要上传的文件名
Dim filename As String = xlsupload.FileName
//定义上传路径(项目根目录)
Dim savePath = Server.MapPath(“~/”) + filename
//上传文件
xlsupload.PostedFile.SaveAs(Trim(savePath))
//定义内存表
Dim xlstable = New DataTable
//定义连接字符串
Dim MyConnectionString As String = “Provider=Microsoft.ACE.OLEDB.12.0;Data Source=” + savePath + “;Extended Properties=’Excel 12.0 Xml;HDR=YES’;”
//读取过程
Using xlscon As New OleDb.OleDbConnection(MyConnectionString)
xlscon.Open()
//定义Sheet表名列表
Dim excelsheetlist As New DataTable
//取得Sheet表名
excelsheetlist = xlscon.GetSchema(“Tables”)
//将Sheet表名添加到DropListDown控件
SheetList.Items.Clear()
For i As Integer = 0 To excelsheetlist.Rows.Count – 1
‘ 获取sheet的名称
Dim sheetName As String = Replace(excelsheetlist.Rows(i).Item(“TABLE_NAME”), “$”, “”)
SheetList.Items.Add(sheetName)
Next
//查询指定Sheet表内的数据
dim xlssqlstr as String = “select * from [” + SheetList.Text + “$]”
Dim XlsAda As New OleDb.OleDbDataAdapter(xlssqlstr, xlscon)
Dim xlstable = New DataTable
XlsAda.Fill(xlstable)
xlshelp.DataSource = xlstable
xlshelp.DataBind()
End Using

滚动至顶部