ASP.Net/ADO.net Database/Excel

Материал из .Net Framework эксперт
Версия от 11:51, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Load data from 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=C:\Yourspreadsheet.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 style="FONT-FAMILY: arial">
    <h2>Simple Data Report 
    </h2>
    <hr size="1" />
    <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>

--%>



Reading from an Excel Workbook (VB.net)

<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.OleDb" %>
<html>
  <head>
    <title>Reading from an Excel Workbook</title>
  </head>
  <body>
    <H3>Reading from an Excel Workbook</H3>
    <asp:DataGrid id="dgInventory" runat="server" />
  </body>
</html>
<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
  Dim strConnection As String = "Provider=Microsoft.Jet.OleDb.4.0;" & _
                  "data source=C:\yourExcellFileName.xls;" & _
                  "Extended Properties=Excel 8.0;"
  Dim objConnection As New OleDbConnection(strConnection)
  Dim strSQL As String = "SELECT * FROM Items WHERE Source="Dell""
  Dim objCommand As New OleDbCommand(strSQL, objConnection)
  objConnection.Open()
  dgInventory.DataSource = objCommand.ExecuteReader()
  dgInventory.DataBind()
  objConnection.Close()
End Sub
</script>