字符串检索-递归遍历文件夹-excel读取成dataset-结果导入到excel
来源:互联网 发布:土木工程要学c语言 编辑:程序博客网 时间:2024/06/05 19:22
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Text.RegularExpressions;using System.IO;using System.Data.OleDb;namespace ProCheck{ public partial class Form1 : Form { public Form1() { InitializeComponent(); progressBar1.Visible = false; } private void button1_Click(object sender, EventArgs e) { try { label5.Text = "Checking!"; string filename = excelpath.Text; DataSet ds; string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=Excel 8.0;" + "data source=" + filename; OleDbConnection myConn = new OleDbConnection(strCon); string strCom = " SELECT * FROM [Sheet1$]"; myConn.Open(); OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn); ds = new DataSet(); myCommand.Fill(ds); DataTable Exceldt = ds.Tables[0]; string proName; progressBar1.Refresh(); progressBar1.Visible = true; progressBar1.Minimum = 1; progressBar1.Maximum = Exceldt.Rows.Count; progressBar1.Step = 1; //for (int i = 0; i < 507;i++ ) for (int i = 0; i < Exceldt.Rows.Count; i++) { progressBar1.PerformStep(); proName = Convert.ToString(Exceldt.Rows[i][0]); string currentdir = dpath.Text; if (currentdir[currentdir.Length - 1] != '\\') //非根目录 currentdir += "\\"; DirectoryInfo Dir = new DirectoryInfo(currentdir); //if (FindFile(currentdir, proName)) if (FindFile(Dir, proName)) { Exceldt.Rows[i][1] = "1"; } else { Exceldt.Rows[i][1] = "0"; } } myConn.Close(); //return ds; label5.Text = "Checking Completed!"; SaveToExcel(Exceldt); label5.Text = "Over!"; progressBar1.Visible = false; } catch(Exception ex) { MessageBox.Show(ex.Message); } } public Boolean FindFile(DirectoryInfo dd, string proname) { FileInfo[] allfile = dd.GetFiles("*." + filtername.Text); foreach (FileInfo tt in allfile) { string str = System.IO.File.ReadAllText(tt.Directory + "/" + tt.Name); Regex reg = new Regex(proname); Match mat = reg.Match(str); if (mat.Success) { return true; } } DirectoryInfo[] direct = dd.GetDirectories(); foreach (DirectoryInfo dirTemp in direct) { if (FindFile(dirTemp, proname)) { return true; } } return false; } public void SaveToExcel(DataTable dt) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; //提示是否创建(*****).xls文件 saveFileDialog.Title = "导出Excel文件到"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { Stream myStream; myStream = saveFileDialog.OpenFile(); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312")); string str = ""; try { //写标题 for (int i = 0; i < dt.Columns.Count; i++) { if (i > 0) { str += "\t"; } str += dt.Columns[i].ColumnName; } sw.WriteLine(str); //写内容 for (int j = 0; j < dt.Rows.Count; j++) { string tempStr = ""; for (int k = 0; k < dt.Columns.Count; k++) { if (k > 0) { tempStr += "\t"; } //tempStr += dt.Rows[j].Cells[k].Value.ToString(); tempStr += dt.Rows[j][k].ToString(); } sw.WriteLine(tempStr); } MessageBox.Show("导出数据成功"); sw.Close(); myStream.Close(); } catch (Exception ee) { MessageBox.Show(ee.Message); return; } finally { sw.Close(); myStream.Close(); } } } }}