从T-SQL中创建Excel的XLS格式文件
从T-SQL中创建Excel的XLS格式文件 T-SQL Excel XLS Create Excel XLS from T-SQLScript Rating Total number of votes [30] By: David A. Long This is a T-SQL script that uses OLE, ADO, Jet4 ISAM, and Linked Server to create and populate an Excel Wo
从T-SQL中创建Excel的XLS格式文件 T-SQL Excel XLSCreate Excel XLS from T-SQL Script Rating Total number of votes [30] By: David A. Long This is a T-SQL script that uses OLE, ADO, Jet4 ISAM, and Linked Server to create and populate an Excel Workbook (XLS) file from T-SQL query. If the Excel Worksheet exists, the query will append to the "table". The code is designed to be used by SQL Agent and to append to the step output with verbose and minimal detail. Code is pretty well commented, including some hard won knowledge about Jet4 ISAM, OLE, ADO, and usage of the Excel table from T-SQL -- Create XLS script DAL - 04/24/2003 -- -- Designed for Agent scheduling, turn on "Append output for step history" -- -- Search for %%% to find adjustable constants and other options -- -- Uses OLE for ADO and OLE DB to create the XLS file if it does not exist -- Linked server requires the XLS to exist before creation -- Uses OLE ADO to Create the XLS Worksheet for use as a table by T-SQL -- Uses Linked Server to allow T-SQL access to XLS table -- Uses T-SQL to populate te XLS worksheet, very fast -- PRINT 'Begin CreateXLS script at '+RTRIM(CONVERT(varchar(24),GETDATE(),121))+' ' PRINT '' GO SET NOCOUNT ON DECLARE @Conn int -- ADO Connection object to create XLS , @hr int -- OLE return value , @src varchar(255) -- OLE Error Source , @desc varchar(255) -- OLE Error Description , @Path varchar(255) -- Drive or UNC path for XLS , @Connect varchar(255) -- OLE DB Connection string for Jet 4 Excel ISAM , @WKS_Created bit -- Whether the XLS Worksheet exists , @WKS_Name varchar(128) -- Name of the XLS Worksheet (table) , @ServerName nvarchar(128) -- Linked Server name for XLS , @DDL varchar(8000) -- Jet4 DDL for the XLS WKS table creation , @SQL varchar(8000) -- INSERT INTO XLS T-SQL , @Recs int -- Number of records added to XLS , @Log bit -- Whether to log process detail -- Init variables SELECT @Recs = 0 -- %%% 1 = Verbose output detail, helps find problems, 0 = minimal output detail , @Log = 1 -- %%% assign the UNC or path and name for the XLS file, requires Read/Write access -- must be accessable from server via SQL Server service account -- & SQL Server Agent service account, if scheduled SET @Path = 'C:\TEMP\Test_'+CONVERT(varchar(10),GETDATE(),112)+'.xls' -- assign the ADO connection string for the XLS creation SET @Connect = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+@Path+';Extended Properties=Excel 8.0' -- %%% assign the Linked Server name for the XLS population SET @ServerName = 'EXCEL_TEST' -- %%% Rename Table as required, this will also be the XLS Worksheet name SET @WKS_Name = 'People' -- %%% Table creation DDL, uses Jet4 syntax, -- Text data type = varchar(255) when accessed from T-SQL SET @DDL = 'CREATE TABLE '+@WKS_Name+' (SSN Text, Name Text, Phone Text)' -- %%% T-SQL for table population, note the 4 part naming required by Jet4 OLE DB -- INSERT INTO SELECT, INSERT INTO valueS, and EXEC sp types are supported -- Linked Server does not support SELECT INTO types SET @SQL = 'INSERT INTO '+@ServerName+'...'+@WKS_Name+' (SSN, Name, Phone) ' SET @SQL = @SQL+'SELECT au_id AS SSN' SET @SQL = @SQL+', LTRIM(RTRIM(ISNULL(au_fname,'''')+'' ''+ISNULL(au_lname,''''))) AS Name' SET @SQL = @SQL+', phone AS Phone ' SET @SQL = @SQL+'FROM pubs.dbo.authors' IF @Log = 1 PRINT 'Created OLE ADODB.Connection object' -- Create the Conn object EXEC @hr = sp_OACreate 'ADODB.Connection', @Conn OUT IF @hr <> 0 -- have to use <> as OLE / ADO can return negative error numbers BEGIN -- Return OLE error EXEC sp_OAGetErrorInfo @Conn, @src OUT, @desc OUT SELECT Error=convert(varbinary(4),@hr), Source=@src, Description=@desc RETURN END IF @Log = 1 PRINT char(9)+'Assigned ConnectionString property' -- Set a the Conn object's ConnectionString property -- Work-around for error using a variable parameter on the Open method EXEC @hr = sp_OASetProperty @Conn, 'ConnectionString', @Connect IF @hr <> 0 BEGIN -- Return OLE error EXEC sp_OAGetErrorInfo @Conn, @src OUT, @desc OUT SELECT Error=convert(varbinary(4),@hr), Source=@src, Description=@desc RETURN END IF @Log = 1 PRINT char(9)+'Open Connection to XLS, for file Create or Append' -- Call the Open method to create the XLS if it does not exist, can't use parameters EXEC @hr = sp_OAMethod @Conn, 'Open' IF @hr <> 0 BEGIN -- Return OLE error EXEC sp_OAGetErrorInfo @Conn, @src OUT, @desc OUT SELECT Error=convert(varbinary(4),@hr), Source=@src, Description=@desc RETURN END -- %%% This section could be repeated for multiple Worksheets (Tables) IF @Log = 1 PRINT char(9)+'Execute DDL to create '''+@WKS_Name+''' worksheet' -- Call the Execute method to Create the work sheet with the @WKS_Name caption, -- which is also used as a Table reference in T-SQL -- Neat way to define column data types in Excel worksheet -- Sometimes converting to text is the only work-around for Excel's General -- Cell formatting, even though the Cell contains Text, Excel tries to format -- it in a "Smart" way, I have even had to use the single quote appended as the -- 1st character in T-SQL to force Excel to leave it alone EXEC @hr = sp_OAMethod @Conn, 'Execute', NULL, @DDL, NULL, 129 -- adCmdText + adExecuteNoRecords -- 0x80040E14 for table exists in ADO IF @hr = 0x80040E14 -- kludge, skip 0x80042732 for ADO Optional parameters (NULL) in SQL7 OR @hr = 0x80042732 BEGIN -- Trap these OLE Errors IF @hr = 0x80040E14 BEGIN PRINT char(9)+''''+@WKS_Name+''' Worksheet exists for append' SET @WKS_Created = 0 END SET @hr = 0 -- ignore these errors END IF @hr <> 0 BEGIN -- Return OLE error EXEC sp_OAGetErrorInfo @Conn, @src OUT, @desc OUT SELECT Error=convert(varbinary(4),@hr), Source=@src, Description=@desc RETURN END IF @Log = 1 PRINT 'Destroyed OLE ADODB.Connection object' -- Destroy the Conn object, +++ important to not leak memory +++ EXEC @hr = sp_OADestroy @Conn IF @hr <> 0 BEGIN -- Return OLE error EXEC sp_OAGetErrorInfo @Conn, @src OUT, @desc OUT SELECT Error=convert(varbinary(4),@hr), Source=@src, Description=@desc RETURN END -- Linked Server allows T-SQL to access the XLS worksheet (Table) -- This must be performed after the ADO stuff as the XLS must exist -- and contain the schema for the table, or worksheet IF NOT EXISTS(SELECT srvname from master.dbo.sysservers where srvname = @ServerName) BEGIN IF @Log = 1 PRINT 'Created Linked Server '''+@ServerName+''' and Login' EXEC sp_addlinkedserver @server = @ServerName , @srvproduct = 'Microsoft Excel Workbook' , @provider = 'Microsoft.Jet.OLEDB.4.0' , @datasrc = @Path , @provstr = 'Excel 8.0' -- no login name or password are required to connect to the Jet4 ISAM linked server EXEC sp_addlinkedsrvlogin @ServerName, 'false' END -- Have to EXEC the SQL, otherwise the SQL is evaluated -- for the linked server before it exists EXEC (@SQL) PRINT char(9)+'Populated '''+@WKS_Name+''' table with '+CONVERT(varchar,@@ROWCOUNT)+' Rows' -- %%% Optional you may leave the Linked Server for other XLS operations -- Remember that the Linked Server will not create the XLS, so remove it -- When you are done with it, especially if you delete or move the file IF EXISTS(SELECT srvname from master.dbo.sysservers where srvname = @ServerName) BEGIN IF @Log = 1 PRINT 'Deleted Linked Server '''+@ServerName+''' and Login' EXEC sp_dropserver @ServerName, 'droplogins' END GO SET NOCOUNT OFF PRINT '' PRINT 'Finished CreateXLS script at '+RTRIM(CONVERT(varchar(24),GETDATE(),121))+' ' GO

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

