String search-recursive traversal folder-read from excel to dataset-import the result to excel

Source: Internet
Author: User

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 button#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]! = '\') // Non-root directory
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; // The system prompts whether to create the file (***** *.xls ).
SaveFileDialog. Title = "Export an Excel file ";

If (saveFileDialog. ShowDialog () = DialogResult. OK)
{
Stream myStream;
MyStream = saveFileDialog. OpenFile ();
StreamWriter sw = new StreamWriter (myStream, System. Text. Encoding. GetEncoding ("gb2312 "));

String str = "";
Try
{
// Write the title
For (int I = 0; I <dt. Columns. Count; I ++)
{
If (I> 0)
{
Str + = "\ t ";
}
Str + = dt. Columns [I]. ColumnName;
}

Sw. WriteLine (str );
// Write content
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 ("data exported ");
Sw. Close ();
MyStream. Close ();

}
Catch (Exception ee)
{
MessageBox. Show (ee. Message );
Return;
}
Finally
{
Sw. Close ();
MyStream. Close ();
}
}

}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.