מה שבעצם הייתי צריכה, כשהתחלתי, היה מידע מרוכז עם דוגמאות על איך עובדים עם קבצים דרך ויז'ואל בייסיק. מצאתי בדרך די הרבה עזרה בפורומים, ביוטיוב, באתרי העזרה של מייקרוסופט, אבל כל דבר לקח שעות והיה באנגלית. מה שאני מנסה לעשות בסדרת הפוסטים הזו, היא לקצר את זמן החיפוש, לייעל את העבודה וכמובן – לעשות את זה בעברית. בסוף כל פוסט אצרף רשימה של לינקים מתוך הפייבוריטס שצברתי בזמן שחיפשתי. למי שתרצה מידע נוסף. מיותר לציין שאני אשמח, אגיל ואדוץ לכל הערה, תיקון, השגה ומידע נוסף שיש לכן להוסיף. בכל מה שקשור לויבי אני כל הזמן לומדת. שנתחיל?
הקוד לפתיחת קובץ:
1 2 3 4 5 6 7 8 9 10 11 | Dim objExcel As Excel.Application Dim ObjBook As Excel.Workbook Dim objSheet As Excel.Worksheet objExcel = New Excel.Application ' בהנחה שידוע שם הקובץ אותו רוצות לפתוח ObjBook = objExcel.Workbooks.Open(sFileName) 'בדיקה שאכן נפתח איזשהו קובץ If (ObjBook IsNot Nothing) Then objSheet = ObjBook .Worksheets(1) 'בחירה בגיליון שאותו רוצות פעיל- גיליון אחד בדוגמה |
כאשר רוצים לפתוח את החלון המאפשר בחירה איזה קובץ לפתוח, ניתן להעזר בפונקציה הבאה:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | Public Function GetFileName(ByVal bOpenDialog As Boolean, ByVal defPath As String) As String Dim FileName As String FileName = "" If bOpenDialog Then 'יצירת אינסטנס לאובייקט מסוג פתיחת חלון קבצים Dim fdlg As OpenFileDialog = New OpenFileDialog() 'כאן יש להכניס את הנתיב הדיפולטי לפתיחת החלון fdlg.InitialDirectory = defPath 'איזה סוג קבצים על החלון להציג fdlg.Filter = "xls files (*.xls)|*.*|All files (*.*)|*.*" fdlg.FilterIndex = 2 fdlg.RestoreDirectory = True 'אם חוזר אוקי אז שם הקובץ הוא הנבחר If fdlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then FileName = fdlg.FileName() End If Return FileName End Function |
כדי לשמור את הקובץ, אפשר להשתמש בשגרה הבאה:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Public Sub SaveFileAsExcel(ByVal fileDetails) Dim objExcel As Excel.Application Dim ObjWorkbook As Excel.Workbook objExcel = New Excel.Application ObjWorkbook = objExcel.Workbooks.Open(fileDetails) If (ObjWorkbook IsNot Nothing) Then Try ObjWorkbook.SaveAs(fileDetails, Excel.XlFileFormat.xlWorkbookNormal) Catch exp As Exception End Try End If ObjWorkbook.Close() ObjWorkbook = Nothing objExcel.Quit() objExcel = Nothing End Sub |
בדיקה האם הקובץ כבר פתוח כרגע במערכת, על ידי שליחת שם הקובץ לפונקציה הבאה. הפונקציה מחזירה TRUE אם הקובץ פתוח, FALSE אם לא:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | Public Function IsFileOpen(ByVal FileName As String) As Boolean Dim FileNum As Integer Dim ErrNum As Integer Dim V As Object If Trim(FileName) = vbNullString Then Return False 'קובץ לא קיים V = Dir(FileName, vbNormal) If IsError(V) = True Or V = vbNullString Then Return False FileNum = FreeFile() Err.Clear() ' ניסיון לפתוח את הקובץ ולנעול אותו. FileOpen(FileNum, FileName, OpenMode.Input, OpenAccess.Default, OpenShare.LockRead) ErrNum = Err.Number FileClose(FileNum) ' לסגור את הקובץ. On Error GoTo 0 Select Case ErrNum Case 0 ' הקובץ סגור. Return False Case 75 ' הקובץ פתוח. Return True Case Else ' שגיאה כלשהי קרתה נניח שהקובץ פתוח. Return True End Select End Function |
קישורים נוספים לעבודה עם קבצים של אקסל שיכולים לעזור:
מדריך ויבי באנגלית
msdn – Automate Office Excel 2007 Reports with Visual Basic 2005
File Operations in Visual Basic (By John Smiley)
MS Help – How to automate Microsoft Excel from Visual Basic
נהניתם ? הרשמו לעדכוני RSS !
אני לא מספיק משופשף בתכנות לאקסל, אבל נראה לי מוזר שאת יוצרת מחדש אובייקט EXCEL.Application כשהקובץ ממילא כבר פתוח ואת רק רוצה לשמור אותו. לדעתי את אמורה לעשות את זה עם ActiveWorkbook או שם דומה ולהכניס אותו ישר לobjWorkbook, בלי שום objExcel.
נכון. במקרה הספציפי שלי מה שאני עושה זה פורסת את הנתונים מכמה קבצים שמתקבלים ונשמרים אצל המשצנש, ויוצרת קובץ חדש שמאגד את הנתונים האלה אחרי שהם עברו עיבוד. אבל אם מה שעושים זה לשמור את אותו קובץ שכבר פתוח – אז כן. אין צורך לייצר אובייקט חדש.
מה שכן כדאי, אולי לבדוק, אם רוצים להשתמש באותו גיליון עבודה לאורך כל התוכנית, זה שהאובייקט מוגדר גלובלית ולא לוקלית. לא ניסיתי ולכן אני לא ממש יודעת.
That sounds very good however , i am still less than sure that I favor it. Anyhow will look further into it and choose for myself!
Well, that's decent, but think about additional choices we have here? Would you mind writing a further post regarding them as well? Appreciate it!
Well, another blogger already posted a topic like this.–.;:
Wow, you seem to be very knowledgable about this kind of topics.":,.'
Perhaps you should update the php server on your webhost, WordPress is kinda slow.*-~,'
We surely need to think a lot more in that direction and find out what i can do about that.
Have you already setup a fan page on Facebook ?-;*~;
I view something really special in this website .
Perfectly written subject material , Really enjoyed looking through .
I love blogging and i can say that you also love blogging.""*-
I love blogging and i can say that you also love blogging.~"-""
I discovered your weblog site on google and appearance a couple of of your early articles. Continue to keep up the quite great operate. I simply extra your RSS feed in order to my personal MSN Information Readers. Seeking forward to reading through much more of your stuff afterwards!?–