1. 主動台模式
2. Execl 檔案 d:\test1.xls
3. 第一列為欄位名稱,請用文字(否則會被更改)
4. 程式碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace CSConsoleExcel
{
class Program
{
static void Main(string[] args)
{
OleDbDataAdapter cmd;
DataSet ds = new DataSet();
//String fileNameString = this.MapPath();
//fileNameString += "\\" + fileNameTextBox.Text;
OleDbConnection cn = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; data source=" + @"D:\test1.xls" + ";Extended Properties=Excel 8.0;");
// Select the data from Sheet1 of the workbook.
string SQL = "select * from [工作表1$]";
cmd = new OleDbDataAdapter(SQL, cn);
cn.Open();
cmd.Fill(ds, "test0");
cn.Close();
Console.WriteLine(ds.Tables[0].Columns[0].Caption);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Console.WriteLine(ds.Tables[0].Rows[i].ItemArray[0]);
}
}
}
}
沒有留言:
張貼留言