PDA

View Full Version : نحوه اجرای یک sheet خاص از فایل اکسل چگونه است؟



mahdi-1
دوشنبه 16 اردیبهشت 1392, 15:15 عصر
نحوه اجرای یک sheet خاص از فایل اکسل چگونه است؟
از دستور system.diagnostice.process استفاده کردم ولی نمیشه sheet خاصی رو برای باز کردنبهش داد و فقط فایل رو باز میکنه.

mahdi-1
دوشنبه 16 اردیبهشت 1392, 15:21 عصر
کسی نبود کمک کنه؟

peymanjon
دوشنبه 16 اردیبهشت 1392, 21:00 عصر
//Add this codes in your progam code
private static Microsoft.Office.Interop.Excel.ApplicationClass appExcel;
private static Workbook newWorkbook = null;
private static _Worksheet objsheet = null;

//Method to initialize opening Excel
static void excel_init(String path)
{
appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();

if (System.IO.File.Exists(path))
{
// then go and load this into excel
newWorkbook = appExcel.Workbooks.Open(path, true, true);
objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
}
else
{
MessageBox.Show("Unable to open file!");
System.Runtime.InteropServices.Marshal.ReleaseComO bject(appExcel);
appExcel = null;
System.Windows.Forms.Application.Exit();
}

}

//Method to get value; cellname is A1,A2, or B1,B2 etc...in excel.
static string excel_getValue(string cellname)
{
string value = string.Empty;
try
{
value = objsheet.get_Range(cellname).get_Value().ToString( );
}
catch
{
value = "";
}

return value;
}

//Method to close excel connection
static void excel_close()
{
if (appExcel != null)
{
try
{
newWorkbook.Close();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(appExcel);
appExcel = null;
objsheet = null;
}
catch (Exception ex)
{
appExcel = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}

بعد از اضافه کردن توابع بالا از این کد استفاده کنید:

excel_init("C:\\excel_tutorial.xlsx");
اگه می خواهید مقدار یک سلول را بدست بیارید:

excel_getValue("B10");

اینم یه نمونه کد دیگه:
http://www.codeproject.com/Articles/5123/Opening-and-Navigating-Excel-with-C

mahdi-1
سه شنبه 17 اردیبهشت 1392, 19:04 عصر
//Add this codes in your progam code
private static Microsoft.Office.Interop.Excel.ApplicationClass appExcel;
private static Workbook newWorkbook = null;
private static _Worksheet objsheet = null;

//Method to initialize opening Excel
static void excel_init(String path)
{
appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();

if (System.IO.File.Exists(path))
{
// then go and load this into excel
newWorkbook = appExcel.Workbooks.Open(path, true, true);
objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
}
else
{
MessageBox.Show("Unable to open file!");
System.Runtime.InteropServices.Marshal.ReleaseComO bject(appExcel);
appExcel = null;
System.Windows.Forms.Application.Exit();
}

}

//Method to get value; cellname is A1,A2, or B1,B2 etc...in excel.
static string excel_getValue(string cellname)
{
string value = string.Empty;
try
{
value = objsheet.get_Range(cellname).get_Value().ToString( );
}
catch
{
value = "";
}

return value;
}

//Method to close excel connection
static void excel_close()
{
if (appExcel != null)
{
try
{
newWorkbook.Close();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(appExcel);
appExcel = null;
objsheet = null;
}
catch (Exception ex)
{
appExcel = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}

بعد از اضافه کردن توابع بالا از این کد استفاده کنید:

excel_init("C:\\excel_tutorial.xlsx");
اگه می خواهید مقدار یک سلول را بدست بیارید:

excel_getValue("B10");

اینم یه نمونه کد دیگه:
http://www.codeproject.com/Articles/5123/Opening-and-Navigating-Excel-with-C

خیلی ممنون دوست عزیز فقط بگید که این دستور فایل به اجرا در میاره یا فقط میشه از داده هاش تو برنامه استفاده کرد و برای یافتن Sheet1 در فایل A چه دستوری باید بنویسم
با تشکر از شما