وقت بخیر دوستان، با آموزش برنامه نویسی (Visual Basic.NET (VB.NET از وب سایت پی وی لرن همراه هستیم. روش ایجاد یک برنامه اکسل از VB.NET را قصد داریم مورد بررسی قرار دهیم.
VB.Net پشتیبانی از قابلیت همکاری بین مدل شیء COM از Microsoft Excel 2010 و اپلیکیشن شما را فراهم می کند.
برای بهرهبرداری از این قابلیت همکاری در برنامه خود ، باید فضای نام Microsoft.Office.Interop.Excel را در برنامه فرم Windows تان وارد کنید.
حال بیایید با دنبال کردن مراحل زیر در Microsoft Visual Studio شروع به ایجاد یک Window Forms Application کنید:
File → New Project → Windows Forms Applications
در آخر ، OK را انتخاب کنید ، Microsoft Visual Studio پروژه شما را ایجاد می کند و فرم زیر را نمایش می دهد.
دکمه کنترل Button1 را در فرم وارد کنید.
یک مرجع به Microsoft Excel Object Library را به پروژه خود اضافه کنید. برای انجام این کار:
Add Reference را از Project Menu انتخاب کنید.
در تب COM ،مورد Microsoft Excel Object Library را پیدا کرده و سپس بر روی Select کلیک کنید.
OK را کلیک کنید.
بر روی پنجره کد دابل کلیک کنید و رویداد Click از Button1 را مطابق شکل زیر وارد کنید.
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | Add the following code snippet on top of Form1.vb Imports Excel = Microsoft.Office.Interop.Excel Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim appXL As Excel.Application Dim wbXl As Excel.Workbook Dim shXL As Excel.Worksheet Dim raXL As Excel.Range ' Start Excel and get Application object. appXL = CreateObject("Excel.Application") appXL.Visible = True ' Add a new workbook. wbXl = appXL.Workbooks.Add shXL = wbXl.ActiveSheet ' Add table headers going cell by cell. shXL.Cells(1, 1).Value = "First Name" shXL.Cells(1, 2).Value = "Last Name" shXL.Cells(1, 3).Value = "Full Name" shXL.Cells(1, 4).Value = "Specialization" ' Format A1:D1 as bold, vertical alignment = center. With shXL.Range("A1", "D1") .Font.Bold = True .VerticalAlignment = Excel.XlVAlign.xlVAlignCenter End With ' Create an array to set multiple values at once. Dim students(5, 2) As String students(0, 0) = "Zara" students(0, 1) = "Ali" students(1, 0) = "Nuha" students(1, 1) = "Ali" students(2, 0) = "Arilia" students(2, 1) = "RamKumar" students(3, 0) = "Rita" students(3, 1) = "Jones" students(4, 0) = "Umme" students(4, 1) = "Ayman" ' Fill A2:B6 with an array of values (First and Last Names). shXL.Range("A2", "B6").Value = students ' Fill C2:C6 with a relative formula (=A2 & " " & B2). raXL = shXL.Range("C2", "C6") raXL.Formula = "=A2 & "" "" & B2" ' Fill D2:D6 values. With shXL .Cells(2, 4).Value = "Biology" .Cells(3, 4).Value = "Mathmematics" .Cells(4, 4).Value = "Physics" .Cells(5, 4).Value = "Mathmematics" .Cells(6, 4).Value = "Arabic" End With ' AutoFit columns A:D. raXL = shXL.Range("A1", "D1") raXL.EntireColumn.AutoFit() ' Make sure Excel is visible and give the user control ' of Excel's lifetime. appXL.Visible = True appXL.UserControl = True ' Release object references. raXL = Nothing shXL = Nothing wbXl = Nothing appXL.Quit() appXL = Nothing Exit Sub Err_Handler: MsgBox(Err.Description, vbCritical, "Error: " & Err.Number) End Sub End Class |
هنگامی که کد فوق با استفاده از دکمه Start موجود در نوار ابزار Microsoft Visual Studio کامپایل و اجرا می شود، پنجره زیر را نشان می دهد.
با کلیک بر روی Button ، برگه اکسل زیر نمایش داده می شود. از شما خواسته می شود که کتاب کار (workbook) را ذخیره کنید.
در این بخش از آموزش vb.net با روش ایجاد یک برنامه اکسل از VB.NET آشنا شدیم. در بخش بعد به بررسی Send Email خواهیم پرداخت. با ما همراه باشید.