الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Uploading Excel File And Extracting Emails

بدأه AhmedElbaz في 16 سبتمبر 2008 · 4 رد · 1,278 مشاهدة · في ASP.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله و بركاته

المشكلة :

- الحاجة الي البحث في ملف 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.";
				}

بالتوفيق ان شاء الله

#2

شكرا على الموضوع اخ احمد

لدي كم سؤال بما اني لم اعمل سابقا على البيانات التي تكون داخل ملف واحد بستخدام ال ADO .net مثل ملفات Access او Excel

لدي كم سؤال بخصوص الموضوع

				strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + @";Extended Properties=""Excel 12.0;""";

				strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + @";Extended Properties=""Excel 8.0;""";

ما الفرق بين الالاثنين؟

اما السؤال الثاني

انا عملت سابق مع قواعد بيانات من نوع Oracle MySQL MsSQL و في جميع هذا الانواع كان ال GetSehema

تكون من احد عناصر ال DataAdapter انت هنا قمت بستدعائه من الOleDbConnection

:blink:

اقتباس
OleDbConnection con = new OleDbConnection(strConn);

DataTable sheets = null;

sheets = con.GetSchema("Tables");

السؤال الثالث من اين اتت الصفوف؟

فانت قم باخذ ال Schema فقط

اقتباس
foreach (DataRow dr in sheets.Rows)

و شكرا

#3
اقتباس
كود strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + @";Extended Properties=""Excel 12.0;""";

strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + @";Extended Properties=""Excel 8.0;""";

ما الفرق بين الالاثنين؟

الفرق بينهم هو الفرق بين office 2003 و office 2007 لان 2007 ممكن يحفظ الملف بامتداد xslx و هذا ال format يختلف عن xsl

اقتباس
اما السؤال الثاني

انا عملت سابق مع قواعد بيانات من نوع Oracle MySQL MsSQL و في جميع هذا الانواع كان ال GetSehema

تكون من احد عناصر ال DataAdapter انت هنا قمت بستدعائه من الOleDbConnection

إقتباس

OleDbConnection con = new OleDbConnection(strConn);

DataTable sheets = null;

sheets = con.GetSchema("Tables");

getSchema توجد في كل ال connections مثل sqlConnection

و اكيد ال DataAdapter.GetSchema تعتمد علي ال connection.GetSchema

اقتباس
السؤال الثالث من اين اتت الصفوف؟

فانت قم باخذ ال Schema فقط

إقتباس

foreach (DataRow dr in sheets.Rows)

عند استدعاء الدالة getShema يمكنك تحديد اي نوع من ال objects تريد و انا حددت tables (التي تشير الي ال sheets)

و تعود ب datatable عبارة عن خصائص الجدول او ال sheet مثل اسم ال sheet و ال LastModified و غيرها

المهم لكل sheet يوجد row يحتوي علي اسمه و هذا المطلوب

شكرا علي التعليق

بالتوفيق

#5

عفوا بالتوفيق

مواضيع مشابهة