如果在打开一份需要打印的文件时,在打印预览里我们会发现表格框线不知为什么消失不见了,遇到这样的情况,我们就要及时进行处理,如果你的打印文件里也出现了此类的问题,那么就和小编一起来学习下边的课程吧:excel打印表格框线消失怎么办?1、打开一份需要打印的文件,如下图所示。 2、选中所有需要的内容区域,如下图所示。 3、单击鼠标右键,选择“设置单元格格式”选项,如下图所示。 4、点击窗口上方的“边框”选项,如下图所示。 5、在左侧的线条样式中选择细实线图样,如下图所示。 6、选择“外边框”

在日常办公中经常使用Excel来处理数据,时常遇到需要使用“筛选”功能。当我们在Excel中选择执行“筛选”时,对于同一列而言,最多只能筛选两个条件,那么,你知道excel同时筛选3个以上关键词该怎么操作吗?接下来,就让小编为大家演示一遍。第一种方法是将条件逐步添加到筛选器中。如果要同时筛选出三个符合条件的明细,首先需要逐步筛选出其中一个。开始时,可以先根据条件筛选出姓“王”的员工。然后单击【确定】,接着在筛选结果中勾选【将当前所选内容添加到筛选器】。操作步骤如下所示。 同样,再次分别执行筛选

