Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > ASP.NET > ASP.NET Excel.Application > ASP.NET Upload Excel and Import to Database (Excel Application)


ASP.NET Upload Excel and Import to Database (Excel Application)

ASP.NET Upload Excel and Import to Database (Excel Application) ตัวอย่างนี้จะเป็นการเขียน Script เพื่อทำการ Upload ไฟล์ Excel (*.xls) แล้วทำการอัพโหลดข้อมูล ที่ได้จากการอัพโหลดเข้าสู่ฐานข้อมูลโดยผ่านคำสั่ง INSERT INTO

Framework 1.1,2.0,3.0,4,0

Excel for C# อ่านและดัดแปลงได้จากบทความนี้


Syntax

1.Dim xlApp As New Excel.Application


AspNetUploadExcelDatabase.aspx

001.<%@ Import Namespace="System.Data"%>
002.<%@ Import Namespace="System.Data.OleDb"%>
003.<%@ Import Namespace="Excel"%>
004.<%@ Page Language="VB" %>
005.<script language="VB" runat="server">
006. 
007.    Sub btnUpload_OnClick(sender As Object, e As EventArgs)
008.         
009.        If Not IsNothing(myFile1.PostedFile) Then
010.         
011.            Dim UlFileName As String
012.            Dim i As Integer
013. 
014.            UlFileName = "MyXls/" & System.IO.Path.GetFileName(myFile1.Value)
015.             
016.            '*** Save Images ***'
017.            myFile1.PostedFile.SaveAs(Server.MapPath(UlFileName))
018. 
019.            '*** Create Excel.Application ***'
020.            Dim xlApp As New Excel.Application
021.            Dim xlBook As Excel.Workbook
022.            Dim xlSheet1 As Excel.Worksheet
023. 
024.            xlBook = xlApp.Workbooks.Open(Server.MapPath(UlFileName))
025.            xlBook.Application.Visible = False
026. 
027.            xlSheet1 = xlBook.Worksheets(1)
028.             
029.            '*** Create DataTable ***'
030.            Dim dt As New System.Data.DataTable
031.            Dim dr As System.Data.DataRow
032. 
033.            '*** Column ***'
034.            dt.Columns.Add("CustomerID")
035.            dt.Columns.Add("Name")
036.            dt.Columns.Add("Email")
037.            dt.Columns.Add("CountryCode")
038.            dt.Columns.Add("Budget")
039.            dt.Columns.Add("Used")
040. 
041.            i = 2
042.            Do While Not Trim(xlSheet1.Cells.Item(i, 1).Value) = ""
043.                '*** Rows ***'
044.                dr = dt.NewRow
045.                dr("CustomerID") = xlSheet1.Cells.Item(i, 1).Value
046.                dr("Name") = xlSheet1.Cells.Item(i, 2).Value
047.                dr("Email") = xlSheet1.Cells.Item(i, 3).Value
048.                dr("CountryCode") = xlSheet1.Cells.Item(i, 4).Value
049.                dr("Budget") = xlSheet1.Cells.Item(i, 5).Value
050.                dr("Used") = xlSheet1.Cells.Item(i, 6).Value
051.                dt.Rows.Add(dr)
052.                i = i + 1
053.            Loop
054. 
055.            '*** End DataTable ***'
056.             
057.            '*** Insert to Database ***'
058.                Dim objConn As OleDbConnection
059.                Dim objCmd As OleDbCommand
060.                Dim strConnString As String
061.                Dim strSQL As String
062.                strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";"
063.                objConn = New OleDbConnection(strConnString)
064.                objConn.Open()
065. 
066.                For i = 0 To dt.Rows.Count - 1                  '*** Insert Record ***'
067.                    strSQL = "INSERT INTO customer2 (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
068.                    " VALUES ('" & dt.Rows(i)("CustomerID") & "','" & dt.Rows(i)("Name") & "' " & _
069.                    ",'" & dt.Rows(i)("Email") & "','" & dt.Rows(i)("CountryCode") & "' " & _
070.                    ",'" & dt.Rows(i)("Budget") & "','" & dt.Rows(i)("Used") & "') "
071.                    objCmd = New OleDbCommand(strSQL, objConn)
072.                    objCmd.ExecuteNonQuery()                   
073.                Next
074.             
075.            Me.lblText.Text = "Record Inserted."
076. 
077.            '*** Quit and Clear Object ***'
078.            objConn.Close()
079.            objConn = Nothing
080. 
081.            xlApp.Application.Quit()
082.            xlApp.Quit()
083.            xlSheet1 = Nothing
084.            xlBook = Nothing
085.            xlApp = Nothing
086.        End IF
087. 
088.    End Sub
089. 
090.</script>
091.<html>
092.<head>
093.<title>ThaiCreate.Com ASP.NET - Excel Application</title>
094.</head>
095.<body>
096.    <form id="form1" runat="server">
097.        <input id="myFile1" type="file" runat="server">
098.        <input id="btnUpload" type="button" OnServerClick="btnUpload_OnClick"  value="Upload" runat="server" />
099.        <hr />
100.        <asp:Label id="lblText" runat="server"></asp:Label>
101.    </form>
102.</body>
103.</html>
104.</form>




Screenshot

ASP.NET Excel

ASP.NET Excel

   
Hate it
Don't like it
It's ok
Like it
Love it
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2008-10-26 20:40:27 / 2017-03-29 11:35:24
  Download : Download  ASP.NET Upload Excel and Import to Database (Excel Application)
 Sponsored Links / Related

 
ASP.NET Excel Response.ContentType = "application/vnd.ms-excel"
Rating :

 
ASP.NET Config Excel (Excel Application)
Rating :

 
ASP.NET Connect to Excel Application
Rating :

 
ASP.NET Write Excel (Excel Application)
Rating :

 
ASP.NET Delete Sheet Excel (Excel Application)
Rating :

 
ASP.NET Write Excel Multiple Sheet (Excel Application)
Rating :

 
ASP.NET Write Excel Style & Format (Excel Application)
Rating :

 
ASP.NET Excel Merge Cell (Excel Application)
Rating :

 
ASP.NET Read Excel (Excel Application)
Rating :

 
ASP.NET Excel Cell Borders (Excel Application)
Rating :

 
ASP.NET Read Excel Multiple Sheet (Excel Application)
Rating :

 
ASP.NET Excel Draw Line (Excel Application)
Rating :

 
ASP.NET ADO.NET & Excel (OleDb)
Rating :

 
ASP.NET Excel Insert Picture (Excel Application)
Rating :

 
ASP.NET ADO.NET & Excel (Odbc)
Rating :

 
ASP.NET Excel Open Document (Excel Application)
Rating :

 
ASP.NET Import Excel to Database (Excel Application)
Rating :

 
ASP.NET Export Database to Excel (Excel Application)
Rating :

 
ASP.NET Export Database to Excel Report/Print Format (Excel Application)
Rating :


ThaiCreate.Com Forum
Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่