Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.OleDb
Imports Excel
Imports System.IO
Imports System.Windows.Forms
Public Class FrmExcel
Inherits System.Windows.Forms.Form
'เหตุการณ์ฟอร์มโหลด
Private Sub FrmExcel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Today As New Date
Today = Date.Now
BeforeDate.Value = Today.AddMonths(-1)
End Sub
'เหตุการณ์กดปุ่ม Export
Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
Dim sqlSelect As String = ""
Dim dadpter As New OleDbDataAdapter
sqlSelect = "SELECT Jobref,ReqtoPlant,Mode,ShipperName,PO,INV,ETD,ATD,ETA,ATA,FWDR,GrossWeight,Quantity,Unit,"
sqlSelect &= "ICLCBM,[1x20],[1x40],[1x40HQ],ContrnCBM,Port,Country,BL_no,DO,Rcvd_Doc,Starting,Released,PlanDelivery,Delivery,Delivery_Place,Remark,"
sqlSelect &= "BG_Date,BG_Amount,ImportCustomer,TypeOfEntry,SKUNumber,DetailOfGoods,CIF,HSCode,TarrifRate,Duty,Vat,Eprivilege,Eduty,Vat2,CostSaving,"
sqlSelect &= "KPI_LeadTime,Billing,KPI_Billing,Duty2,DutyExcludedVat,DueDateDuty,"
sqlSelect &= "Freight,Exwork,DO1,OtherCharge,Total,ConsolBill,FrtBillNo,FrtBillDate,TranSportation,TotalAmount,BillingMonth,FrtFromUS,SavingFrtCost"
sqlSelect &= " FROM Detail"
sqlSelect &= " WHERE (ETD BETWEEN #" & CDate(BeforeDate.Value) & "# AND #" & CDate(UpToDate.Value) & "#) AND"
If rdoImport.Checked = True Then
sqlSelect &= " IsTypeImport = '1' AND IstypeExport = '0'"
ElseIf rdoExport.Checked = True Then
sqlSelect &= " IsTypeImport = '0' AND IstypeExport = '1'"
End If
dadpter = New OleDbDataAdapter(sqlSelect, ConnectAccess)
dadpter.Fill(dt)
'*** Export to Excel ****'
Dim FileName As String = "Kholer/FileExcel.xls"
'*** Create Excel.Application ***'
Dim xlApp As Excel.Application = New Excel.Application()
Dim xlSheet1 As Excel.Worksheet
Dim xlBook As Excel.Workbook
Dim i As Integer
Dim intRows As Integer
xlBook = xlApp.Workbooks.Add()
xlBook.Application.Visible = False
'*** Create Sheet 1 ***'
xlSheet1 = CType(xlBook.Worksheets(1), Worksheet)
xlSheet1.Name = "My Sheet1"
'*** Header ***'
With xlApp.ActiveSheet '<-- ตรงที่ผมติดครับ
.Value = "JOB REF"
End With
'***********'
End Sub
End Class
Dim ExcelApp As Excel.Application
Dim ExcelBooks As Excel.Workbook
Dim ExcelSheets As Excel.Worksheet
ExcelApp = New Excel.Application
Dim CurrentThread As System.Threading.Thread
CurrentThread = System.Threading.Thread.CurrentThread
CurrentThread.CurrentCulture = New CultureInfo("en-US")
ExcelApp.Visible = True
ExcelBooks = ExcelApp.Workbooks.Add()
ExcelSheets = ExcelBooks.ActiveSheet
ExcelSheets = DirectCast(ExcelBooks.Worksheets(1), Excel.Worksheet)
With ExcelSheets
.Cells(1, 1)).Value = "555"
End With
Imports Excel
Imports System.IO
Public Class Form1
Private Sub GenerateExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateExcel.Click
Dim FileName As String = "C:\GenExcel\GenExcel\xls\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()
'*** 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
'*** If Files Already Exist Delete files ***'
Dim MyFile As New FileInfo(FileName)
If MyFile.Exists Then
MyFile.Delete()
End If
MyFile = Nothing
'*** Save Excel ***'
'xlSheet1.PrintOut 1 '*** Print to printer ***'
xlSheet1.SaveAs(FileName)
xlApp.Application.Quit()
xlApp.Quit()
'*** Quit and Clear Object ***'
xlSheet1 = Nothing
xlBook = Nothing
xlApp = Nothing
End Sub
End Class