ASP.NET Tutorial/ADO.net Database/Excel — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:56, 26 мая 2010
Create connection to Excel data source (C#)
<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
void Page_Load(object sender, EventArgs e) {
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\spreadsheet.xls; Extended Properties=""Excel 8.0;HDR=Yes"";";
string CommandText = "select * from [Book$]";
OleDbConnection myConnection = new OleDbConnection(ConnectionString);
OleDbCommand myCommand = new OleDbCommand(CommandText, myConnection);
myConnection.Open();
DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
DataGrid1.DataBind();
myConnection.Close();
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:datagrid id="DataGrid1" runat="server" EnableViewState="False" ForeColor="Black" BackColor="White" CellPadding="3" GridLines="None" CellSpacing="1">
<HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
<ItemStyle backcolor="#DEDFDE"></ItemStyle>
</asp:datagrid>
</form>
</body>
</html>
Use OleDbCommand to retrieve data from Excel data source
<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
void Page_Load(object sender, EventArgs e) {
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\spreadsheet.xls; Extended Properties=""Excel 8.0;HDR=Yes"";";
string CommandText = "select * from [Book$]";
OleDbConnection myConnection = new OleDbConnection(ConnectionString);
OleDbCommand myCommand = new OleDbCommand(CommandText, myConnection);
myConnection.Open();
DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
DataGrid1.DataBind();
myConnection.Close();
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:datagrid id="DataGrid1" runat="server" EnableViewState="False" ForeColor="Black" BackColor="White" CellPadding="3" GridLines="None" CellSpacing="1">
<HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
<ItemStyle backcolor="#DEDFDE"></ItemStyle>
</asp:datagrid>
</form>
</body>
</html>