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 > ASP Forum > ASP สร้าง ไฟล์ Excel จากฐานข้อมูล SQL Server2005 ด้วย asp classic ธรรมดา



 

ASP สร้าง ไฟล์ Excel จากฐานข้อมูล SQL Server2005 ด้วย asp classic ธรรมดา

 



Topic : 063554



โพสกระทู้ ( 13 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter



เรียนทุกท่านครับ


จากโค๊ดดังกล่าวผมสามารถประยุกต์กับ ตัว SQL Server ได้หรือเปล่า ครับ (ที่ highlight สีชมพูครับ)


<%@ Language=VBScript %>
02.
<%
03.
Option Explicit
04.
Response.Buffer = TRUE
05.
Response.ContentType = "application/vnd.ms-excel"
06.
%>
07.
<html>
08.
<head>
09.
</head>
10.
<body>
11.
<%
12.
[color=pink]Dim Conn,strSQL,objRec
13.
Set Conn = Server.Createobject("ADODB.Connection")
14.
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("MyXls/mydatabase.mdb"),"" , ""
15.
strSQL = "SELECT * FROM customer "
16.
Set objRec = Server.CreateObject("ADODB.Recordset")
17.
objRec.Open strSQL, Conn, 1,3
18.
%>
[/color]

19.
<table width="600" border="1">
20.
<tr>
21.
<th width="91"> <div align="center">CustomerID </div></th>
22.
<th width="98"> <div align="center">Name </div></th>
23.
<th width="198"> <div align="center">Email </div></th>
24.
<th width="97"> <div align="center">CountryCode </div></th>
25.
<th width="59"> <div align="center">Budget </div></th>
26.
<th width="71"> <div align="center">Used </div></th>
27.
</tr>
28.
<%
29.
While Not objRec.EOF
30.
%>
31.
<tr>
32.
<td><div align="center"><%=objRec.Fields("CustomerID").Value%></div></td>
33.
<td><%=objRec.Fields("Name").Value%></td>
34.
<td><%=objRec.Fields("Email").Value%></td>
35.
<td><div align="center"><%=objRec.Fields("CountryCode").Value%></div></td>
36.
<td align="right"><%=objRec.Fields("Budget").Value%></td>
37.
<td align="right"><%=objRec.Fields("Used").Value%></td>
38.
</tr>
39.
<%
40.
objRec.MoveNext
41.
Wend
42.
%>
43.
</table>
44.
<%
45.
objRec.Close()
46.
Conn.Close()
47.
Set objRec = Nothing
48.
Set Conn = Nothing
49.
%>
50.
</body>
51.
</html>


ขอบคุณครับ

Ponomy



Tag : ASP







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2011-07-21 21:15:56 By : Ponomy View : 1780 Reply : 1
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ดูตัวอย่างนี้ครับ

Code (ASP)
		Conn.Open "Driver={SQL Server};Server=host;Database=database;UID=user;PWD=password;"


เปลี่ยนแค่ Connection String ครับ

Code (ASP)
<%
		Dim Conn,strSQL,objRec
		Dim xlApp,xlBook,xlSheet1,FileName,intRows
		Dim Fso,MyFile

		Set Conn = Server.Createobject("ADODB.Connection")
		Conn.Open "Driver={SQL Server};Server=host;Database=database;UID=user;PWD=password;"
		strSQL = "SELECT * FROM customer "
		Set objRec = Server.CreateObject("ADODB.Recordset")
		objRec.Open strSQL, Conn, 1,3

		If Not objRec.EOF Then
			
				FileName = "MyXls/MyReport.xls"

				'*** Create Exce.Application ***'
				Set xlApp = Server.CreateObject("Excel.Application")
				Set xlBook = xlApp.Workbooks.Add

				'*** Create Sheet 1 ***'
				xlBook.Worksheets(1).Name = "My Customer"								
				xlBook.Worksheets(1).Select

				'*** Width & Height (A1:A1) ***'
				With xlApp.ActiveSheet.Range("A1:A1")
					.ColumnWidth = 10.0
				End With
				With xlApp.ActiveSheet.Range("B1:B1")
					.ColumnWidth = 13.0
				End With
				With xlApp.ActiveSheet.Range("C1:C1")
					.ColumnWidth = 23.0
				End With
				With xlApp.ActiveSheet.Range("D1:D1")
					.ColumnWidth = 12.0
				End With
				With xlApp.ActiveSheet.Range("E1:E1")
					.ColumnWidth = 13.0
				End With
				With xlApp.ActiveSheet.Range("F1:F1")
					.ColumnWidth = 12.0
				End With

				With xlApp.ActiveSheet.Range("A1:F1")
					.BORDERS.Weight = 1  
					.MergeCells = True	
					.Font.Bold = True
					.Font.Size = 20
					.HorizontalAlignment = -4108 
				End With
				With xlApp.ActiveSheet.Cells(1,1)
					.Value = "Customer Report"
				End With

				'*** Header ***'
				With xlApp.ActiveSheet.Cells(3,1)
					.Value = "CustomerID"
					.Font.Bold = True
					.VerticalAlignment = -4108 
					.HorizontalAlignment = -4108 
					.BORDERS.Weight = 1
				End With
				With xlApp.ActiveSheet.Cells(3,2)
					.Value = "Name"
					.Font.Bold = True
					.VerticalAlignment = -4108 
					.HorizontalAlignment = -4108
					.BORDERS.Weight = 1
				End With	
				With xlApp.ActiveSheet.Cells(3,3)
					.Value = "Email"
					.Font.Bold = True
					.VerticalAlignment = -4108 
					.HorizontalAlignment = -4108 
					.BORDERS.Weight = 1
				End With
				With xlApp.ActiveSheet.Cells(3,4)
					.Value = "CountryCode"
					.Font.Bold = True
					.VerticalAlignment = -4108 
					.HorizontalAlignment = -4108 
					.BORDERS.Weight = 1
				End With
				With xlApp.ActiveSheet.Cells(3,5)
					.Value = "Budget"
					.Font.Bold = True
					.VerticalAlignment = -4108 
					.HorizontalAlignment = -4108 
					.BORDERS.Weight = 1
				End With
				With xlApp.ActiveSheet.Cells(3,6)
					.Value = "Used"
					.Font.Bold = True
					.VerticalAlignment = -4108 
					.HorizontalAlignment = -4108 
					.BORDERS.Weight = 1
				End With
				'***********'
			
			intRows = 4
			While Not objRec.EOF
				
				'*** Detail ***'
				With xlApp.ActiveSheet.Cells(intRows,1)
					.Value = objRec.Fields("CustomerID").Value
					.BORDERS.Weight = 1
					.HorizontalAlignment = -4108
				End With
				With xlApp.ActiveSheet.Cells(intRows,2)
					.Value = objRec.Fields("Name").Value
					.BORDERS.Weight = 1
				End With
				With xlApp.ActiveSheet.Cells(intRows,3)
					.Value = objRec.Fields("Email").Value
					.BORDERS.Weight = 1
				End With
				With xlApp.ActiveSheet.Cells(intRows,4)
					.Value = objRec.Fields("CountryCode").Value
					.HorizontalAlignment = -4108 
					.BORDERS.Weight = 1
				End With
				With xlApp.ActiveSheet.Cells(intRows,5)
					.Value = objRec.Fields("Budget").Value
					.BORDERS.Weight = 1
				End With
				With xlApp.ActiveSheet.Cells(intRows,6)
					.Value = objRec.Fields("Used").Value
					.BORDERS.Weight = 1
				End With

				intRows = intRows + 1
				objRec.MoveNext
			Wend

			'*** If Files Already Exist Delete files ***'
			Set Fso = CreateObject("Scripting.FileSystemObject")
			If (Fso.FileExists(Server.MapPath(FileName))) Then
			   Set MyFile = Fso.GetFile(Server.MapPath(FileName))
			   MyFile.Delete
			End If

			'*** Save Excel ***'
			'xlBook.PrintOut 1 '*** Print to printer ***'
			xlBook.SaveAs Server.MapPath(FileName)
			xlApp.Application.Quit

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

		End If

			objRec.Close()
			Conn.Close()
			Set objRec = Nothing
			Set Conn = Nothing		

%>


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






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-07-23 09:06:17 By : webmaster
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ASP สร้าง ไฟล์ Excel จากฐานข้อมูล SQL Server2005 ด้วย asp classic ธรรมดา
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

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