SQL Server: Export data to Excel
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 697   1
URL.หัวข้อ / URL
SQL Server: Export data to Excel

เป็นโค้ดสำหรับส่งข้อมูลจาก SQL Server ไปเป็น Excel ไฟล์
ถามไว้ที่ http://www.sqlservercentral.com/forum/topic.asp?TOPIC_ID=15683&FORUM_ID=9&CAT_ID=1&Topic_Title=Export+to+Excel+without+DTS%2E+Possible%3F&Forum_Title=General

ผมเห็นว่าน่าสนใจดี เลยนำมาฝากให้ลองนำไปใช้ดูครับ
โดยจะต้อง
1. สร้างไฟล์ Excel ชื่อ Success.xls ไว้ที่ c:\ (หรือห้องอื่นก็ แต่ต้องมาเปลี่ยนโค้ดข้างล่างด้วย)
2. เปิด SQL Query Analyzer แล้ว pasted โค้ดข้างล่างนี้ลงไป
3. ลอง Run ดู แล้วไปเปิดไฟล์ในข้อ 1 ดูว่ามีข้อมูลหรือไม่

-- Inputs:File name of the excel document.
-- Returns:The query result, written directly in Excel document!
DECLARE @err_desc varchar(255)
DECLARE @i int
DECLARE @xls_cell varchar(100)
DECLARE @OProp varchar(100)
DECLARE @O_XLS int
DECLARE @ret_val int
DECLARE @xls int
DECLARE @file varchar(255)
SET @i=0
EXEC @ret_val = sp_OACreate 'Excel.Application', @O_XLS OUTPUT, 4
IF @ret_val <> 0 or @@Error <> 0 GOTO com_handler
EXEC @ret_val = sp_OAMethod @O_XLS, 'Workbooks.Open', @xls OUTPUT,'C:\SUCCESS.xls'
-- in case you are creating new Excel file change the upper row with the following
--EXEC @ret_val = sp_OAMethod @O_XLS, 'Workbooks.Add', @xls OUTPUT
IF @ret_val <> 0 or @@Error <> 0 GOTO com_handler
SET @file = 'C:\SUCCESS.xls'
DECLARE authors_cursor CURSOR FOR
SELECT au_lname FROM authors
order by au_lname asc
OPEN authors_cursor
-- Perform the first fetch.
FETCH NEXT FROM authors_cursor
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0

BEGIN
-- This is executed as long as the previous fetch succeeds.
set @i=@i+1
set @OProp='Application.ActiveWorkbook.Sheets(1).Cells(' + cast(@i as varchar(2)) + ', 1).value'
-- genaration of correct property addressing
EXEC @ret_val = sp_OASetProperty @O_XLS, @OProp, @xls_cell
-- set the value of the property
FETCH NEXT FROM authors_cursor into @xls_cell
--IF @ret_val <> 0 or @@Error <> 0 GOTO com_handler
END
CLOSE authors_cursor
DEALLOCATE authors_cursor
------------------------------๊๐เ้ ํเ ๐เแ๎๒เ๒เ ๑ ๊๓๐๑๎๐เ
EXEC @ret_val = sp_OAMethod @O_XLS, 'Application.ActiveWorkbook.Save',Null
-- in case you are creating new Excel file change the upper row with the following
--EXEC @ret_val = sp_OAMethod @O_XLS,'Application.ActiveWorkbook.SaveAs',Null, 'c:\success.xls'
IF @ret_val <> 0 or @@Error <> 0 GOTO com_handler
EXEC @ret_val = sp_OAMethod @O_XLS, 'Quit'
-- I've commented the following line deliberately. SEE NOTE AT THE END
--EXEC sp_OADestroy @O_XLS
GOTO Done
com_handler:
CLOSE authors_cursor
DEALLOCATE authors_cursor

EXEC sp_oageterrorinfo @O_XLS, @ret_val out, @err_desc out
-- the last parameter gives small textual description of the error. It was very usefull!!
select @ret_val, @err_desc
EXEC @ret_val = sp_OAMethod @O_XLS, 'Quit'
EXEC sp_OADestroy @O_XLS
GOTO Done
Done:
-- I've commented the last line deliberately: It was causing the whole automation process in SQL to stop
-- so no futher automation instances could be created until the server was restarted. I read in MSDN that the instance
-- should be destroyed right after the SQL batch is completed, that is why I consider the last row not neccessary. BUT
-- you should check it yourself
--EXEC sp_OAStop

หมายเหตุ ถ้าจะนำไปใช้กับตารางอื่น ก็ให้เปลี่ยนตรงโค้ดสีน้ำเงิน และตรงโค้ดสีแดงของผมต้อง remark (--) ออก เพราะมัน error เมื่อผมลองทดสอบดูครับ

1 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R06802
   
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0429s