السلام عليكم ورحمة الله و بركاته
المشكلة :
- الحاجة الي البحث في ملف excel اي اصدار 97 -2003 - 2007 عن الاميلات
- الاخذ في الاعتبار وجود اكثر من Sheet في نفس الملف
الحل :
-تحميل الملف علي السرفر
- اضافة FileUpload control ,Upload button and label for errors
<asp:FileUpload ID="fileUploadExcel" runat="server" /> <asp:Button ID="btnImportExcel" runat="server" onclick="btnImportExcel_Click" Text="Import Data" > </asp:Button>
- التاكد من وجود ملف
if (!fileUploadExcel.HasFile)
{
lblErr.Text = "Please, Select Excel file.";
return;
}- التاكد من ان الملف Excel
// get file extension - using System.IO.Path
string fileExtension = Path.GetExtension(fileUploadExcel.FileName).ToLower();
// check if extension is correct
if (fileExtension != ".xlsx" && fileExtension != ".xls")
{
lblErr.Text = "Please, Select Excel file.";
return;
}-حفظ الملف
// save file to ExcelFiles folder
string filePath = Server.MapPath("~/ExcelFiles/" + fileUploadExcel.FileName);
fileUploadExcel.SaveAs(filePath);-انشاء ال connection string للملف باستخدام OleDb Providers
// create connection string based upon file extension
string strConn;
if (fileExtension == ".xlsx")
{
strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + @";Extended Properties=""Excel 12.0;""";
}
else
{
strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + @";Extended Properties=""Excel 8.0;""";
}- انشاء اتصال OleDbConnection و اعادة ال Sheets الموجودة في الملف ( يعامل ال sheet معاملة الجدول في ال database )
// Create OleDbConnection
OleDbConnection con = new OleDbConnection(strConn);
// data table to hold sheets' names
DataTable sheets = null;
try
{
// open connection
con.Open();
// get sheets(tables) using GetSchema method
sheets = con.GetSchema("Tables");
}
finally // dispose connection - we can use [using] clause instead of try-finally blocks
{
con.Close();
}- عمل loop و استرجاع بيانات كل ال sheets و استخدام ال regular expression لاستخراج الايميلات الموجودة في الملف و تخزينها في List of strings
// list for storing emails
List<string> importedEmails = new List<string>();
// iterate over sheets
foreach (DataRow dr in sheets.Rows)
{
// build command text - query - using TABLE_NAME column
string cmdText = "SELECT * FROM [" + dr["TABLE_NAME"].ToString() + "]";
// get sheet rows
OleDbDataAdapter cmd = new OleDbDataAdapter(cmdText, con);
DataSet dsExcel = new DataSet();
cmd.Fill(dsExcel, "ExcelSheet");
// go through all rows and extract emails using regular expression pattern
foreach (DataRow drEmail in dsExcel.Tables["ExcelSheet"].Rows)
{
// check each one of fields in current row
foreach (object field in drEmail.ItemArray)
{
// field isn't empty
if (!string.IsNullOrEmpty(field.ToString().Trim()))
{
// regular expression role
Match emailMatch = Regex.Match(field.ToString(), @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
// field contains email -- may be there are more than one email address in the same field but in this case no
if (emailMatch.Success)
{
// avoid duplication
if (!importedEmails.Contains(emailMatch.Value))
importedEmails.Add(emailMatch.Value);
}
}
}
}
}- عرض الايميلات
if (importedEmails.Count > 0)
{
lblErr.Text = importedEmails.Count.ToString() + " Email(s) Found.";
foreach (string email in importedEmails)
{
Response.Write(email);
Response.Write("<br/>");
}
}
else
{
lblErr.Text = "0 Email Found.";
}بالتوفيق ان شاء الله