控件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>
//Default.aspx.cs 文件
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Test_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string filename1 = "entID.xls ";
OleDbConnection DBConnection = new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source= " + Server.MapPath( "~/App_Data/ "+filename1+ " ") + "; " + "Extended Properties=\ "Excel 8.0;HDR=Yes\ " ");
DBConnection.Open();
string SQLString = "SELECT * FROM [Sheet1$] ";
OleDbCommand DBCommand = new OleDbCommand(SQLString, DBConnection);
IDataReader DBReader = DBCommand.ExecuteReader();
GridView1.DataSource = DBReader;
GridView1.DataBind();
DBReader.Close();
DBConnection.Close();
}
}