可以解决的:我这里是.NET代码,可以改其他代码,差不多的。。
sql导入excel函数:
Public Function XBWexcel(ByVal xbw2 As String) As Integer
Dim myDataSet As New DataSet
Try
Dim myConn As New SqlConnection(strconn)
myConn.Open()
Dim strCom As String = xbw2 ’导出时的sql查询语句
Dim myCommand As New SqlDataAdapter(strCom, myConn)
myCommand.Fill(myDataSet, "xbwtable")
myConn.Close()
Catch ey As Exception
MessageBox.Show("连接错误! " + ey.ToString(), "错误")
End Try
Dim table As DataTable = myDataSet.Tables("xbwtable")
Dim AppExcel As Excel.Application = New Excel.Application '创建一个空的Excel电子表格文档
AppExcel.Workbooks.Add(True)
Dim colIndex As Integer = 0 '读取数据的字段名称,并在产生的Excel表格的第一行显示出来
Dim col As DataColumn = New DataColumn
For Each col In table.Columns
colIndex = colIndex + 1
AppExcel.Cells(1, colIndex) = col.ColumnName
Next
Dim rowIndex As Integer = 1
Dim row As DataRow
For Each row In table.Rows
rowIndex = rowIndex + 1
colIndex = 0
Dim col1 As DataColumn
For Each col1 In table.Columns
colIndex = colIndex + 1
AppExcel.Cells(rowIndex, colIndex) = row(col1.ColumnName).ToString()
Next
Next
AppExcel.Visible = True
End Function
执行上面函数时指定xbw2的参数,比如 xbw2 as straing="select * from [表] where [条件1]。。。" 然后执行XBWexcel(xbw2)就可以了。
下面是把excel导入sql数据库:
Dim OpenFileDialog As New OpenFileDialog
OpenFileDialog.InitialDirectory = My.Application.Info.DirectoryPath()
OpenFileDialog.Filter = "EXCEL文件(*.xls)|*.xls"
If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
Dim FileName As String = OpenFileDialog.FileName
Dim xbwmsg As String
xbwmsg = MsgBox("文件:'" & FileName & "'你是否确定导入!?", 1)
If xbwmsg <> vbOK Then Exit Sub
Dim MstrConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & FileName & "';Extended Properties=Excel 8.0;HDR=YES; IMEX=1"
Dim myDataset As New DataSet
Dim da As New OleDbDataAdapter("select * from [$偏差报告]", MstrConn)
Me.Cursor = Cursors.WaitCursor
PictureBox1.Visible = True
Label21.Visible = True
Label21.Text = "数据读取,请稍候……10%"
Me.Enabled = False
Try
da.Fill(myDataset)
DataGrid1.DataSource = myDataset.Tables.Item(0).DefaultView
Catch ex As Exception
MsgBox(ex.Message.ToString)
Exit Sub
End Try
Label21.Text = "数据读取,请稍候……100%"
到这里已经差不多了,相信你可以把刚才导入新建的数据库表格自己导入sql数据库了。这样做的好处是灵活机动,可以修改导出的数据库文件,也可以增加数据库文件。。。。。。自己去体会吧。。。
我想不可能吧,可能的话,也之用到xml了
c#有专门支持读取execel的类,也有支持读取数据库的类。用dataset