 |
|
ขอตัวอย่างโค้ต vb.net ส่งออกเป็น excel หน่อยครับถ้ามีค้นหาด้วยก็ดีครับ |
|
 |
|
|
 |
 |
|
มันจะเป็นการแก้ปัญหาไม่ตรงจุดน่ะครับ ทำตามตัวอย่างไม่ผ่านเลยหาโค้ดใหม่เนี่ย
ถ้าคนอื่นเขาทำผ่านแล้ว คุณไม่ผ่านอยู่คนเดียวนี่ไม่ทราบว่ามันผิดที่อะไรครับ
|
 |
 |
 |
 |
Date :
2010-08-16 12:03:00 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
error แบบนี้แก้ไขยังไงหรอครับ
ส่วนหัว
Code (VB.NET)
Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.OleDb
Imports Excel
Imports System.IO
Imports System.Windows.Forms
รูปที่ขึ้น error

|
ประวัติการแก้ไข 2010-08-16 12:56:31
 |
 |
 |
 |
Date :
2010-08-16 12:55:22 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้ใช้บน Excel Version อะไรครับ
|
 |
 |
 |
 |
Date :
2010-08-16 12:57:59 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
2003 ครับ
|
 |
 |
 |
 |
Date :
2010-08-16 13:05:04 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Syntax แบบง่ายๆของ vb.Net ทำให้เป็นปัญหาค่ะ
Dim xlApp As New Excel.Application
แก้เป็น
Dim xlApp As Excel.Application = New Excel.Application()
เพราะตัว xlApp แบบแรกจะเป็น object ไม่มี properties อะไรของ excel app เลย
|
 |
 |
 |
 |
Date :
2010-08-16 14:08:35 |
By :
blurEyes |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ได้อ่ะครับลองแก้ไขตามคุณ Stupid gurl แล้ว
code
Code (VB.NET)
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
รุปตอนจะสร้าง header ครับ

มันไม่มี properties cells ให้เลือกอ่ะครับ
|
 |
 |
 |
 |
Date :
2010-08-16 15:22:03 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เท่าที่ดูเหมือนจะเป็นว่า ไม่มี properties นี้ป่าวคะ ลองดู object view ดูยังคะ
ปกติจะไม่ใช้ office interop เพื่อติดต่อกับ excel ,word ค่ะเลยไม่ชำนาญเท่าไหร่ รอพี่วินมาตอบแล้วกันนะคะ
|
 |
 |
 |
 |
Date :
2010-08-16 16:16:17 |
By :
blurEyes |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมดูแล้วมันก็ถูกน่ะครับหรือว่าผมไม่เห็น พี่วินรบกวนบอกด้วยครับ ใกล้จะเสร็จแล้วครับเหลือส่งออก excel อย่างเีดียว
|
 |
 |
 |
 |
Date :
2010-08-16 20:33:17 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
With xlApp.ActiveSheet.Cells(2,2)
.Value = "Mr.Weerachai Nukitram"
End With
|
 |
 |
 |
 |
Date :
2010-08-16 20:39:44 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เดียวจะลองเขียนให้ดูซะตัวอย่างน่ะครับ
|
 |
 |
 |
 |
Date :
2010-08-17 09:45:26 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2010-08-17 10:24:33 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แต่ต้องรอตอนเย็น ๆ น่ะครับ 
|
 |
 |
 |
 |
Date :
2010-08-17 10:55:38 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จะรอครับ ช่วงที่รอจะหาวิธีแก้ไขไปด้วยครับ
|
 |
 |
 |
 |
Date :
2010-08-17 11:20:59 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่รู้เค้าเขียนกันยังงัย ลองดูแบบนี้แล้วกันCode (VB.NET)
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
|
 |
 |
 |
 |
Date :
2010-08-17 11:30:08 |
By :
tee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
กลับถึงบ้านก่อนน่ะครับ ว่าแต่เขียนบน Visual Studio 2010 ใช้หรือเปล่าครับ
|
 |
 |
 |
 |
Date :
2010-08-17 17:38:18 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช่ครับ
|
 |
 |
 |
 |
Date :
2010-08-17 19:44:22 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมลองทำตามบทเรียนมันก็ Gen ได้ปกติน่ะครับ
Code (VB.NET)
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
|
 |
 |
 |
 |
Date :
2010-08-17 20:35:44 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับพี่วิน แค่ลบบรรทัดนี้ออก Option Strict On
แต่ผมงงว่าทำไมถึงใช้คำสั่งนี้ไม่ได้อ่ะครับ พี่วินพออธิบายให้ผมเข้าใจได้ป่าวครับ
ขอบคุณพี่วินมากน่ะครับและพี่ๆทุกคนที่ช่วยแนะนำ
|
 |
 |
 |
 |
Date :
2010-08-17 20:53:34 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Option Explicit On อันนี้จะต้องประกาศตัวแปรทุกตัว คือถ้าใน ASP/VB มันจะต้อง Dim ตัวแปร ทุกครั้ง ถ้าประกาศตัวนี้
Option Strict On ประมาณว่าจะต้องมีการกำหนดชนิดของตัวแปร ที่จัดเจนให้กับตัวแปร
|
 |
 |
 |
 |
Date :
2010-08-17 21:53:14 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ ไปนอนล่ะ

|
 |
 |
 |
 |
Date :
2010-08-17 22:26:46 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|