65.9K
CodeProject 正在变化。 阅读更多。
Home

当 SQL Server 未安装 Excel 时,使用 DTS 格式化 Excel 工作表

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.47/5 (5投票s)

2007 年 12 月 31 日

CPOL
viewsIcon

25265

当 SQL Server 未安装 Excel 时,使用 DTS 格式化 Excel 工作表

引言

本文档帮助我们理解如何从 DTS 包中格式化 Excel 表格。通常,如果安装了 Microsoft Excel,可以使用 VBScript 实现此目的。

本文档旨在展示如何在 SQL Server 上未安装 Excel 的情况下,对电子表格进行额外的格式化。

Using the Code

以下 ActiveX 脚本展示了如何使用模板文件通过数据泵任务格式化 Excel 表格。

第一步,创建一个预先格式化的模板文件。这就是我们想要的样子。

将模板文件复制到所需的输出位置,然后使用数据泵任务导出数据。

myTemplate.JPG

以下代码从位于“Pubs”数据库中的“publishers”表中提取数据...

'**********************************************************************
'  Visual Basic ActiveX Script
'************************************************************************
Dim Rs
dim con
Dim sConString
dim strFile
dim FSO 
Dim oTask_create_table
dim oTask_select_query
dim pkg
dim connectionExcel
dim oStep_create_table 
DIM oStep_select_query
'-------------MAIN FUNCTION-----------------------
Function Main()
 dim Contract_number
 dim fso
 dim fold
 dim counter 
 dim fil
 dim rsContract 
 dim SqlString
 
   ' ----------create a new instance of an ADO Connection object--------- 
    Set con = CreateObject("ADODB.Connection")
    Set Rs  = CreateObject("ADODB.Recordset")
 set fso = CREATEOBJECT("Scripting.FileSystemObject")


 '-------------Connection String Settings------------------------
  sConString = "Provider=SQLOLEDB.1;Persist Security Info=True;_
	Trusted_Connection=Yes;Initial Catalog = _
	" & DTSGlobalVariables("gv_database").Value & ";_
	Data Source= "  & DTSGlobalVariables("gv_servername").Value  
  con.open sConString 
  SET pkg = DTSGlobalVariables.Parent
 
  SET oTask_select_query = pkg.Tasks("DTSTask_DTSDataPumpTask_1")   
  SET oStep_select_query = pkg.Steps("DTSStep_DTSDataPumpTask_1")    
  set connectionExcel = pkg.Connections("Microsoft Excel 97-2000")
  Create_summary_Sheet 
  con.close  
  Main = DTSTaskExecResult_Success
End Function

Function Create_summary_Sheet
 dim sSQLText
 dim x
 dim File_Name
 
 SET  rsColumnRemap  = CreateObject("ADODB.Recordset")
 set fso = CREATEOBJECT("Scripting.FileSystemObject")
 
 Set rs  = CreateObject("ADODB.Recordset")
 sSQLText = "select count('x')  'C'  from publishers "
 Rs.ActiveConnection = con 
 Rs.Source = sSQLText
 Rs.Open
 '-----If the no. of records is >0, then prepare excel sheet for that contract.
 
 if Rs.fields("C") > 0 then
  '**************COPY TEMPLATE FILE TO REPORT LOCATION*********************
  
  filename = fso.GetFolder(DTSGlobalVariables("gv_OutputExcelPath").Value)  _
	& "\Report.xls"
  fso.CopyFile  DTSGlobalVariables("gv_ReportTemplate").value,  filename  , true
  
  connectionExcel.DataSource = filename   
  sSQLText = "select * from publishers"
  oTask_select_query.Properties("SourceSQLStatement").Value = sSQLText      
  oTask_select_query.CustomTask.DestinationObjectName = "Sheet1$"    
  oStep_select_query.Execute   
    
 end if
 rs.Close
 
End Function

增强功能

以上代码只能为特定格式生成格式化的输出文件。我们无法一次性生成具有不同格式的输出文件。

为此,我们需要动态地重新映射源列和目标列。

© . All rights reserved.