首页 > 开发 > 综合 > 正文

T-SQL 存储过程创建 PDF 格式文件(报表)

2024-07-21 02:07:40
字体:
来源:转载
供稿:网友

转贴,仅引用了 fso,pdf 格式可以自己写,就像标志语言:
creating a pdf from a stored procedure
http://www.sqlservercentral.com/columnists/mivica/creatingapdffromastoredprocedure.asp

 

不愿注册就贴出来:
creating a pdf from a stored procedure
regular columnist : m ivica
posted: 08/26/2003
more articles from this columnist
14089 reads

add article to your virtual briefcase  (what is this?)

article rating   total number of votes [183]  

this content sponsored by: is sql the center of your universe?
sqlcentric is a comprehensive web-based network database monitoring and alert system. - brought to you by pearl knowledge solutions, inc.
http://www.pearlknows.com

this article explains how to create a a stored procedure that will in turn create a simple column based report in pdf without using any external tools or libraries (and their associated licensing costs!).

sql2pdf makes a pdf report from text inserted in the table psopdf ( nvarchar(80) ). first a table named psopdf should be created.


create table psopdf (code nvarchar(80))

after that create the stored procedure sql2pdf.

sql2pdf.txt

and table psopdf has to be filled with your data as shown in examples below.
at the end the stored procedure is called using the file name only (not extension).


exec sql2pdf 'filename'

the result is in your c:/ directory.

example 1:
insert psopdf(code) select space(60) + 'company ltd'insert psopdf(code) select space(60) + 'company address'insert psopdf(code) select space(60) + 'street name & no'insert psopdf(code) select ' 'insert psopdf(code) select space(34) + 'bill of sale'insert psopdf(code) select ' 'insert psopdf(code) select 'product' + space(10) + 'quantity'+ space(10) + 'price' + space(10) + 'total'insert psopdf(code) select replace(space(56), ' ', '_')insert psopdf(code) select 'product1' + space(9) + '10.00 '+ space(10) + '52.30' + space(10) + '5230.0'insert psopdf(code) select 'product2' + space(9) + '2.00 '+ space(10) + '10.00' + space(10) + ' 20.0'insert psopdf(code) select replace(space(56), ' ', '_')insert psopdf(code) select space(50) + '5250.0'

after insert call the stored procedure with file name demo2.

exec sql2pdf 'demo2'

the result is in your c:/ directory.



example 2:
second example uses a database pubs.

use pubsinsert psopdf(code) select t1.au_lname + ' ' + t1.au_fname + ' ' + t1.phone + ' ' + t1.address + ' ' + t1.city + ' ' + t1.state + ' ' + t1.zip from authors t1, authors t2

after insert call the stored procedure with file name demo1.

exec sql2pdf 'demo1'

>the result is in your c:/ directory.



 

sql2pdf.txt

