C#應用Ado.net讀取Excel表的辦法。本站提示廣大學習愛好者:(C#應用Ado.net讀取Excel表的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#應用Ado.net讀取Excel表的辦法正文
本文實例講述了C#應用Ado.net讀取Excel表的辦法。分享給年夜家供年夜家參考。詳細剖析以下:
微軟NET供給了一個交互的辦法,經由過程應用ADO.NET與Microsoft Office法式。可使用內置的OLEDB來拜訪Excel的XLS表格。上面的例子演示了若何在C#編程讀取Excel任務表。須要援用System.Data.OleDb庫
using System; using System.Data.OleDb; namespace ConsoleApplication1 { class Program { static void Main() { string connString = "Provider=Microsoft.Jet.OleDb.4.0; data source=c:\\sample.xls; 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 { con.Open(); OleDbDataReader theDatardr = cmd.ExecuteReader(); while (theDatardr.Read()) { Console.WriteLine("{0}:{1}({2})–{3}({4})",theDatardr.GetString(0),theDatardr.GetString(1),theDatardr.GetString(2),theDatardr.GetString(3),theDatardr.GetString(4)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { con.Dispose(); } } } }
願望本文所述對年夜家的C#法式設計有所贊助。