您可以使用 FileUpload 控件将文件上传到您的服务器,然后可以在每次需要时检索该文件。
从那里您可以使用 OleDbConnection 读取 excel 文件,将每个获取的记录添加到列表中以逐个迭代,然后将它们插入到您的数据库中。
这是一个使用三个类的小示例:CConexion(处理 OleDbConnection 进程)、ExcelParser(读取获取的记录并将其插入到您的数据库中,以便您可以在需要时对其进行查询)和 ExcelRecord,它表示每个注册表上的数据容器你的 Excel 文件。
当您的计算机上有 MS Office Excel 2010 时,会安装这些 OleDb 库。我已经测试了代码并且运行良好。
该 excel 文件在示例中称为 Hoja1.xlsx,并存储在 TestApp 根目录下名为 Files 的文件夹中,当您打开它时,会出现三个记录,每个记录有两个字段(A1,B1),(A2,B2) ,(A3,B3) 在文件的第一张也称为 Hoja1.xlsx
''CConexion Class:
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Data
Public Class CConexion
#Region "Private Variables"
Dim sOleDbConnectionString As String = String.Empty
Dim conexionOleDb As New OleDbConnection()
#End Region
#Region "Con_Ole"
Public Interface IConexionOleDb
Property retConexionOleDb() As OleDbConnection
Sub retOpenOleDb()
Sub retCloseOleDb()
End Interface
Public Property retConexionOleDb() As OleDbConnection
Get
Return conexionOleDb
End Get
Set(ByVal value As OleDbConnection)
End Set
End Property
Public Sub retOpenOleDb()
If Not conexionOleDb.State = System.Data.ConnectionState.Open Then
conexionOleDb.Open()
End If
End Sub
Public Sub retCloseOleDb()
If Not conexionOleDb.State = ConnectionState.Closed Then
conexionOleDb.Close()
End If
End Sub
#End Region
#Region "Constructors"
Public Sub New()
End Sub
Public Sub New(ByVal rutaOleDb As String)
sOleDbConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & rutaOleDb _
& ";" & "Extended Properties=Excel 12.0;"
conexionOleDb.ConnectionString = sOleDbConnectionString
End Sub
#End Region
End Class
''ExcelRecord Class:
Public Class ExcelRecord
Private _RId As Short = 0
Public Property RId() As Short
Get
Return _RId
End Get
Set(ByVal value As Short)
_RId = value
End Set
End Property
Private _RText As String = String.Empty
Public Property RText() As String
Get
Return _RText
End Get
Set(ByVal value As String)
_RText = value
End Set
End Property
Public Sub New()
End Sub
Public Sub New(ByVal Rid As Short, ByVal RText As String)
Me.RId = Rid
Me.RText = RText
End Sub
End Class
''ExcelParser Class:
Imports System.Data.OleDb
Imports System.Collections.Generic
Public Class ExcelParser
Private Function InsertRecords(ByVal objExcelRecords As List(Of ExcelRecord)) As Boolean
''Your code for insertion here
Return True
End Function
Public Function ReadExcel(ByVal filePath As String) As Short
Dim cn As New CConexion(filePath)
Dim dr As OleDbDataReader
Dim OperationState As Boolean = False
Dim objExcelRecords As New List(Of ExcelRecord)
Try
Dim cmd As New OleDbCommand("Select * from [Hoja1$]", cn.retConexionOleDb)
cn.retOpenOleDb()
dr = cmd.ExecuteReader
While dr.Read
Dim objExcelRecord As New ExcelRecord(CShort(dr(0)), CStr(dr(1)))
objExcelRecords.Add(objExcelRecord)
End While
OperationState = InsertRecords(objExcelRecords)
CType(dr, IDisposable).Dispose()
Catch ex As Exception
Finally
cn.retCloseOleDb()
cn.retConexionOleDb.Dispose()
End Try
Return OperationState
End Function
End Class
''Test Page ExcelReader.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim filePath As String = Server.MapPath("~\Files\Hoja1.xlsx")
Dim objExcelParser As New ExcelParser()
If objExcelParser.ReadExcel(filePath) Then
Response.Write("Read!")
Else
Response.Write("No Read!")
End If
End If
End Sub
让我知道这是否适合您。希望能帮助到你。