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,027

HOME > C# (.NET) > ASP.NET Excel (EPPlus) , VB.Net,C# > (C#) ASP.Net Excel & EPPlus Import Excel To Database



Clound SSD Virtual Server

(C#) ASP.Net Excel & EPPlus Import Excel To Database

(C#) ASP.Net Excel & EPPlus Import Excel To Database อีกหัวข้อหนึ่งที่ขาดไม่ได้เลยเกี่ยวกับการใช้ EPPlus เพื่อจัดการกับ Excel คือ การ Import ข้อมูลจาก Excel ลงใน Database ซึ่งวิธีที่จะใช้กันก็ไม่ได้แตกต่างไปจากหลักการทั่วๆ ไป คือใช้การ Loop ข้อมูลจาก Excel แล้วใช้คำสั่งในการ Insert ข้อมูล

Language Code : VB.NET || C#

Framework : 1,2,3,4

ในตัวอย่างนี้จะใช้ Database ของ SQL Server ในกรณีที่จะใช้ Database อื่น ๆ อ่านได้จากบทความนี้

C#,VB.Net กับ .Net Import Excel (xls,xlsx) to Database (SQL Server,MySQL,Oracle)


เริ่มกันเลย!!

ASP.NET Excel EPPlus

เป็นตัวอย่างไฟล์ Excel

ASP.NET Excel EPPlus

โครงสร้างของ Table แบบง่าย ๆ

Code (C#)

            FileInfo excel = new FileInfo(Server.MapPath(@"Xls/myData.xlsx"));
            using (var package = new ExcelPackage(excel))
            {
                var workbook = package.Workbook;

                //*** Sheet 1
                var worksheet = workbook.Worksheets.First();

                string strConnString = @"Server=localhost;UID=sa;PASSWORD=;database=mydatabase";
                var objConn = new SqlConnection(strConnString);
                objConn.Open();

                //*** Loop to Insert
                int totalRows = worksheet.Dimension.End.Row;
                for (int i = 2; i <= totalRows; i++)
                {
                    string strSQL = "INSERT INTO myTable (Column1,Column2,Column3,Column4,Column5) "
                        + " VALUES  ("
                        + " '" + worksheet.Cells[i, 1].Text.ToString() + "', "
                        + " '" + worksheet.Cells[i, 2].Text.ToString() + "', "
                        + " '" + worksheet.Cells[i, 3].Text.ToString() + "', "
                        + " '" + worksheet.Cells[i, 4].Text.ToString() + "', "
                        + " '" + worksheet.Cells[i, 5].Text.ToString() + "' "
                        + ")";
                    var objCmd = new SqlCommand(strSQL, objConn);
                    objCmd.ExecuteNonQuery();
                }

                objConn.Close();
            }


Screenshot

ASP.NET Excel EPPlus

ข้อมูลจเถูก Insert ลงใน Table

C#,VB.Net กับ .Net Import Excel (xls,xlsx) to Database (SQL Server,MySQL,Oracle)







   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2017-03-20 22:09:10 / 2017-03-20 23:08:09
  Download : No files
 Sponsored Links / Related

 
(C#) ASP.Net Excel & EPPlus Generate Excel file
Rating :

 
(C#) ASP.Net Excel & EPPlus Cell Ranges/Border
Rating :

 
(C#) ASP.Net Excel & EPPlus Cell styling (Color, Font, Alignments)
Rating :

 
(C#) ASP.Net Excel & EPPlus Open Excel and Write Excel (Template)
Rating :

 
(C#) ASP.Net Excel & EPPlus Read Excel/Cell file
Rating :

 
(C#) ASP.Net Excel & EPPlus Convert Excel DataTable
Rating :

 
(C#) ASP.Net Excel & EPPlus Import Excel To Database
Rating :

 
(C#) ASP.Net Excel & EPPlus Generate Excel Report from Database
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
   







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