Read Excel files from ASP.NET

Read Excel files from ASP.NET: This page provides a simple example of how to query an Excel spreadsheetfrom an ASP.NET page using either C# or VB.NET. Check it out!This code was written in response to a message posted on one ofCharles Carroll''s ASP.NET lists. You can ... This page provides a simple example of how to query an Excel spreadsheet from an ASP.NET page using either C# or VB.NET. Check it out!
<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System" %>

<script language="C#" runat="server">
    protected void Page_Load(Object Src, EventArgs E)
    {
        string strConn;
        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
        "Data Source=C:\\exceltest.xls;" +
        "Extended Properties=Excel 8.0;";
        //You must use the $ after the object you reference in the spreadsheet
        OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);

        DataSet myDataSet = new DataSet();
        myCommand.Fill(myDataSet, "ExcelInfo");
        DataGrid1.DataSource = myDataSet.Tables["ExcelInfo"].DefaultView;
        DataGrid1.DataBind();
    }
</script>

<html>
<head>
</head>
<body>
    <p>
        <asp:Label ID="Label1" runat="server">SpreadSheetContents:</asp:Label></p>
    <asp:DataGrid ID="DataGrid1" runat="server" />\
</body>
</html>
Reactions

Post a Comment

0 Comments

Close Menu