Generating Excel Report in .NET Framework สร้างเอกสาร Microsoft Excel (.xls) ง่าย ๆ บน Windows Application และ Console Application ด้วย Framework (VB.NET)
1. สร้าง Windows Application หรือ Console Application
2. Add Library ของ Excel
3. Code ในโปรแกรม
Code (VB.NET)
Dim FileName As String = "MyExcel.xls"
'*** Create Excel.Application ***'
Dim xlApp As New Microsoft.Office.Interop.Excel.Application
Dim xlSheet1 As Microsoft.Office.Interop.Excel.Worksheet
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
xlBook = xlApp.Workbooks.Add()
'*** Create Sheet 1 ***'
xlSheet1 = xlBook.Worksheets(1)
xlSheet1.Name = "My Sheet1"
xlApp.Application.Visible = False
'*** Write text to Row 1 Column 1 ***'
With xlApp.ActiveSheet.Cells(1, 1)
.Value = "ThaiCreate.Com"
End With
'*** Write text to Row 2 Column 2 ***'
With xlApp.ActiveSheet.Cells(2, 2)
.Value = "Mr.Weerachai Nukitram"
End With
'*** Save Excel ***'
'xlSheet1.PrintOut 1 '*** Print to printer ***'
xlSheet1.SaveAs("C:\" & FileName)
xlApp.Application.Quit()
xlApp.Quit()
'*** Quit and Clear Object ***'
xlSheet1 = Nothing
xlBook = Nothing
xlApp = Nothing