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 > ASP.NET > ASP.NET Excel.Application > ASP.NET Export Database to Excel (Excel Application)



Clound SSD Virtual Server

ASP.NET Export Database to Excel (Excel Application)

ASP.NET Export Database to Excel (Excel Application) เป็นการส่งออกข้อมูลจากฐานข้อมูลออกในรูปแบบของไฟล์ Excel (*.xls) ในตัวอย่างผมใช้ฐานข้อมูล Microsoft Access ผ่าน System.Data.OleDb โดยผู้ใช้สามารถเลือกรายการจาก Checkbox เพื่อเลือกจำนวน Record

Framework 1.1,2.0,3.0,4,0

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


Syntax

Dim xlApp As New Excel.Application


AspNetExportDatabaseToExcel.aspx

<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Import Namespace="Excel"%>
<%@ Import Namespace="System.IO"%>
<%@ Page Language="VB" %>
<script runat="server"> 
	
	Dim objConn As OleDbConnection
	Dim objCmd As OleDbCommand
	

    Sub Page_Load(sender As Object, e As EventArgs)
		Dim strConnString As String
		strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";"
		objConn = New OleDbConnection(strConnString)
		objConn.Open()

		IF NOT Page.IsPostBack() Then
			BindData()
		End IF
    End Sub

	Sub BindData()
		Dim strSQL As String
		strSQL = "SELECT * FROM customer"

		Dim dtReader As OleDbDataReader
		objCmd = New OleDbCommand(strSQL, objConn)
		dtReader = objCmd.ExecuteReader()
		
		'*** BindData to Repeater ***'
		myRepeater.DataSource = dtReader
		myRepeater.DataBind()

		dtReader.Close()
		dtReader = Nothing

	End Sub

	Sub Page_UnLoad()
		objConn.Close()
		objConn = Nothing
	End Sub


    Sub Button1_Click(sender As Object, e As EventArgs)

        Dim FileName As String = "MyXls/MyExcel.xls"

        '*** Create Excel.Application ***'
        Dim xlApp As New Excel.Application
        Dim xlSheet1 As Excel.Worksheet
        Dim xlBook As Excel.Workbook

		xlBook = xlApp.Workbooks.Add()

		xlBook.Application.Visible = False

        '*** Create Sheet 1 ***'
        xlSheet1 = xlBook.Worksheets(1)
        xlSheet1.Name = "My Sheet1"

		'*** Header ***'
		With xlApp.ActiveSheet.Cells(1,1)
			.Value = "CustomerID"
		End With
		With xlApp.ActiveSheet.Cells(1,2)
			.Value = "Name"
		End With
		With xlApp.ActiveSheet.Cells(1,3)
			.Value = "Email"
		End With
		With xlApp.ActiveSheet.Cells(1,4)
			.Value = "CountryCode"
		End With
		With xlApp.ActiveSheet.Cells(1,5)
			.Value = "Budget"
		End With
		With xlApp.ActiveSheet.Cells(1,6)
			.Value = "Used"
		End With
		'***********'

        Dim chkCusID As System.Web.UI.WebControls.CheckBox
        Dim lblID As System.Web.UI.WebControls.Label
		Dim lblCustomerID,lblName,lblEmail,lblCountryCode,lblBudget,lblUsed As System.Web.UI.WebControls.Label
        Dim i,intRows As Integer
		
		intRows = 2

        For i = 0 To myRepeater.Items.Count - 1
            chkCusID = myRepeater.Items(i).FindControl("chkCustomerID")
            lblCustomerID = myRepeater.Items(i).FindControl("lblCustomerID")
			lblName = myRepeater.Items(i).FindControl("lblName")
			lblEmail = myRepeater.Items(i).FindControl("lblEmail")
			lblCountryCode = myRepeater.Items(i).FindControl("lblCountryCode")
			lblBudget = myRepeater.Items(i).FindControl("lblBudget")
			lblUsed = myRepeater.Items(i).FindControl("lblUsed")
            IF chkCusID.Checked = True Then

				'*** Detail ***'
				With xlApp.ActiveSheet.Cells(intRows,1)
					.Value = lblCustomerID.Text
				End With

				With xlApp.ActiveSheet.Cells(intRows,2)
					.Value = lblName.Text
				End With

				With xlApp.ActiveSheet.Cells(intRows,3)
					.Value = lblEmail.Text
				End With

				With xlApp.ActiveSheet.Cells(intRows,4)
					.Value = lblCountryCode.Text
				End With

				With xlApp.ActiveSheet.Cells(intRows,5)
					.Value = lblBudget.Text
				End With

				With xlApp.ActiveSheet.Cells(intRows,6)
					.Value = lblUsed.Text
				End With
				intRows = intRows + 1
            End IF
        Next

        '*** If Files Already Exist Delete files ***'
		Dim MyFile As New FileInfo(Server.MapPath(FileName))
		If MyFile.Exists Then
			MyFile.Delete()
		End IF
		MyFile = Nothing

        '*** Save Excel ***'
        'xlSheet1.PrintOut 1 '*** Print to printer ***'
        xlSheet1.SaveAs(Server.MapPath(FileName))
        xlApp.Quit()
		'System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet1)
		'System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
		'System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)

        '*** Quit and Clear Object ***'
        xlSheet1 = Nothing
        xlBook = Nothing
        xlApp = Nothing

		Me.lblText.Text = "Excel Created <a href="& FileName & ">Click here</a> to Download."
    End Sub

</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Excel Application</title>
</head>
<body>
	<form id="form1" runat="server">
	<table border="1">
    <asp:Repeater id="myRepeater" runat="server">
	<HeaderTemplate>		
			<tr>
				<th>Select</th>
				<th>CustomerID</th>
				<th>Name</th>
				<th>Email</th>
				<th>CountryCode</th>
				<th>Budget</th>
				<th>Used</th>
			</tr>
	</HeaderTemplate>
	<ItemTemplate>
		<tr>
			<td align="center"><asp:CheckBox id="chkCustomerID" runat="server">
			</asp:CheckBox></td>			
			<td align="center"><asp:Label id="lblCustomerID" runat="server" Text='<%#Container.DataItem("CustomerID") %>'>
			</asp:Label></td>
			<td><asp:Label id="lblName" runat="server" Text='<%#Container.DataItem("Name") %>'>
			</asp:Label></td>
			<td><asp:Label id="lblEmail" runat="server" Text='<%#Container.DataItem("Email") %>'>
			</asp:Label></td>
			<td align="center"><asp:Label id="lblCountryCode" runat="server" Text='<%#Container.DataItem("CountryCode") %>'>
			</asp:Label></td>
			<td align="right"><asp:Label id="lblBudget" runat="server" Text='<%#Container.DataItem("Budget") %>'>
			</asp:Label></td>
			<td align="right"><asp:Label id="lblUsed" runat="server" Text='<%#Container.DataItem("Used") %>'>
			</asp:Label></td>
		</tr>		
	</ItemTemplate>	
	</asp:Repeater>
	</table>
	<br />
	<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Submit"></asp:Button>
	<hr />
	<asp:Label id="lblText" runat="server"></asp:Label>
	</form>
</body>
</html>









Screenshot

ASP.NET Excel

ASP.NET Excel

   
Share


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


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


   


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







Load balance : Server 03
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 อัตราราคา คลิกที่นี่