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();
}
}
}
}
}