British Inside

An Englishman living in small town America

ASP/ASP.NET Web Hosting: 3 Months FREE + FREE Setup, CLICK HERE!

DiscountASP.NET now offers Windows 2008/IIS7 Web Hosting: CLICK HERE for Info!
  • ASP.NET 2.0 Hosting
  • ASP.NET AJAX Support
  • LINQ & Silverlight Compatible
  • SQL 2005/2000
  • CLICK HERE!

  • Reading XLS spreadsheets from ASP.NET

    Here's a tip for when you need to read a spreadsheet from ASP.NET (well, .NET really). Thinking about it now I'm surprised that I haven't needed to do this before.

    The second or third article I found was John Kilgo's Reading Excel (.xls) Files with ADO.NET... and I admit I was sceptical when I read the method outlined there..but it works a treat. File this under ".NET voodoo"..

    Simply put, you add a row of columns names and create a named range around that row plus the data that you want to import. Then you import it with this code...

    string connstr = @"Provider=Microsoft.Jet.OleDb.4.0;data source=c:\my.xls;Extended Properties=Excel 8.0;";
    string sql = "SELECT TOP 10 * FROM namedrangename"
    // or whatever you called the named range
    OleDbConnection conn = new OleDbConnection(connstr);
    OleDbCommand cmd = new OleDbCommand
    (sql, conn);
    conn.Open();
    something.DataSource = cmd.ExecuteReader();
    something.DataBind();

    Not much is it? But I think it's a pretty cool trick. Thanks John!

    ASP.NET 3.5 Web Hosting: 3 Months FREE + FREE Setup - CLICK HERE!

    Posted: Friday, August 31, 2007 7:01 PM by James

    Comments

    No Comments

    New Comments to this post are disabled