SoFunction
Updated on 2025-03-01

C# uses method to read Excel tables

This article describes the method of using C# to read Excel tables. Share it for your reference. The specific analysis is as follows:

Microsoft NET provides an interactive method by using programs with Microsoft Office. You can use built-in OLEDB to access Excel's XLS tables. The following example demonstrates how to programmatically read an Excel worksheet in C#. Requires a reference library

using System;
using ;
namespace ConsoleApplication1
{
 class Program
 {
 static void Main()
 {
  string connString = "Provider=.4.0; data source=c:\\; Extended Properties=Excel 8.0;";
  // Select using a Worksheet name
  string selectqry = "SELECT * FROM [Sheet1$]";
  OleDbConnection conn = new OleDbConnection(connString);
  OleDbCommand cmd = new OleDbCommand(selectqry,con);
  try
  {
  ();
  OleDbDataReader theDatardr = ();
  while (())
  {
   ("{0}:{1}({2})–{3}({4})",(0),(1),(2),(3),(4));
  }
  }
  catch (Exception ex)
  {
  ();
  }
  finally
  {
  ();
  }
 }
 }
}

I hope this article will be helpful to everyone's C# programming.