將DBF,XLS,XML,MDB檔案匯入C#DataGrid的方法

來源:互聯網
上載者:User
datagrid|xml 以下的源碼裡分別給出了將DBF,XLS,XML,MDB檔案匯入C#DataGrid的方法,供各位參考。

//PutInDataSet.cs的源碼
using System;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Data;
using System.Collections;

namespace PutInDataSet
{
/// <summary>
/// DataSetTransIn 的摘要說明。
/// </summary>
public class PutInDataSet
{
/// <summary>
/// 傳入的檔案變數
/// </summary>
private DataSet my_Ds;//存放檔案的資料集
private string my_Err;//錯誤資訊
private string my_TableName;//傳入的檔案名稱
private TableType my_TableType;//傳入的檔案類型
private string my_TablePath;//傳入的檔案路徑
private int my_TableIndex;//表的索引
OleDbCommandBuilder my_Builder;//命令串

/// <summary>
/// 資料庫連接變數
/// </summary>
private string my_StrConnection;//連接字串
private string my_StrSelect;//select語句

/// <summary>
/// 可以處理的檔案類型
/// </summary>
public enum TableType
{
MDB,XLS,DBF,DOC,TXT,XML,HTML
}

public PutInDataSet(string TablePath,string TableName,TableType TableType)
{
///<summary>
///獲得傳入的路徑,檔案名稱及檔案類型;
///</summary>
this.my_TablePath=TablePath;//路徑
this.my_TableName=TableName;//檔案名稱
this.my_TableType=TableType;//檔案類型
}

public DataSet Convert()
{
DataSet iRtn_Ds=new DataSet();
switch (this.my_TableType)
{
case TableType.DBF:
iRtn_Ds = this.DbfToDs();
break;

case TableType.MDB:
iRtn_Ds = this.MdbToDs();
break;

case TableType.XLS:
iRtn_Ds = this.XlsToDs();
break;

case TableType.XML:
iRtn_Ds = this.XmlToDs();
break;
}
return iRtn_Ds;
}

///<summary>
///將DBF檔案放入DataSet
///</summary>
private DataSet DbfToDs()
{
//資料庫連接定義
OdbcConnection my_conn; //資料連線
OdbcDataAdapter my_Adapter;//資料配接器

//資料庫連接
this.my_StrConnection= "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + this.my_TablePath;
this.my_StrSelect="SELECT * FROM " + this.my_TableName;
my_conn = new OdbcConnection(this.my_StrConnection);
my_conn.Open();
my_Adapter = new OdbcDataAdapter(this.my_StrSelect,my_conn);
this.my_Ds=new DataSet();

//填充資料集
my_Adapter.Fill(this.my_Ds,this.my_TableName);
return this.my_Ds;
}

///<summary>
///將MDB檔案放入DataSet
///</summary>
private DataSet MdbToDs()
{
//資料庫連接定義
OleDbConnection my_conn;
OleDbDataAdapter my_Adapter;

//資料庫連接
this.my_StrConnection= "Provider=Microsoft.JET.OLEDB.4.0;data source=" + this.my_TablePath;
this.my_StrSelect="SELECT * FROM " + this.my_TableName;
my_conn = new OleDbConnection(this.my_StrConnection);
my_conn.Open();
my_Adapter = new OleDbDataAdapter(this.my_StrSelect,my_conn);
this.my_Ds=new DataSet();

//填充資料集
my_Adapter.Fill(this.my_Ds,this.my_TableName);
return this.my_Ds;
}

///<summary>
///將XML檔案放入DataSet
///</summary>
private DataSet XmlToDs()
{

//填充資料集
this.my_Ds=new DataSet();
this.my_Ds.ReadXml(this.my_TablePath+this.my_TableName,XmlReadMode.ReadSchema);
this.my_Ds.DataSetName="XmlData";
return this.my_Ds;
}

///<summary>
///將Excel檔案放入DataSet
///</summary>
private DataSet XlsToDs()
{
OleDbConnection my_conn;
OleDbDataAdapter my_Adapter;

//資料庫連接
this.my_StrConnection= "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties=Excel 8.0;data source="+this.my_TablePath+this.my_TableName;
this.my_StrSelect="SELECT * FROM [SHEET1$]";
my_conn = new OleDbConnection(this.my_StrConnection);
my_conn.Open();
my_Adapter = new OleDbDataAdapter(this.my_StrSelect,my_conn);
this.my_Builder=new OleDbCommandBuilder(my_Adapter);
this.my_Ds=new DataSet();

//填充資料集
my_Adapter.Fill(this.my_Ds,"ExcelData");
return this.my_Ds;
}


}
}


//Form_PutInDataSet.cs的源碼

using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using DataAccess.SysManage;
using BusinessRules;
using DataSetTrans;


namespace WinForm.Common
{
/// <summary>
/// FormDesktop 的摘要說明。
/// </summary>
public class FormDesktop : System.Windows.Forms.Form
{
private WinForm.Common.DeskTop deskTop1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的設計器變數。
/// </summary>
private System.ComponentModel.Container components = null;

private DataSet m_ds = new DataSet();
private System.Windows.Forms.DataGrid dataGrid1; //資料來源
private string m_TableName; //外部檔案名稱

public FormDesktop()
{
//
// Windows 表單設計器支援所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
//
}

/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.deskTop1 = new WinForm.Common.DeskTop();
this.button1 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// deskTop1
//
this.deskTop1.BackColor = System.Drawing.SystemColors.Desktop;
this.deskTop1.Location = new System.Drawing.Point(168, 440);
this.deskTop1.Name = "deskTop1";
this.deskTop1.Size = new System.Drawing.Size(16, 24);
this.deskTop1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(256, 216);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 40);
this.button1.TabIndex = 1;
this.button1.Text = "GetData";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(192, 40);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(216, 152);
this.dataGrid1.TabIndex = 2;
//
// FormDesktop
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.AppWorkspace;
this.ClientSize = new System.Drawing.Size(624, 485);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.button1);
this.Controls.Add(this.deskTop1);
this.Name = "FormDesktop";
this.Text = "系統控制台";
this.Resize += new System.EventHandler(this.FormDesktop_Resize);
this.Load += new System.EventHandler(this.FormDesktop_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

private void FormDesktop_Load(object sender, System.EventArgs e)
{

}

/// <summary>
/// 當視窗改變大小時自動置中。
/// </summary>
private void FormDesktop_Resize(object sender, System.EventArgs e)
{
if (this.WindowState != FormWindowState.Minimized)
{
if (this.Width > this.deskTop1.Width && this.Height > this.deskTop1.Height )
{
this.deskTop1.Left = (this.Width - this.deskTop1.Width) / 2;
this.deskTop1.Top = (this.Height- this.deskTop1.Height)/ 2;
}
}
}

private void button1_Click(object sender, System.EventArgs e)
{
DataSet out_Ds=new DataSet();
PutInDataSet obj=new PutInDataSet("檔案路徑","檔案名稱",PutInDataSet.TableType.檔案格式);//調用PutInDataSet類
out_Ds=obj.Convert();//轉換到DataSet中
this.dataGrid1.DataSource=out_Ds.Tables["表名"];//在DataGrid中顯示

}
}
}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.