 |
|
Upload Excel and Import to Database asp.net ติดปัญหา รันไม่ได้ ช่วยดูหน่อยค่ะ |
|
 |
|
|
 |
 |
|
Code (VBScript)
Sub BindData()
Dim OpenFile As String
Dim i As Integer
OpenFile = "MyXls/Sheet1.xls"
'*** Create Excel.Application ***'
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet1 As Excel.Worksheet
xlBook = xlApp.Workbooks.Open(Server.MapPath(OpenFile))
xlBook.Application.Visible = False
xlSheet1 = CType(xlBook.Worksheets(1), Excel.Worksheet)
'*** Create DataTable ***'
Dim dt As New System.Data.DataTable
Dim dr As System.Data.DataRow
'*** Column ***'
dt.Columns.Add("id")
dt.Columns.Add("name")
dt.Columns.Add("lname")
For i = 0 To 2
'*** Rows ***'
dr = dt.NewRow
dr("id") = xlSheet1.Cells.Item(i, 0).Value
dr("name") = xlSheet1.Cells.Item(i, 1).Value
dr("lname") = xlSheet1.Cells.Item(i, 2).Value
dt.Rows.Add(dr)
Next
'*** End DataTable ***'
'*** BindData To DataGrid ***'
Me.myRepeater.DataSource = dt
Me.myRepeater.DataBind()
'*** Quit and Clear Object ***'
xlApp.Application.Quit()
xlApp.Quit()
xlSheet1 = Nothing
xlBook = Nothing
xlApp = Nothing
End Sub
Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim objConn As OleDbConnection
Dim objCmd As OleDbCommand
Dim strConnString As String
Dim strSQL As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database/EVALUATION.mdb") & ";"
objConn = New OleDbConnection(strConnString)
objConn.Open()
Dim chkID As System.Web.UI.WebControls.CheckBox
Dim lblID, lblName, lbllname As System.Web.UI.WebControls.Label
Dim i As Integer
For i = 0 To myRepeater.Items.Count - 1
chkID = CType(myRepeater.Items(i).FindControl("chkID"), CheckBox)
lblID = CType(myRepeater.Items(i).FindControl("lblid"), Label)
lblName = CType(myRepeater.Items(i).FindControl("lblname"), Label)
lbllname = CType(myRepeater.Items(i).FindControl("lbllname"), Label)
If chkID.Checked = True Then
'*** Insert Record ***'
strSQL = "INSERT INTO Table1 (id,name,lname) " & _
" VALUES ('" & lblID.Text & "','" & lblName.Text & "','" & lbllname.Text & "') "
objCmd = New OleDbCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
End If
Next
Me.pnlForm.Visible = False
Me.lblText.Text = "Record Inserted."
objConn.Close()
objConn = Nothing
End Sub

มันไม่ผ่านตรงภาพนะค่ะ ไม่ทราบว่าผิดตรงไหน ดูแบบมาจาก https://www.thaicreate.com/asp.net/asp.net-upload-excel-import-to-database.html เลย
Tag : .NET, Ms SQL Server 2008, Excel (Excel.Application), Web (ASP.NET)
|
|
 |
 |
 |
 |
Date :
2014-02-07 15:17:39 |
By :
^^ |
View :
882 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ชี้ที่ Error มันแจ้งว่าอะไรครับ
|
 |
 |
 |
 |
Date :
2014-02-07 15:39:52 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แบบนี้ค่ะ

|
 |
 |
 |
 |
Date :
2014-02-10 09:17:26 |
By :
^^ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้ค่ะๆๆ แต่พอรันแล้วเป็นแบบนี้ คิดว่าผิดตรงเรียกฐานข้อมูล นี้
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database/EVALUATION.mdb") & ";"
ข้างล่าง web.config แล้วจะแปลงมาแทนอันนี้อย่างไรค่ะ
และไฟล์ .xls เก็บไว้ที่ไหนค่ะ

web.config
Code (ASP)
<connectionStrings>
<!--<add name="EVALUATION" connectionString="Data Source=PA06;Initial Catalog=EVALUATION;Integrated Security=True"
providerName="System.DataClient" />-->
<add name="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;
 Data Source={0};Extended Properties='Excel 8.0;HDR={1}'" />
<add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;
 Data Source={0};Extended Properties='Excel 8.0;HDR={1}'" />
<add name="conString" connectionString="Data Source=PA06;Initial Catalog=Evaluation;Integrated Security=True" />
<add name="EvaluationConnectionString" connectionString="Server=PA06;Uid=sa;PASSWORD=;database=EVALUATION;Max Pool Size=400;Connect Timeout=600;"
providerName="System.Data.SqlClient" />
</connectionStrings>
|
 |
 |
 |
 |
Date :
2014-02-10 14:48:26 |
By :
^^ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้ save ได้แล้ว แต่.....มันไม่เรียกเปิดไฟล์งานค่ะ
ไม่ทราบว่าไฟล์นี้มันเก็บใน floder --> MyXls/Sheet1.xls นี้ไหมค่ะ แล้วต้องไง้ที่ไดรไหน งงนะค่ะ
|
 |
 |
 |
 |
Date :
2014-02-10 16:23:25 |
By :
^^ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|