在我们日常的工作学习中,从他人处拷贝了Excel文件,打开进行内容添加或重新编辑后,再保存的有时候,有时会提示出现兼容性检查的对话框,非常的麻烦,不知道Excel软件,可不可改为正常模式呢?那么下面就由小编为大家带来解决这个问题的详细步骤,让我们一起来学习吧。最后一定记得收藏保存。1、打开一个工作表,在工作表的名称中显示多出来一个兼容模式,如图所示。2、在这个工作表中,进行了内容的修改后保存,结果总是弹出兼容检查器的对话框,很麻烦看见这个页面,如图所示。 3、点击Office按钮,点另存为,然

在处理数据时,有时我们会遇到数据包含了倍数、温度等等各种符号的时候,你知道excel上标应该如何设置吗?我们在使用excel处理数据时,如果不会设置上标,这可是会让我们的很多数据在录入时就会比较麻烦。今天小编就为大家带来了excel上标的具体设置方法。1.首先,让我们打开桌面上的MicrosoftOfficeExcel文档,选择需要修改为上标的文字,具体如图所示。2.然后,点击右键,在点击后出现的菜单中,选择“设置单元格格式”选项,具体如图所示。3.接下来,在系统自动弹出来的“单元格格式”对话框

在电脑中删除或解压缩文件夹,时有时候会弹出提示对话框“错误0x80004005:未指定错误”,如果遇到这中情况应该怎么解决呢?提示错误代码0x80004005的原因其实有很多,但大部分因为病毒导致,我们可以重新注册dll来解决问题,下面,小编给大伙讲解0x80004005错误代码处理经验。有用户在使用电脑时出现错误代码0X80004005的提示,0x80004005错误主要是由于计算机没有正确注册某些动态链接库文件,或者计算机与Internet之间存在不允许的HTTPS连接防火墙所引起。那么如何

大部分用户使用Excel都是用来处理表格数据的,其实Excel还有vba程序编写,这个除了专人士应该没有多少用户用过此功能,在vba编写时常常会用到iif函数,它其实跟if函数的功能差不多,下面小编给大家介绍下iif函数的用法。Excel中SQL语句和VBA代码中都有iif函数。iif函数和excel工作表中的IF函数用法相似,执行真假值判断,根据逻辑计算的真假值,返回不同结果。IF函数用法是(条件,是,否)。VBA中的IF语句和IIF函数,前者IF语句是控制语句可以根据条件执行不同的语句,后者

在软件的学习中,我们习惯用excel,不仅仅是因为需要方便,更因为它可以满足多种实际工作中需要的格式,而且excel运用起来非常的灵活,有种模式是方便阅读的,今天带给大家的就是:excel阅读模式在哪里设置。1、打开电脑,然后再打开Excel应用,找到目标数据。2、要想在Excel中,设置阅读模式,有两种方式。第一种:Excel中,有大量的便捷处理方式,分布在Excel中布局中。在Excel的右下角,有设置阅读模式的快捷方式,找到十字标志的图案,点击即可进入阅读模式,在十字标志的右边有一个小的三

1、打开PPT,翻页至需要插入excel图标的页面。点击插入选项卡。2、点击【对象】。3、跳出以下对话框。4、点击【由文件创建】,点击【浏览】。5、选择需要插入的excel表格。6、点击确定后跳出如下页面。7、勾选【显示为图标】。8、点击确定即可。
