|
本文采用c#语言写的从dbf数据库提取数据规格化生成为文本
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; namespace gongzi { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Label label4; private System.Windows.Forms.RadioButton radioButton1; private System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label6; private System.Windows.Forms.TextBox textBox5; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Label msg; protected System.Windows.Forms.TextBox gettime; private System.Windows.Forms.GroupBox groupBox2; protected System.Windows.Forms.DataGrid datas; private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1; protected string sourcefile; protected string filepath; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.msg = new System.Windows.Forms.Label(); this.button2 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.textBox5 = new System.Windows.Forms.TextBox(); this.gettime = new System.Windows.Forms.TextBox(); this.label6 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.radioButton2 = new System.Windows.Forms.RadioButton(); this.radioButton1 = new System.Windows.Forms.RadioButton(); this.label4 = new System.Windows.Forms.Label(); this.textBox3 = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.datas = new System.Windows.Forms.DataGrid(); this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.datas)).BeginInit(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.msg); this.groupBox1.Controls.Add(this.button2); this.groupBox1.Controls.Add(this.button1); this.groupBox1.Controls.Add(this.textBox5); this.groupBox1.Controls.Add(this.gettime); this.groupBox1.Controls.Add(this.label6); this.groupBox1.Controls.Add(this.label5); this.groupBox1.Controls.Add(this.radioButton2); this.groupBox1.Controls.Add(this.radioButton1); this.groupBox1.Controls.Add(this.label4); this.groupBox1.Controls.Add(this.textBox3); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.textBox2); this.groupBox1.Controls.Add(this.textBox1); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Location = new System.Drawing.Point(16, 16); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(520, 248); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; // // msg // this.msg.Location = new System.Drawing.Point(24, 200); this.msg.Name = "msg"; this.msg.Size = new System.Drawing.Size(224, 16); this.msg.TabIndex = 15; // // button2 // this.button2.Location = new System.Drawing.Point(272, 200); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(80, 24); this.button2.TabIndex = 14; this.button2.Text = "选择文件"; this.button2.Click += new System.EventHandler(this.button2_Click); // // button1 // this.button1.Location = new System.Drawing.Point(384, 200); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(80, 24); this.button1.TabIndex = 13; this.button1.Text = "导出文件"; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox5 // this.textBox5.Location = new System.Drawing.Point(120, 160); this.textBox5.Name = "textBox5"; this.textBox5.Size = new System.Drawing.Size(104, 21); this.textBox5.TabIndex = 12; this.textBox5.Text = "001"; // // gettime // this.gettime.Location = new System.Drawing.Point(120, 128); this.gettime.Name = "gettime"; this.gettime.Size = new System.Drawing.Size(216, 21); this.gettime.TabIndex = 11; this.gettime.Text = ""; // // label6 // this.label6.Location = new System.Drawing.Point(24, 168); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(104, 24); this.label6.TabIndex = 10; this.label6.Text = "第几个代理数据"; // // label5 // this.label5.Location = new System.Drawing.Point(24, 136); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(80, 16); this.label5.TabIndex = 9; this.label5.Text = "送银行日期"; // // radioButton2 // this.radioButton2.Location = new System.Drawing.Point(264, 96); this.radioButton2.Name = "radioButton2"; this.radioButton2.TabIndex = 8; this.radioButton2.Text = "代扣"; // // radioButton1 // this.radioButton1.Checked = true; this.radioButton1.Location = new System.Drawing.Point(120, 96); this.radioButton1.Name = "radioButton1"; this.radioButton1.TabIndex = 7; this.radioButton1.TabStop = true; this.radioButton1.Text = "代收"; // // label4 // this.label4.Location = new System.Drawing.Point(24, 104); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(56, 16); this.label4.TabIndex = 6; this.label4.Text = "借款标志"; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(120, 64); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(344, 21); this.textBox3.TabIndex = 5; this.textBox3.Text = "3100020511900201628"; // // label3 // this.label3.Location = new System.Drawing.Point(24, 72); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(72, 16); this.label3.TabIndex = 4; this.label3.Text = "企业帐号"; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(336, 32); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(128, 21); this.textBox2.TabIndex = 3; this.textBox2.Text = "201"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(120, 32); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(112, 21); this.textBox1.TabIndex = 2; this.textBox1.Text = "028610050"; // // label2 // this.label2.Location = new System.Drawing.Point(256, 40); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(72, 16); this.label2.TabIndex = 1; this.label2.Text = "代理业务种类"; // // label1 // this.label1.Location = new System.Drawing.Point(24, 40); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(72, 16); this.label1.TabIndex = 0; this.label1.Text = "代理业务编号"; // // groupBox2 // this.groupBox2.Controls.Add(this.datas); this.groupBox2.Location = new System.Drawing.Point(16, 272); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(520, 224); this.groupBox2.TabIndex = 1; this.groupBox2.TabStop = false; this.groupBox2.Text = "groupBox2"; // // datas // this.datas.AllowDrop = true; this.datas.DataMember = ""; this.datas.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.datas.Location = new System.Drawing.Point(8, 16); this.datas.Name = "datas"; this.datas.Size = new System.Drawing.Size(504, 200); this.datas.TabIndex = 0; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(552, 504); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "Form1"; this.Text = "存储代理业务(社保专用)"; this.Load += new System.EventHandler(this.Form1_Load); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.datas)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { string strdate=System.DateTime.Now.Year.ToString(); if((System.DateTime.Now.Month>=10)&&(System.DateTime.Now.Month<=12)) strdate+=System.DateTime.Now.Month.ToString(); else strdate+="0"+System.DateTime.Now.Month.ToString(); if((System.DateTime.Now.Day>=1)&&(System.DateTime.Now.Day<=9)) strdate+="0"+System.DateTime.Now.Day.ToString(); else strdate+=System.DateTime.Now.Day.ToString(); this.gettime.Text=strdate; } private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e) {
} private void button2_Click(object sender, System.EventArgs e) { System.IO.Stream myStream; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "F:\\gh" ; openFileDialog1.Filter = "dbf files (*.dbf)|*.DBF" ; openFileDialog1.FilterIndex = 2 ; openFileDialog1.RestoreDirectory = true ; if(openFileDialog1.ShowDialog() == DialogResult.OK) { try { if((myStream = openFileDialog1.OpenFile())!= null) { // Insert code to read the stream here. myStream.Close(); this.msg.Text=openFileDialog1.FileName.ToString(); this.sourcefile=openFileDialog1.FileName.ToString(); getdbf(this.sourcefile); } } catch(System.IO.IOException ex) { MessageBox.Show("载入文件失败,请检查该文件是否已经被打开"); } } } protected void getdbf(string table) { System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection(); string [url=mailto:connStr=@]connStr=@"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + @table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO"; conn.ConnectionString = connStr; conn.Open(); string sql = @"select * from " + @table; System.Data.Odbc.OdbcDataAdapter da =new System.Data.Odbc.OdbcDataAdapter(sql,conn); System.Data.DataSet ds=new DataSet(); da.Fill(ds); this.datas.DataSource=ds.Tables[0]; conn.Close(); conn.Dispose(); } private void button1_Click(object sender, System.EventArgs e) { if(this.textBox5.Text.Trim().Length>1) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { this.filepath= folderBrowserDialog1.SelectedPath+"\\"; savedatas(this.sourcefile); } } else { MessageBox.Show("请输入是第几笔数据,默认是001"); } } protected void savedatas(string table) { System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection(); string [url=mailto:connStr=@]connStr=@"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + @table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO"; conn.ConnectionString = connStr; //此处修改 究竟是哪个字段和位书 此处暂时定为右截取17位 ,另外还需要除空 //string sql = @"select ACCNO from " + @table; string sql = @"select Aae010,Aic142 from " + @table; System.Data.Odbc.OdbcCommand cmd=new System.Data.Odbc.OdbcCommand(sql,conn); conn.Open(); System.Data.Odbc.OdbcDataReader dr; dr=cmd.ExecuteReader(); string str; str=this.textBox1.Text.ToString()+this.gettime.Text.ToString()+this.textBox5.Text.ToString()+".txt"; System.IO.StreamWriter sw; sw=File.CreateText(this.filepath+str); while(dr.Read()) { str="2"+this.textBox1.Text.ToString()+this.textBox2.Text.ToString(); str+=dr[0].ToString().Substring(0,16)+"11"; //暂时补成19位为11 str+="0"+" 0";//6位最后0 str+=static_str(dr[1].ToString().Trim(),17); str+=" 1"+"2"; str+=" "; sw.WriteLine(str); } sw.Close(); conn.Close(); conn.Dispose(); MessageBox.Show("导出成功"); } static string static_str(string str, int n) { for (int i=str.Length;i<n;i++) { str=" "+str; } return str.ToString(); } } }
|