 |
|
สอบถามการ Import Excel ลง ใน VB.Net สามารถเลือกเฉพาะคอลัมน์หรือแถวตามที่ต้องการได้ไหมคะ |
|
 |
|
|
 |
 |
|
คำถามนี้น่าสนครับ
แต่น่าจะยาก
ผมใช้การเปด excell ผ่าน Microsoft.Office.Interop.Excel มาแล้ว for เอาอ่าครับ
|
 |
 |
 |
 |
Date :
2015-09-12 12:45:27 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณได้ DataTable แล้วนี่ครับ มันก็ Loop เพื่อเลือกแต่ล่ะรายการได้เลยครับ
อีกตัวอย่าง
Code (VB.NET)
<%
Dim xlApp,xlBook,xlSheet1,xlSheet2,OpenFile,i
Dim Conn,strSQL,objExec
OpenFile = "MyXls/MyCustomer.xls"
'*** Create Exce.Application ***'
Set xlApp = Server.CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(Server.MapPath(OpenFile))
Set xlSheet1 = xlBook.Worksheets(1)
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("MyXls/mydatabase.mdb"),"" , ""
For i = 2 To 5
If Trim(xlSheet1.Cells.Item(i,1)) <> "" Then
strSQL = ""
strSQL = strSQL &"INSERT INTO customer2 "
strSQL = strSQL &"(CustomerID,Name,Email,CountryCode,Budget,Used) "
strSQL = strSQL &"VALUES "
strSQL = strSQL &"('"&xlSheet1.Cells.Item(i,1)&"','"&xlSheet1.Cells.Item(i,2)&"', '"&xlSheet1.Cells.Item(i,3)&"' "
strSQL = strSQL &",'"&xlSheet1.Cells.Item(i,4)&"','"&xlSheet1.Cells.Item(i,5)&"', '"&xlSheet1.Cells.Item(i,6)&"') "
Set objExec = Conn.Execute(strSQL)
Set objExec = Nothing
End IF
Next
xlApp.Application.Quit
'*** Quit and Clear Object ***'
Conn.Close()
Set Conn = Nothing
Set xlSheet1 = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
%>
|
 |
 |
 |
 |
Date :
2015-09-13 10:05:58 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังไม่ค่อยเข้าใจเลยคะ รบกวนพี่ๆอธิบายหน่อยนะคะ ขอบคุณคะ
|
 |
 |
 |
 |
Date :
2015-09-18 21:43:42 |
By :
Preeyapan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำได้แล้วคะ โดยไม่ต้องวนลูปเลยคะ เพียงแต่เพิ่มว่าเราต้องการเลือกแถวคอลัมน์ไหนใน excel บ้าง
Dim da As New OleDbDataAdapter("Select *from[MyExcel$A4:D]", cn)
เพิ่ม A4:D ไปคะ
ขอบคุณพี่ๆคะที่ให้คำแนะนำ
|
 |
 |
 |
 |
Date :
2015-09-21 12:58:30 |
By :
Preeyapan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
  
|
 |
 |
 |
 |
Date :
2015-09-21 13:44:34 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|