Dim FileName As String = "MyExcel.xls"
'*** Create Excel.Application ***'
Dim xlApp As New Excel.Application
Dim xlSheet1 As Excel.Worksheet
Dim xlBook As Excel.Workbook
xlBook = xlApp.Workbooks.Add()
xlBook.Application.Visible = False
'*** Create Sheet 1 ***'
xlSheet1 = xlBook.Worksheets(1)
xlSheet1.Name = "My Sheet1"
'*** Header ***'
With xlApp.ActiveSheet.Cells(1, 1)
.Value = "เลขที่"
End With
With xlApp.ActiveSheet.Cells(1, 2)
.Value = "วันที่"
End With
With xlApp.ActiveSheet.Cells(1, 3)
.Value = "รายการ"
End With
With xlApp.ActiveSheet.Cells(1, 4)
.Value = "รายการย่อย"
End With
With xlApp.ActiveSheet.Cells(1, 5)
.Value = "ผู้รับ ผู้จ่าย"
End With
With xlApp.ActiveSheet.Cells(1, 6)
.Value = "รับ"
End With
With xlApp.ActiveSheet.Cells(1, 7)
.Value = "จ่าย"
End With
With xlApp.ActiveSheet.Cells(1, 8)
.Value = "คงเหลือ"
End With
With xlApp.ActiveSheet.Cells(1, 9)
.Value = "หมายเหตุ"
End With
'***********'
Dim intRows As Integer = 2
For i As Integer = 0 To Me.gridRevenue.Rows.Count - 1
'*** Detail ***'
With xlApp.ActiveSheet.Cells(intRows, 1)
.Value = gridRevenue.Rows(i).Cells(1)
End With
With xlApp.ActiveSheet.Cells(intRows, 2)
.Value = gridRevenue.Rows(i).Cells(2)
End With
With xlApp.ActiveSheet.Cells(intRows, 3)
.Value = gridRevenue.Rows(i).Cells(3)
End With
With xlApp.ActiveSheet.Cells(intRows, 4)
.Value = gridRevenue.Rows(i).Cells(4)
End With
With xlApp.ActiveSheet.Cells(intRows, 5)
.Value = gridRevenue.Rows(i).Cells(5)
End With
With xlApp.ActiveSheet.Cells(intRows, 6)
.Value = gridRevenue.Rows(i).Cells(6)
End With
With xlApp.ActiveSheet.Cells(intRows, 7)
.Value = gridRevenue.Rows(i).Cells(7)
End With
With xlApp.ActiveSheet.Cells(intRows, 8)
.Value = gridRevenue.Rows(i).Cells(8)
End With
With xlApp.ActiveSheet.Cells(intRows, 9)
.Value = gridRevenue.Rows(i).Cells(9)
End With
intRows = intRows + 1
Next
'*** If Files Already Exist Delete files ***'
Dim MyFile As New FileInfo(Server.MapPath(FileName))
If MyFile.Exists Then
MyFile.Delete()
End If
MyFile = Nothing
'*** Save Excel ***'
'xlSheet1.PrintOut 1 '*** Print to printer ***'
xlSheet1.SaveAs(Server.MapPath(FileName))
xlApp.Quit()
'System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet1)
'System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
'System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
'*** Quit and Clear Object ***'
xlSheet1 = Nothing
xlBook = Nothing
xlApp = Nothing
Response.Redirect(FileName)
Tag : .NET, Excel (Excel.Application), VB.NET, VS 2008 (.NET 3.x)