資源簡介
ODBC 引擎,C#讀取Excel,Excel2000,2003,2007等

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Data.OleDb;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
namespace?ReadExcel07
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?void?btnPopulate_Click(object?sender?EventArgs?e)
????????{
????????????//?You?can?change?C:\Members.xlsx?to?any?path?where?
????????????//?the?file?is?located.
????????????string?connectionString?=?@“Provider=Microsoft.ACE.OLEDB.12.0;
???????????????Data?Source=C:\Members.xlsx;Extended?Properties=““Excel?12.0;HDR=YES;“““;
????????????//?if?you?don‘t?want?to?show?the?header?row?(first?row)
????????????//?use?‘HDR=NO‘?in?the?string
????????????string?strSQL?=?“SELECT?*?FROM?[Sheet1$]“;
????????????OleDbConnection?excelConnection?=?new?OleDbConnection(connectionString);
????????????excelConnection.Open();?//?This?code?will?open?excel?file.
????????????OleDbCommand?dbCommand?=?new?OleDbCommand(strSQL?excelConnection);
????????????OleDbDataAdapter?dataAdapter?=?new?OleDbDataAdapter(dbCommand);
????????????//?create?data?table
????????????DataTable?dTable?=?new?DataTable();
????????????dataAdapter.Fill(dTable);
????????????//?bind?the?datasource
????????????dataBingingSrc.DataSource?=?dTable;
????????????//?assign?the?dataBindingSrc?to?the?DataGridView
????????????dgvExcelList.DataSource?=?dataBingingSrc;
????????????//?dispose?used?objects
????????????dTable.Dispose();
????????????dataAdapter.Dispose();
????????????dbCommand.Dispose();
????????????excelConnection.Close();
????????????excelConnection.Dispose();
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????1767??2007-11-28?17:52??Form1.cs
- 上一篇:C# 松下PLC通訊協議標準模塊
- 下一篇:超市管理系統C#+SQL
評論
共有 條評論