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

创建|存储过程

转贴,仅引用了 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 pso@vip.hr)') 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

 

时间: 2024-11-17 06:22:17

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

java pdf word-java 如何将pdf格式文件转为word格式文件

问题描述 java 如何将pdf格式文件转为word格式文件 java 如何将pdf格式文件转为word格式文件,内容包含中文 解决方案 参考:http://blog.csdn.net/zwx19921215/article/details/34439851 解决方案二: java使用IText生成PDF格式文件 解决方案三: smallpdf.com 解决方案四: 你是不是只是转个文件格式?推荐smallpdf.com 解决方案五: http://bbs.csdn.net/topics/390

win7操作系统把pdf格式文件转换成ppt格式的方法

  大家都知道,pdf格式的文件采用了图文格式,但是一些用户想要把pdf格式文件转换成ppt格式,让文件表达的更加生动,Power Point简称ppt,是一种演示文稿图形程序.那么win7操作系统如何把pdf格式文件转换成ppt格式?或者其他格式呢?比如TXT.Word等等.今天小编来给大家介绍win7操作系统把pdf格式文件转换成ppt格式的方法. 具体方法如下: 1.下载安装pdf转换成ppt转换器工具,大家可以直接百度搜索,网络上有相当多的下载资源; 2.双击快捷方式,进入主界面后,我们

win7系统下pdf格式文件怎么转换成excel表格格式

  win7系统下pdf格式文件怎么转换成excel表格格式?有时候由于工作需要会把pdf格式文件转换成excel表格格式,但是不知道怎么设置,如果照着原文重新输入一遍是非常浪费时间的,而且还容易出错,有什么简单方法可以让pdf转excel表格格式吗?方法当然是有的,下面小编给大家介绍win7系统下pdf格式文件转换成excel表格格式的方法. 具体方法如下: 1.首先安装一个pdf格式转换工具到电脑中; 2.下载安装好转换软件之后,双击软件的快捷图标打开软件,选择要转换的文件格式; 3.接着把

win7打开PDF格式文件会提示数字越界怎么办?

  现在的工作,已经开始越来越数字化了,这也就要求咱们需要越发的了解电脑,了解专业的技术,因为现在的很多工作,特别是做文职工作的,一定要学会使用办公软件,当然,办公软件也已经不仅仅局限于之前的word.excel文件了,例如咱们最新的PS.PW.PDF等软件,也是需要熟练掌握的.而今天,小编就要以win7 旗舰版为例,为大家介绍一个关于PDF文件的操作技巧.在win7 旗舰版中,如果打开PDF格式文件时,电脑提示数字越界的话,咱们要如何处理? 第一种情况:如果在win7旗舰版中打开PDF格式文件

请问怎样将pdf格式文件转换成html5

问题描述 请问怎样将pdf格式文件转换成html5 以前知道JACOB可以将PDF,Word,Excel转换成html,但是有能转换成html5的实现方法吗?(之前访问过一个叫crocodoc的国外网站,不知它是怎么实现的),请各位大神答救!!!!

阅读器-.net 打开PDF格式文件

问题描述 .net 打开PDF格式文件 在winform 项目中,不需要用户下载安装任何阅读器的情况下,如何打开PDF格式文件 解决方案 Chrome的内核WebKit.net原生支持pdf显示,另外也可以webbrowser+pdf.js 解决方案二: 当接收或是下载了pdf格式的文件,如果电脑上没有相关联程序的话文件是无法打开查看的.其实pdf这种格式文件也是一直文档格式,可以打开查看也可以被编辑的.那怎样打开pdf文件呢. 只是阅读pdf文件内容可以用很多工具来打开查看pdf,在电脑上如果

vss2005 打开pdf问题-vss 2005 打开pdf格式文件时显示文件损坏,无法打开

问题描述 vss 2005 打开pdf格式文件时显示文件损坏,无法打开 我自己搭建了一个VSS2005文件管理系统,其他基本上没有什么问题,就是在签入pdf文件后,在vss上进行view操作时,显示文件损坏,打不开:后我将文件签出到本地电脑打开时也是这样. 百度了一下,说是微软的bug,需要下载补丁,安装补丁之后,重新签入新的pdf文件,进行上述操作时还是显示文件损坏. 在服务器端和客户端的文件类型中的二进制文件,我也添加了*.pdf,过后也还是不管用. 请大家帮忙看一下,折腾了好几天了,谢谢!

如何实现通过点击LINKBUUTON(文件名)打开PDF格式文件,PDF文件要求在另一个页面上显示!

问题描述 protectedvoidLinkButton1_Click(objectsender,EventArgse){LinkButtonl=(LinkButton)sender;stringll=l.Text;stringFileName;Response.Clear();Response.ContentType="application/pdf";Response.AddHeader("content-disposition","inline;at

平台-网页转pdf格式文件时出现字体异常

问题描述 网页转pdf格式文件时出现字体异常 Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized(iTextAsian.jar 异常) ,asp.net平台,如何进行处理.