-- drop procedure sql2pdfcreate procedure sql2pdf @filename varchar(100) as create table #pdf (idnumber int identity(1,1) ,code nvarchar(200)) create table #xref (idnumber int identity(1,1) ,code varchar(30)) create table #text (idnumber int identity(1,1) ,code varchar(200)) declare @end varchar(7), @beg varchar(7), @a1 varchar(3), @a2 varchar(3), @ad varchar(5), @cr varchar(8), @pr varchar(9), @ti varchar(6), @xstr varchar(10), @page varchar(8000),@pdf varchar(100),@trenutnired nvarchar(200), @rows int, @ofset int, @len int, @nopg int, @fs int,@ole int,@x int,@file int, @object int select @pdf = 'c:/' + @filename + '.pdf' set @page = '' set @nopg = 0 set @object = 6 set @end = 'endobj' set @beg = ' 0 obj' set @a1 = '<<' set @a2 = '>>' set @ad = ' 0 r' set @cr = char(67) + char(114) + char (101) + char(97) + char(116) + char (111) + char(114) set @pr = char(80) + char(114) + char (111) + char(100) + char(117) + char (99 ) + char(101) + char(114) set @ti = char(84) + char(105) + char (116) + char(108) + char(101) set @xstr = ' 00000 n' set @ofset = 396 insert into #xref(code) values ('xref') insert into #xref(code) values ('0 10') insert into #xref(code) values ('0000000000 65535 f') insert into #xref(code) values ('0000000017' + @xstr) insert into #xref(code) values ('0000000790' + @xstr) insert into #xref(code) values ('0000000869' + @xstr) insert into #xref(code) values ('0000000144' + @xstr) insert into #xref(code) values ('0000000247' + @xstr) insert into #xref(code) values ('0000000321' + @xstr) insert into #xref(code) values ('0000000396' + @xstr) insert into #pdf (code) values ('%' + char(80) + char(68) + char (70) + '-1.2') insert into #pdf (code) values ('%佑佑') insert into #pdf (code) values ('1' + @beg) insert into #pdf (code) values (@a1) insert into #pdf (code) values ('/' + @cr + ' (ivica masar ' + char(80) + char(83) + char (79) + char(80) + char(68) + char (70) + ')') insert into #pdf (code) values ('/' + @pr + ' (stored procedure for ms sql [email protected])') insert into #pdf (code) values ('/' + @ti + ' (sql2' + char(80) + char(68) + char (70) + ')') insert into #pdf (code) values (@a2) insert into #pdf (code) values (@end) insert into #pdf (code) values ('4' + @beg) insert into #pdf (code) values (@a1) insert into #pdf (code) values ('/type /font') insert into #pdf (code) values ('/subtype /type1') insert into #pdf (code) values ('/name /f1') insert into #pdf (code) values ('/encoding 5' + @ad) insert into #pdf (code) values ('/basefont /courier') insert into #pdf (code) values (@a2) insert into #pdf (code) values (@end) insert into #pdf (code) values ('5' + @beg) insert into #pdf (code) values (@a1) insert into #pdf (code) values ('/type /encoding') insert into #pdf (code) values ('/baseencoding /winansiencoding') insert into #pdf (code) values (@a2) insert into #pdf (code) values (@end) insert into #pdf (code) values ('6' + @beg) insert into #pdf (code) values (@a1) insert into #pdf (code) values (' /font ' + @a1 + ' /f1 4' + @ad + ' ' + @a2 + ' /procset [ /' + char(80) + char(68) + char (70) + ' /text ]') insert into #pdf (code) values (@a2) insert into #pdf (code) values (@end) insert into #text(code) (select code from psopdf) select @x = count(*) from #text select @x = (@x / 60) + 1 while @nopg < @x begin declare syskursor insensitive scroll cursor for select substring((code + space(81)), 1, 80) from #text where idnumber between ((@nopg * 60) + 1) and ((@nopg + 1) * 60 ) for read only open syskursor fetch next from syskursor into @trenutnired select @object = @object + 1 select @page = @page + ' ' + cast(@object as varchar) + @ad select @len = len(@object) + len(@object + 1) insert into #pdf (code) values (cast(@object as varchar) + @beg) insert into #pdf (code) values (@a1) insert into #pdf (code) values ('/type /page') insert into #pdf (code) values ('/parent 3' + @ad) insert into #pdf (code) values ('/resources 6' + @ad) select @object = @object + 1 insert into #pdf (code) values ('/contents ' + cast(@object as varchar) + @ad) insert into #pdf (code) values (@a2) insert into #pdf (code) values (@end) select @ofset = @len + 86 + @ofset insert into #xref(code) (select substring('0000000000' + cast(@ofset as varchar), len('0000000000' + cast(@ofset as varchar)) - 9, len('0000000000' + cast(@ofset as varchar))) + @xstr) insert into #pdf (code) values (cast(@object as varchar) + @beg) insert into #pdf (code) values (@a1) select @object = @object + 1 insert into #pdf (code) values ('/length ' + cast(@object as varchar) + @ad) insert into #pdf (code) values (@a2) insert into #pdf (code) values ('stream') insert into #pdf (code) values ('bt') insert into #pdf (code) values ('/f1 10 tf') insert into #pdf (code) values ('1 0 0 1 50 802 tm') insert into #pdf (code) values ('12 tl') while @@fetch_status = 0 begin insert into #pdf (code) values ('t* (' + @trenutnired + ') tj') fetch next from syskursor into @trenutnired end insert into #pdf (code) values ('et') insert into #pdf (code) values ('endstream') insert into #pdf (code) values (@end) select @rows = (select count(*) from #text where idnumber between ((@nopg * 60) + 1) and ((@nopg + 1) * 60 ))* 90 + 45 select @nopg = @nopg + 1 select @len = len(@object) + len(@object - 1) select @ofset = @len + 57 + @ofset + @rows insert into #xref(code) (select substring('0000000000' + cast(@ofset as varchar), len('0000000000' + cast(@ofset as varchar)) - 9, len('0000000000' + cast(@ofset as varchar))) + @xstr) insert into #pdf (code) values (cast(@object as varchar) + @beg) insert into #pdf (code) values (@rows) insert into #pdf (code) values (@end) select @len = len(@object) + len(@rows) select @ofset = @len + 18 + @ofset insert into #xref(code) (select substring('0000000000' + cast(@ofset as varchar), len('0000000000' + cast(@ofset as varchar)) - 9, len('0000000000' + cast(@ofset as varchar))) + @xstr) close syskursor deallocate syskursor end insert into #pdf (code) values ('2' + @beg) insert into #pdf (code) values (@a1) insert into #pdf (code) values ('/type /catalog') insert into #pdf (code) values ('/pages 3' + @ad) insert into #pdf (code) values ('/pagelayout /onecolumn') insert into #pdf (code) values (@a2) insert into #pdf (code) values (@end) update #xref set code = (select code from #xref where idnumber = (select max(idnumber) from #xref)) where idnumber = 5 delete from #xref where idnumber = (select max(idnumber) from #xref) insert into #pdf (code) values ('3' + @beg) insert into #pdf (code) values (@a1) insert into #pdf (code) values ('/type /pages') insert into #pdf (code) values ('/count ' + cast(@nopg as varchar)) insert into #pdf (code) values ('/mediabox [ 0 0 595 842 ]') insert into #pdf (code) values ('/kids [' + @page + ' ]') insert into #pdf (code) values (@a2) insert into #pdf (code) values (@end) select @ofset = @ofset + 79 update #xref set code =(select substring('0000000000' + cast(@ofset as varchar), len('0000000000' + cast(@ofset as varchar)) - 9, len('0000000000' + cast(@ofset as varchar))) + @xstr) where idnumber = 6 insert into #xref(code) values ('trailer') insert into #xref(code) values (@a1) select @object = @object + 1 update #xref set code = '0 ' + cast(@object as varchar) where idnumber = 2 insert into #xref(code) values ('/size ' + cast(@object as varchar)) insert into #xref(code) values ('/root 2' + @ad) insert into #xref(code) values ('/info 1' + @ad) insert into #xref(code) values (@a2) insert into #xref(code) values ('startxref') select @len = len(@nopg) + len(@page) select @ofset = @len + 86 + @ofset insert into #xref(code) values (@ofset) insert into #xref(code) values ('%%' + char(69) + char (79) + char(70)) insert into #pdf (code) (select code from #xref) --select code from #pdf select @trenutnired = 'del '+ @pdf execute @ole = sp_oacreate 'scripting.filesystemobject', @fs out exec master..xp_cmdshell @trenutnired, no_output execute @ole = sp_oamethod @fs, 'opentextfile', @file out, @pdf, 8, 1 declare syskursor insensitive scroll cursor for select code from #pdf order by idnumber for read only open syskursor fetch next from syskursor into @trenutnired while @@fetch_status = 0begin execute @ole = sp_oamethod @file, 'writeline', null, @trenutnired fetch next from syskursor into @trenutnired end close syskursor deallocate syskursor delete from psopdf execute @ole = sp_oadestroy @file execute @ole = sp_oadestroy @fs

 
注册会员,创建你的web开发资料库,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表