Friday, November 5, 2010

IS - ScriptTask - Protective Excel

I have fallen in to a scenario where i needed to create multiple Excel data files  from a source.   This task i was enjoyed doing. Because of a Script Task which is totally on creation of Protetctive Excel sheets, Locking some columns and coloring required column cells. I thought it is a good project to share with you all.
--> Source DB: Adventureworks database
--> Table that i would like to split in to multiple ExcelSeets:
--> Create multiple Excel sheets bassed on distinct Item. ex: File name will be like :   Item_20101105.xls


Option Strict Off
Option Explicit On

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

    Public Sub Main()


    Dim xlApp As Object
    Dim xlBook As Object
    Dim xlWorkSheet As Object
    Dim objRange As Object
    Dim strExcelFile As String
    Dim fullfilename As String
    Dim ClientName, CurrentDate, SchoolOPEID As String
    ClientName = CStr(Dts.Variables("ClientName").Value)
    CurrentDate = CStr(Dts.Variables("CurrentDate").Value)
    SchoolOPEID = CStr(Dts.Variables("SchoolID").Value)
    fullfilename = ClientName + "_NewPlace_" + CurrentDate + "_" + SchoolID + ".xls"
    Try
    strExcelFile = "C:FlatFileSource\" + fullfilename
    xlApp = CreateObject("Excel.Application")
    xlBook = xlApp.Workbooks.Open(strExcelFile)
    If WorksheetExists(xlBook, "Excel_Destination") Then
       xlWorkSheet = xlBook.Sheets("Excel_Destination")
                ' Color and format title row
       objRange = xlWorkSheet.Range("A1", "BW1")
       objRange.Font.Size = 11
       objRange.Font.Bold = True
       objRange.Interior.ColorIndex = 16
       objRange.Font.ColorIndex = 1
                'Adjust titles to show up
       objRange.EntireColumn.Autofit()
                'Color required field using following code.
                ' Dim myCount As Integer
                ' myCount = xlWorkSheet.UsedRange.Rows.Count
       Const xlEdgeLeft = 7
       Const xlContinuous = 1
       Const xlAutomatic = -4105
       Const xlThin = 2
       Const xlGray16 = 17
       Const xlHairline = 1
       objRange = xlWorkSheet.Range("E1:K1", "E65535:K65535")
                'objRange.Interior.ColorIndex = 3
       objRange.Borders.LineStyle = xlContinuous
       objRange.Borders.ColorIndex = 3
       objRange.Borders.Weight = xlThin
       xlWorkSheet.Columns("AI:AI").NumberFormat = "0,#"
       objRange = xlWorkSheet.Range("BX:ET", "BX65535:ET65535")
       objRange.Columns.Delete()
                ' Following will lock all columns except specified.
       xlWorkSheet.Unprotect()
       Dim strmypassword
       xlWorkSheet.Protection.AllowEditRanges.Add("FirstSet", xlWorkSheet.Columns("E:AP"))
       xlWorkSheet.Protection.AllowEditRanges.Add("SecondSet", xlWorkSheet.Columns("BE"))
       xlWorkSheet.Protect(strmypassword)
       xlBook.Save()
     End If
       Catch e As Exception
       MsgBox("ERROR:" & e.ToString, MsgBoxStyle.Critical)
        Finally
            If Not xlBook Is Nothing Then
                xlBook.Close()
                xlBook = Nothing
            End If
            If Not xlApp Is Nothing Then
                xlApp.Quit()
                xlApp = Nothing
            End If
        End Try
        Dts.TaskResult = Dts.Results.Success
    End Sub
    Function WorksheetExists(ByRef xlWorkbook As Object, ByVal strWorksheetName As String) As Boolean
        Dim xlWorksheet As Object
        If xlWorkbook Is Nothing Then
            WorksheetExists = False
        Else
            xlWorksheet = xlWorkbook.Sheets(strWorksheetName)
            WorksheetExists = Not xlWorksheet Is Nothing
        End If
    End Function
End Class

No comments: