將DBF,XLS,XML,MDB文件導入C#DataGrid的方法
2024-09-05 20:55:47
供稿:網友
以下的源碼里分別給出了將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中顯示
}
}
}
國內最大的酷站演示中心!