Read Excel files from ASP.NET

5:03:00 am 0 Comments


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>

0 comments:

How to add Header and Footer on every page by using HttpModule in asp.net

4:55:00 am 0 Comments


In this post,I will show you a very simple way to add header and footer on every page using HttpModule.

  • Open visual studio and create a new website.
  • Right click on website and add a new class named HeaderAndFooter,and inherit this class with IHttpModule inteface.
using System;
using System.Web;
using System.IO;

public class HeaderAndFooter : IHttpModule
{
    const string PageHeaderText = "<h1>Header Added by Module<h1>";
    const string PageFooterText = "<h1>Footer added by Module</h1>";

    public void Init(HttpApplication app)
    {
        // Register for pipeline events
        app.BeginRequest += new EventHandler(OnBeginRequest);
        app.EndRequest += new EventHandler(OnEndRequest);
    }

    public void Dispose()
    {
        // Nothing to do here
    }

    public void OnBeginRequest(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        HttpContext ctx = app.Context;

        // Add custom header to the HTTP response
        ctx.Response.AppendHeader("Author", "DinoE");
        ctx.Response.Write(PageHeaderText);
    }

    public void OnEndRequest(object sender, EventArgs e)
    {
        // Get access to the HTTP context 
        HttpApplication app = (HttpApplication)sender;
        HttpContext ctx = app.Context;

        // Append some custom text
        ctx.Response.Write(PageFooterText);
    }
}
  • Open the web.config and register the module as below
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <httpModules>
      <add name="HeaderAndFooter" type="HeaderAndFooter,App_Code"/>
    </httpModules>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
</configuration>

0 comments: