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 > บทความจากสมาชิก > อ่าน Excel ง่ายๆ ด้วย ExcelDataReader รองรับ xls, xlsx โดยไม่ต้องติดตั้ง Ms Office (VB.Net, C#)


 

อ่าน Excel ง่ายๆ ด้วย ExcelDataReader รองรับ xls, xlsx โดยไม่ต้องติดตั้ง Ms Office (VB.Net, C#)

อ่าน Excel ง่ายๆ ด้วย ExcelDataReader รองรับ xls, xlsx โดยไม่ต้องติดตั้ง Ms Office (VB.Net, C#) หลายคนที่เขียน .Net Application คงจะเจอปัญหาเกี่ยวกับการใช้งาน Excel เช่น การอ่าน Excel มาใช้บน Application ซึ่งในบทความหลาย ๆ บทความทางแอดมินได้แนะนำให้ใช้กับ Interop.Excel โดย Library ตัวนี้จะมาพร้อมกับ Microsoft Office Excel และการใช้งานก็จำเป็นจะต้องติดตั้งที่เครื่องของ Server ด้วย แต่ปัญหาจากการใช้งาน Library ตัวนี้ก็มีตามมามากเช่นเดียวกัน เช่น ปัญหาเรื่องการเซ็ตค่า Permission การใช้งานกับเวอร์ชั่นต่าง ๆ มักจะมีปัญหา และปัญหาที่สำคัญที่เจอบ่อยคือ มี Process ของ Excel ค้างที่อยู่เครื่อง Server เมื่อเรียกใช้งานบ่อย ๆ อาจจะทำให้เครื่อง Server ทำงานช้า หรือ Excel เกิดค้างจนไม่สามารถทำงานต่อไปได้



ExcelDataReader รองรับ Microsoft Excel files (97-2007)


สำหรับ Library ของ ExcelDataReader ได้เปิดตัวให้มาใช้กันซะพักแล้ว ผมเองก็ได้ใช้มันไปกับหลาย Project และที่ผ่านมาก็สามารถทำงานได้ดี อ่านได้เรวดเร็ว รองรับไฟล์ที่เวอร์ชั่น 2010 หรือ 2013 ได้อย่างไม่มีปัญหา สามารถใช้งานกับ .Net Framework ในเวอร์ชันใหม่ ๆ อย่างไม่มีปัญหา เช่นปัจจุบันใช้กับเวอร์ชั้น .Net 4.5

Download!!
https://exceldatareader.codeplex.com/


How to use

C#
01.FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
02. 
03.//1. Reading from a binary Excel file ('97-2003 format; *.xls)
04.IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
05.//...
06.//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
07.IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
08.//...
09.//3. DataSet - The result of each spreadsheet will be created in the result.Tables
10.DataSet result = excelReader.AsDataSet();
11.//...
12.//4. DataSet - Create column names from first row
13.excelReader.IsFirstRowAsColumnNames = true;
14.DataSet result = excelReader.AsDataSet();
15. 
16.//5. Data Reader methods
17.while (excelReader.Read())
18.{
19.    //excelReader.GetInt32(0);
20.}
21. 
22.//6. Free resources (IExcelDataReader is IDisposable)
23.excelReader.Close();

VB.Net
01.Dim stream As FileStream = File.Open(filePath, FileMode.Open, FileAccess.Read)
02. 
03.'1. Reading from a binary Excel file ('97-2003 format; *.xls)
04.Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateBinaryReader(stream)
05.'...
06.'2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
07.Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateOpenXmlReader(stream)
08.'...
09.'3. DataSet - The result of each spreadsheet will be created in the result.Tables
10.Dim result As DataSet = excelReader.AsDataSet()
11.'...
12.'4. DataSet - Create column names from first row
13.excelReader.IsFirstRowAsColumnNames = True
14.Dim result As DataSet = excelReader.AsDataSet()
15. 
16.'5. Data Reader methods
17.While excelReader.Read()
18.    'excelReader.GetInt32(0);
19.End While
20. 
21.'6. Free resources (IExcelDataReader is IDisposable)
22.excelReader.Close()




การเรียกใช้งาน ExcelDataReader สำหรับ ExcelDataReader สามารถติดตั้งและใช้งานฟรีได้จาก Manage NuGet Package

02

ติดตั้งจาก NuGet Package

03

ค้นหาจากชื่อ "ExcelDataReader" จากนั้นเลือก Install

04

Library ถูก Import เข้ามาเรียบร้อย

ตัวอย่างการอ่าน Excel มีตัวอย่างให้ทั้งของ VB.Net และ C#

05

ตัวอย่างไฟล์ของ Excel ที่อยู่ใน Project

01

ข้อมูลที่อยู่ใน Excel

Ex1 : อ่านข้อมูลจาก Excel ให้อยู่ในรูปแบบของ DataSet สามารถนำไปใช้กับ GridView หรือ Control อื่น ๆ ที่เรียกจาก DataSource ได้เลย

C#
01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Web;
05.using System.Web.UI;
06.using System.Web.UI.WebControls;
07.using System.IO;
08.using System.Data;
09.using Excel;
10. 
11.namespace ExcelReader
12.{
13.    public partial class myWebForm : System.Web.UI.Page
14.    {
15.        protected void Page_Load(object sender, EventArgs e)
16.        {
17.            using (FileStream stream = File.Open(Server.MapPath("Xls/myExcel.xlsx"), FileMode.Open, FileAccess.Read))
18.            {
19.                IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
20.                excelReader.IsFirstRowAsColumnNames = true;
21.                DataSet ds = excelReader.AsDataSet();
22. 
23.                this.myGridView.DataSource = ds.Tables[0];
24.                this.myGridView.DataBind();
25.            }
26. 
27.        }
28.    }
29.}

VB.Net
01.Imports System.IO
02.Imports System.Data
03.Imports Excel
04. 
05.Public Class myWebForm
06.    Inherits System.Web.UI.Page
07. 
08.    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
09.        Using stream As FileStream = File.Open(Server.MapPath("Xls/myExcel.xlsx"), FileMode.Open, FileAccess.Read)
10.            Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateOpenXmlReader(stream)
11.            excelReader.IsFirstRowAsColumnNames = True
12.            Dim ds As DataSet = excelReader.AsDataSet()
13. 
14.            Me.myGridView.DataSource = ds.Tables(0)
15.            Me.myGridView.DataBind()
16.        End Using
17.    End Sub
18. 
19.End Class

06

Ex2 : อ่านข้อมูลจาก Excel สามารถ Loop เพื่ออ่านแต่ล่ะรายการ เพื่อกำหนดคุณสมบัติอื่น ๆ

C#
01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Web;
05.using System.Web.UI;
06.using System.Web.UI.WebControls;
07.using System.IO;
08.using System.Data;
09.using Excel;
10. 
11.namespace ExcelReader
12.{
13.    public class ExcelColumn
14.    {
15.        public string Col1 { get; set; }
16.        public string Col2 { get; set; }
17.        public string Col3 { get; set; }
18.        public string Col4 { get; set; }
19.        public string Col5 { get; set; }
20.    }
21. 
22.    public partial class myWebForm : System.Web.UI.Page
23.    {
24.        protected void Page_Load(object sender, EventArgs e)
25.        {
26.            using (FileStream stream = File.Open(Server.MapPath("Xls/myExcel.xlsx"), FileMode.Open, FileAccess.Read))
27.            {
28.                IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
29.                excelReader.IsFirstRowAsColumnNames = true;
30.                var ls = new List<ExcelColumn>();
31.                while (excelReader.Read())
32.                {
33.                    //excelReader.GetString(0);
34.                    //excelReader.GetString(1);
35.                    //excelReader.GetString(2);
36.                    //excelReader.GetString(3);
37.                    //excelReader.GetString(4);
38.                    ls.Add(new ExcelColumn
39.                    {
40.                        Col1 = excelReader.GetString(0),
41.                        Col2 = excelReader.GetString(1),
42.                        Col3 = excelReader.GetString(2),
43.                        Col4 = excelReader.GetString(3),
44.                        Col5 = excelReader.GetString(4),
45.                    });
46.                }
47. 
48.                this.myGridView.DataSource = ls;
49.                this.myGridView.DataBind();
50.            }
51.        }
52.    }
53.}




VB.Net
01.Imports System.IO
02.Imports System.Data
03.Imports Excel
04. 
05.Public Class ExcelColumn
06.    Public Property Col1 As String
07.        Get
08.            Return m_Col1
09.        End Get
10.        Set
11.            m_Col1 = Value
12.        End Set
13.    End Property
14.    Private m_Col1 As String
15.    Public Property Col2() As String
16.        Get
17.            Return m_Col2
18.        End Get
19.        Set
20.            m_Col2 = Value
21.        End Set
22.    End Property
23.    Private m_Col2 As String
24.    Public Property Col3() As String
25.        Get
26.            Return m_Col3
27.        End Get
28.        Set
29.            m_Col3 = Value
30.        End Set
31.    End Property
32.    Private m_Col3 As String
33.    Public Property Col4() As String
34.        Get
35.            Return m_Col4
36.        End Get
37.        Set
38.            m_Col4 = Value
39.        End Set
40.    End Property
41.    Private m_Col4 As String
42.    Public Property Col5() As String
43.        Get
44.            Return m_Col5
45.        End Get
46.        Set
47.            m_Col5 = Value
48.        End Set
49.    End Property
50.    Private m_Col5 As String
51.End Class
52. 
53.Public Class myWebForm
54.    Inherits System.Web.UI.Page
55. 
56.    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
57.        Using stream As FileStream = File.Open(Server.MapPath("Xls/myExcel.xlsx"), FileMode.Open, FileAccess.Read)
58.            Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateOpenXmlReader(stream)
59.            excelReader.IsFirstRowAsColumnNames = True
60.            Dim ls = New List(Of ExcelColumn)()
61.            While excelReader.Read()
62.                'excelReader.GetString(0)
63.                'excelReader.GetString(1)
64.                'excelReader.GetString(2)
65.                'excelReader.GetString(3)
66.                'excelReader.GetString(4)
67.                ls.Add(New ExcelColumn() With {
68.                    .Col1 = excelReader.GetString(0),
69.                    .Col2 = excelReader.GetString(1),
70.                    .Col3 = excelReader.GetString(2),
71.                    .Col4 = excelReader.GetString(3),
72.                    .Col5 = excelReader.GetString(4)})
73.            End While
74. 
75.            Me.myGridView.DataSource = ls
76.            Me.myGridView.DataBind()
77.        End Using
78.    End Sub
79. 
80.End Class


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

  By : TC Admin
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2016-12-19
  Download : No files
Sponsored Links
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 อัตราราคา คลิกที่นี่