• 将DBF,XLS,XML,MDB文件导入C#DataGrid的方法


      1//以下的源码里分别给出了将DBF,XLS,XML,MDB文件导入C#DataGrid的方法,供各位参考。 
      2
      3//PutInDataSet.cs的源码 
      4using System; 
      5using System.Data.Odbc; 
      6using System.Data.OleDb; 
      7using System.Data; 
      8using System.Collections; 
      9
     10namespace PutInDataSet 
     11
     12/// <summary> 
     13/// DataSetTransIn 的摘要说明。 
     14/// </summary> 

     15public class PutInDataSet 
     16
     17/// <summary> 
     18/// 传入的文件变量 
     19/// </summary> 

     20private DataSet my_Ds;//存放文件的数据集 
     21private string my_Err;//错误信息 
     22private string my_TableName;//传入的文件名 
     23private TableType my_TableType;//传入的文件类型 
     24private string my_TablePath;//传入的文件路径 
     25private int my_TableIndex;//表的索引 
     26OleDbCommandBuilder my_Builder;//命令串 
     27
     28/// <summary> 
     29/// 数据库连接变量 
     30/// </summary> 

     31private string my_StrConnection;//连接字符串 
     32private string my_StrSelect;//select语句 
     33
     34/// <summary> 
     35/// 可以处理的文件类型 
     36/// </summary> 

     37public enum TableType 
     38
     39MDB,XLS,DBF,DOC,TXT,XML,HTML 
     40}
     
     41
     42public PutInDataSet(string TablePath,string TableName,TableType TableType) 
     43
     44///<summary> 
     45///获得传入的路径,文件名及文件类型; 
     46///</summary> 

     47this.my_TablePath=TablePath;//路径 
     48this.my_TableName=TableName;//文件名 
     49this.my_TableType=TableType;//文件类型 
     50}
     
     51
     52public DataSet Convert() 
     53
     54DataSet iRtn_Ds=new DataSet(); 
     55switch (this.my_TableType) 
     56
     57case TableType.DBF: 
     58iRtn_Ds = this.DbfToDs(); 
     59break
     60
     61case TableType.MDB: 
     62iRtn_Ds = this.MdbToDs(); 
     63break
     64
     65case TableType.XLS: 
     66iRtn_Ds = this.XlsToDs(); 
     67break
     68
     69case TableType.XML: 
     70iRtn_Ds = this.XMLToDs(); 
     71break
     72}
     
     73return iRtn_Ds; 
     74}
     
     75
     76///<summary> 
     77///将DBF文件放入DataSet 
     78///</summary> 

     79private DataSet DbfToDs() 
     80
     81//数据库连接定义 
     82OdbcConnection my_conn; //数据连接 
     83OdbcDataAdapter my_Adapter;//数据适配器 
     84
     85//数据库连接 
     86this.my_StrConnection= "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + this.my_TablePath; 
     87this.my_StrSelect="SELECT * FROM " + this.my_TableName; 
     88my_conn = new OdbcConnection(this.my_StrConnection); 
     89my_conn.Open(); 
     90my_Adapter = new OdbcDataAdapter(this.my_StrSelect,my_conn); 
     91this.my_Ds=new DataSet(); 
     92
     93//填充数据集 
     94my_Adapter.Fill(this.my_Ds,this.my_TableName); 
     95return this.my_
     96
     97
     98Ds; 
     99}
     
    100
    101///<summary> 
    102///将MDB文件放入DataSet 
    103///</summary> 

    104private DataSet MdbToDs() 
    105
    106//数据库连接定义 
    107OleDbConnection my_conn; 
    108OleDbDataAdapter my_Adapter; 
    109
    110//数据库连接 
    111this.my_StrConnection= "Provider=Microsoft.JET.OLEDB.4.0;data source=" + this.my_TablePath; 
    112this.my_StrSelect="SELECT * FROM " + this.my_TableName; 
    113my_conn = new OleDbConnection(this.my_StrConnection); 
    114my_conn.Open(); 
    115my_Adapter = new OleDbDataAdapter(this.my_StrSelect,my_conn); 
    116this.my_Ds=new DataSet(); 
    117
    118//填充数据集 
    119my_Adapter.Fill(this.my_Ds,this.my_TableName); 
    120return this.my_Ds; 
    121}
     
    122
    123///<summary> 
    124///将XML文件放入DataSet 
    125///</summary> 

    126private DataSet XMLToDs() 
    127
    128
    129//填充数据集 
    130this.my_Ds=new DataSet(); 
    131this.my_Ds.ReadXML(this.my_TablePath+this.my_TableName,XMLReadMode.ReadSchema); 
    132this.my_Ds.DataSetName="XMLData"
    133return this.my_Ds; 
    134}
     
    135
    136///<summary> 
    137///将Excel文件放入DataSet 
    138///</summary> 

    139private DataSet XlsToDs() 
    140
    141OleDbConnection my_conn; 
    142OleDbDataAdapter my_Adapter; 
    143
    144//数据库连接 
    145this.my_StrConnection= "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties=Excel 8.0;data source="+this.my_TablePath+this.my_TableName; 
    146this.my_StrSelect="SELECT * FROM [SHEET1$]"
    147my_conn = new OleDbConnection(this.my_StrConnection); 
    148my_conn.Open(); 
    149my_Adapter = new OleDbDataAdapter(this.my_StrSelect,my_conn); 
    150this.my_Builder=new OleDbCommandBuilder(my_Adapter); 
    151this.my_Ds=new DataSet(); 
    152
    153//填充数据集 
    154my_Adapter.Fill(this.my_Ds,"ExcelData"); 
    155return this.my_Ds; 
    156}
     
    157
    158
    159}
     
    160}
     
    161
    162
    163//Form_PutInDataSet.cs的源码 
    164
    165using System; 
    166using System.Data; 
    167using System.Drawing; 
    168using System.Collections; 
    169using System.ComponentModel; 
    170using System.Windows.Forms; 
    171using DataAccess.SysManage; 
    172using BusinessRules; 
    173using DataSetTrans; 
    174
    175
    176namespace WinForm.Common 
    177
    178/// <summary> 
    179/// FormDesktop 的摘要说明。 
    180/// </summary> 

    181public class FormDesktop : System.Windows.Forms.Form 
    182
    183private WinForm.Common.DeskTop deskTop1; 
    184private System.Windows.Forms.Button button1; 
    185/// <summary> 
    186/// 必需的设计器变量。 
    187/// </summary> 

    188private System.ComponentModel.Container components = null
    189
    190private DataSet m_ds = new DataSet(); 
    191private System.Windows.Forms.DataGrid dataGrid1; //数据源 
    192private string m_TableName; //外部文件名称 
    193
    194public FormDesktop() 
    195
    196// 
    197// Windows 窗体设计器支持所必需的 
    198// 
    199InitializeComponent(); 
    200
    201// 
    202// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
    203// 
    204}
     
    205
    206/// <summary> 
    207/// 清理所有正在使用的资源。 
    208/// </summary> 

    209protected override void Dispose( bool disposing ) 
    210
    211if( disposing ) 
    212
    213if(components != null
    214
    215components.Dispose(); 
    216}
     
    217}
     
    218base.Dispose( disposing ); 
    219}
     
    220
    221Windows Form Designer generated code 
    280
    281private void FormDesktop_Load(object sender, System.EventArgs e) 
    282
    283
    284}
     
    285
    286/// <summary> 
    287/// 当窗口改变大小时自动居中。 
    288/// </summary> 

    289private void FormDesktop_Resize(object sender, System.EventArgs e) 
    290
    291if (this.WindowState != FormWindowState.Minimized) 
    292
    293if (this.Width > this.deskTop1.Width && this.Height > this.deskTop1.Height ) 
    294
    295this.deskTop1.Left = (this.Width - this.deskTop1.Width) / 2
    296this.deskTop1.Top = (this.Height- this.deskTop1.Height)/ 2
    297}
     
    298}
     
    299}
     
    300
    301private void button1_Click(object sender, System.EventArgs e) 
    302
    303DataSet out_Ds=new DataSet(); 
    304PutInDataSet obj=new PutInDataSet("文件路径","文件名",PutInDataSet.TableType.文件格式);//调用PutInDataSet类 
    305out_Ds=obj.Convert();//转换到DataSet中 
    306this.dataGrid1.DataSource=out_Ds.Tables["表名"];//在DataGrid中显示 
    307
    308}
     
    309}
     
    310}
     
    311
  • 相关阅读:
    iOS优秀博文合集
    iOS优化策略
    iOS编码规范
    二维码
    Objective-C Http常用API 同步请求与异步请求
    iOS开发之Runtime函数
    ios开发之常用宏的定义
    iOS开发之动画编程的几种方法
    iOS开发之常用第三方框架(下载地址,使用方法,总结)
    使用OC和swift创建系统自带的刷新界面
  • 原文地址:https://www.cnblogs.com/lavenderzh/p/1357168.html
Copyright © 2020-2023  润新知