SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to Dynamically and Iteratively Populate An Excel Workbook from SQL Server


How to Dynamically and Iteratively Populate An Excel Workbook from SQL Server

Author
Message
Philippe Cand
Philippe Cand
SSC Veteran
SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)

Group: General Forum Members
Points: 284 Visits: 294
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
delphigao
delphigao
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
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
Philippe Cand
Philippe Cand
SSC Veteran
SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)

Group: General Forum Members
Points: 284 Visits: 294

This link is broken

BI Guy
Otto Porter
Otto Porter
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
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
Anipaul
Anipaul
SSCrazy Eights
SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)SSCrazy Eights (9.6K reputation)

Group: General Forum Members
Points: 9627 Visits: 1407
Nice article...



Misha_SQL
Misha_SQL
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1618 Visits: 1010
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!



Cowboy DBA
Cowboy DBA
Mr or Mrs. 500
Mr or Mrs. 500 (541 reputation)Mr or Mrs. 500 (541 reputation)Mr or Mrs. 500 (541 reputation)Mr or Mrs. 500 (541 reputation)Mr or Mrs. 500 (541 reputation)Mr or Mrs. 500 (541 reputation)Mr or Mrs. 500 (541 reputation)Mr or Mrs. 500 (541 reputation)

Group: General Forum Members
Points: 541 Visits: 2255
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
CJ-414083
CJ-414083
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 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.
CJ-414083
CJ-414083
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 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.
mdeschen1
mdeschen1
SSC Journeyman
SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)

Group: General Forum Members
Points: 75 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!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search