Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««23456»»

How to Dynamically and Iteratively Populate An Excel Workbook from SQL Server Expand / Collapse
Author
Message
Posted Thursday, August 21, 2008 10:56 PM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, December 10, 2014 8:44 AM
Points: 49, Visits: 282
I have never found a way to replace data in an existing Excel file. The standard behavior is to append data. The only workaround is to have excel.exe on the server and use Interop. Too bad
 ' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic
' The ScriptMain class is the entry point of the Script Task.
Option Strict Off
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports Microsoft.Office.Interop.Excel

Public Class ScriptMain

' The execution engine calls this method when the task executes.
' To access the object model, use the Dts object. Connections, variables, events,
' and logging features are available as static members of the Dts class.
' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
'
' To open Code and Text Editor Help, press F1.
' To open Object Browser, press Ctrl+Alt+J.
Private _app As Microsoft.Office.Interop.Excel.Application
Private _books As Microsoft.Office.Interop.Excel.Workbooks
Private _book As Microsoft.Office.Interop.Excel.Workbook
Protected _sheets As Microsoft.Office.Interop.Excel.Sheets
Protected _sheet As Microsoft.Office.Interop.Excel.Worksheet

Public Sub Main()
' Add your code here
Try
Dim FileName As String
Dim Tab As String
FileName = CType(Dts.Variables("DestinationFile").Value, String)
Tab = CType(Dts.Variables("AccessTB").Value, String)
OpenExcelWorkbook(FileName)
_sheet = CType(_sheets(Tab), Microsoft.Office.Interop.Excel.Worksheet)
_sheet.Select(Type.Missing)
Dim range As Microsoft.Office.Interop.Excel.Range = _sheet.Rows("2:65536")
range.Delete(Microsoft.Office.Interop.Excel.XlDeleteShiftDirection.xlShiftUp)
NAR(range)
NAR(_sheet)
CloseExcelWorkbook()
NAR(_book)
_app.Quit()
NAR(_app)
Dts.TaskResult = Dts.Results.Success
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub

Protected Sub OpenExcelWorkbook(ByVal fileName As String)
'try
'{
_app = New Microsoft.Office.Interop.Excel.Application()
If _book Is Nothing Then
_books = _app.Workbooks
_book = _books.Open(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
_sheets = _book.Worksheets
End If
'}
'catch(Exception ex)
'{
' Console.WriteLine(ex.ToString());
'}

End Sub
Protected Sub CloseExcelWorkbook()
_book.Save()
_book.Close(False, Type.Missing, Type.Missing)
End Sub
Protected Sub NAR(ByVal o As Object)
Try
If Not o Is Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
End If
Finally
o = Nothing
End Try
End Sub
' To avoid the variable locking problem use this method to read package variables.
' No read variables need to be declared in the "ReadOnlyVariables" list of the script
' component if this method is used inside the code.
Private Function ReadVariable(ByVal varName As String) As Object

Dim result As Object
Try
Dim vars As Variables
Dts.VariableDispenser.LockForRead(varName)
Dts.VariableDispenser.GetVariables(vars)
Try
result = vars(varName).Value
Catch ex As Exception
Throw ex
Finally
vars.Unlock()
End Try
Catch ex As Exception
Throw ex
End Try

Return result

End Function

' To avoid the variable locking problem use this method to set package variables.
' No write variables need to be declared in the "ReadWriteVariables" list of the script
' component if this method is used inside the code.
Private Sub WriteVariable(ByVal varName As String, ByVal varValue As Object)
Try
Dim vars As Variables
Dts.VariableDispenser.LockForWrite(varName)
Dts.VariableDispenser.GetVariables(vars)
Try
vars(varName).Value = varValue
Catch ex As Exception
Throw ex
Finally
vars.Unlock()
End Try
Catch ex As Exception
Throw ex
End Try

End Sub

End Class



BI Guy
Post #557061
Posted Friday, August 22, 2008 12:10 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, August 5, 2010 9:01 PM
Points: 1, Visits: 7
How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET --- http://support.microsoft.com/kb/316934/en-us
Post #557075
Posted Friday, August 22, 2008 9:20 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, December 10, 2014 8:44 AM
Points: 49, Visits: 282

This link is broken


BI Guy
Post #557446
Posted Friday, August 22, 2008 9:56 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, March 12, 2009 3:29 PM
Points: 2, Visits: 6


What am I missing here? I am using VS 2008 and nowhere can I find a way to
"Add Execute SQL Task to the canvas .....".

What version of VS are you using. I can't even find how this is done by searching MSDN.

They describe how to configure it but do not describe how to create it in the first place
which is typical of MS documentation.

thanks, Otto Porter
Post #557489
Posted Tuesday, August 26, 2008 6:18 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, December 15, 2014 4:15 AM
Points: 5,471, Visits: 1,402
Nice article...


Post #558786
Posted Wednesday, August 27, 2008 11:55 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 12:27 AM
Points: 542, Visits: 805
Very nice article. I am sure I will be using the techniques introduced here in the near future. Some minor complaints: the screenshots at the end (dealing with variables) seem a bit off (specifically the one showing ExcelTab variable, which in fact belongs to ExcelFileName), also the package provided for download is a little different (variable names) from the article, but like I said, this is not a big deal.

Thank you!



Post #559848
Posted Friday, August 29, 2008 3:10 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: 2 days ago @ 7:57 AM
Points: 99, Visits: 2,237
It would be an excellent article if it was better written. Poorly written which takes a while to read through and understand which is unfortunate as there are some very good and informative points raised
Post #561060
Posted Wednesday, September 10, 2008 11:34 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, April 12, 2010 4:36 PM
Points: 7, Visits: 8
What is the actual Expression used in the Excel Connection Manager? The screen shots in your original article are too blury to read, and your followup post simply left it out:

"Set the Excel Connection (notice that usually you specific connection 1, connection 2 etc but here NO specified number) to use a New Property expression. In the New Property expression and add the variable defined for the Excelsheet name. This variable references back to the cursor (called in this case 'Region' . . ."

Could you simply write the expression, please?

Really appreciate it.
Post #567149
Posted Wednesday, September 10, 2008 11:44 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, April 12, 2010 4:36 PM
Points: 7, Visits: 8
What is the actual Expression used in the Excel Connection Manager? The screen shots in your original article are too blury to read, and your followup post simply left it out:

"Set the Excel Connection (notice that usually you specific connection 1, connection 2 etc but here NO specified number) to use a New Property expression. In the New Property expression and add the variable defined for the Excelsheet name. This variable references back to the cursor (called in this case 'Region' . . ."

Could you simply write the expression, please?

Really appreciate it.
Post #567159
Posted Wednesday, September 10, 2008 6:15 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 30, 2009 8:07 PM
Points: 15, Visits: 40
Anyone having problems reading the graphics please note your contact address/email here and I will send you a copy of the article. (if you don't mind)

At this time I am not actually working with SQL Server, rather with Oracle tools so don't have them loaded right now. Sorry about that!
Post #567399
« Prev Topic | Next Topic »

Add to briefcase «««23456»»

Permissions Expand / Collapse