日期:2014-05-20  浏览次数:20499 次

控件GRIDVIEW 如何读取数据源为EXCEL中数据?
通过程序怎么建立数据源是EXCEL,以及怎么读取EXCEL数据?
感谢啊

------解决方案--------------------
用oledb查询excel得到datatable; select * from [sheet1$]
------解决方案--------------------
Try
Dim MyOleDbCn As New System.Data.OleDb.OleDbConnection

Dim FileName As String = " "
Dim OpenFileDialog As New OpenFileDialog
OpenFileDialog.Title = "打開 "
OpenFileDialog.Filter = ".xls|*.xls "
OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments

If (OpenFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
FileName = OpenFileDialog.FileName
End If
If FileName = " " Then
Exit Sub
End If
'If Dir(FileName) <> " " Then
' Kill(FileName)
'End If
MyOleDbCn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0; " & _
"Data Source= " & FileName & "; " & _
"Extended ProPerties= " "Excel 8.0;HDR=Yes;IMEX=1 " " "
MyOleDbCn.Close()
MyOleDbCn.Open()
Dim ComStr As String
ComStr = "SELECT * FROM [Sheet 1$] "
Dim adapter As New OleDb.OleDbDataAdapter(ComStr, MyOleDbCn)
dt.Clear()
adapter.Fill(dt)
dt.AcceptChanges()
Me.DataGridView1.DataSource = dt
MyOleDbCn.Close()
Catch ex As Exception
MsgBox(Err.Description, MsgBoxStyle.Critical, "System Error! ")
End Try
------解决方案--------------------
//环境:
//vs.net2005 asp.net2.0
//Default.aspx文件

<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Default.aspx.cs " Inherits= "Test_Default " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:GridView ID= "GridView1 " runat= "server " CellPadding= "4 " ForeColor= "#333333 " GridLines= "None ">
<FooterStyle BackColor= "#507CD1 " Font-Bold= "True " ForeColor= "White " />
<RowStyle BackColor= "#EFF3FB " />
<EditRowStyle BackColor= "#2461BF " />
<SelectedRowStyle BackColor= "#D1DDF1 " Font-Bold= "True " ForeColor= "#333333 " />
<PagerStyle BackColor= "#2461BF " ForeColor= "White " HorizontalAlign= "Center " />
<HeaderStyle BackColor= "#507CD1 " Font-Bold= "True " ForeColor= "White " />
<AlternatingRowStyle BackColor= "White " />
</asp:GridView>

</div>
</form>
</body>
</html>