السلام عليكم ورحمة الله وبركاته
اليوم تعبت من كثر ما ادور في النت على الحل لمشكلتي
حليت كل المشاكل الموجودة وباقي مشكلة واحدة هشرحها بعد ما أضع الكود
طبعا كود c# و console app.
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 Microsoft.Office.Interop.Excel;
using System.Reflection;
using Microsoft.Office.Tools.Excel;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string h = "khaled";
string file = @"C:\Users\hadi\Desktop\TestSheet.xls";
FindExample(file,h);
}
private void FindExample(Microsoft.Office.Interop.Excel.Worksheet ws, string sFindMe)
{
Microsoft.Office.Interop.Excel.Range rgFound;
string sFirstFoundAddress;
// Find's parameters are "sticky". If you don't specify them
// they'll default to the last used values - including parameters
// set via Excel's user interface
rgFound = ws.Cells.Find(sFindMe,
ws.Cells[1, 1],
Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues,
Microsoft.Office.Interop.Excel.XlLookAt.xlPart, Missing.Value, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false, Missing.Value, Missing.Value);
// If Find doesn't find anything, rgFound will be null
if (rgFound != null)
{
// Save the address of the first found item -
// it will be used in a loop terminating condition.
sFirstFoundAddress = rgFound.get_Address(
true, true, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value);
MessageBox.Show("Found match at cell " + sFirstFoundAddress);
// Continue finding subsequent items using FindNext
rgFound = ws.Cells.FindNext(rgFound);
string sAddress = rgFound.get_Address(
true, true, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value);
// Start a loop that calls FindNext until
// the first found cell is found again
while (!sAddress.Equals(sFirstFoundAddress))
{
MessageBox.Show("Found match at cell " + sAddress);
rgFound = ws.Cells.FindNext(rgFound);
sAddress = rgFound.get_Address(
true, true, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value);
}
}
MessageBox.Show("The worksheet has been searched.");
}
}
}طبعاً المشكلة الوحيدة في اعتقادي هي كيفية وضع مسار ملف الأكسل المطلوب البحث فيه
الخطأ الموجود في الكود السابق هو عند string file = @"C:\Users\hadi\Desktop\TestSheet.xls";
طبعاً لأني عرفته ك string والمفروض الكلاس يأخذ قيمة من نوع Microsoft.Office.Interop.Excel.Worksheet
اتمنى ان اجد المساعدة بأسرع وقت ممكن
لمن يحب ان يجرب الكود عنده لا ينسى ان يضيف ال refrences الخاصة بالإكسل
Microsoft.Office.Interop.Excel
Microsoft.Office.Tools.Exce
الشكر المسبق موصول لجميع الأعضاء