|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, April 29, 2013 10:28 AM
Points: 46,
Visits: 262
|
|
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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, August 05, 2010 9:01 PM
Points: 1,
Visits: 7
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, April 29, 2013 10:28 AM
Points: 46,
Visits: 262
|
|
This link is broken
BI Guy
|
|
|
|
|
Forum 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
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: 2 days ago @ 2:50 AM
Points: 4,785,
Visits: 1,334
|
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 5:54 PM
Points: 525,
Visits: 617
|
|
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!
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Yesterday @ 5:32 AM
Points: 88,
Visits: 1,960
|
|
| 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
|
|
|
|
|
Forum 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.
|
|
|
|
|
Forum 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.
|
|
|
|
|
Grasshopper
      
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!
|
|
|
|