Labels

Apache Hadoop (3) ASP.NET (2) AWS S3 (2) Batch Script (3) BigQuery (21) BlobStorage (1) C# (3) Cloudera (1) Command (2) Data Model (3) Data Science (1) Django (1) Docker (1) ETL (7) Google Cloud (5) GPG (2) Hadoop (2) Hive (3) Luigi (1) MDX (21) Mongo (3) MYSQL (3) Pandas (1) Pentaho Data Integration (5) PentahoAdmin (13) Polybase (1) Postgres (1) PPS 2007 (2) Python (13) R Program (1) Redshift (3) SQL 2016 (2) SQL Error Fix (18) SQL Performance (1) SQL2012 (7) SQOOP (1) SSAS (20) SSH (1) SSIS (42) SSRS (17) T-SQL (75) Talend (3) Vagrant (1) Virtual Machine (2) WinSCP (1)

Monday, January 17, 2011

SSIS 2008 - Script task to Import Excel file from SharePoint

Use the below VB script to import excel file from SharePoint in SSIS 2008 Script Task:

' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic 2008.
' The ScriptMain is the entry point class of the script.

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

<System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
 Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

 Enum ScriptResults
  Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
  Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
 End Enum
 

 ' The execution engine calls this method when the task executes.
 ' To access the object model, use the Dts property. Connections, variables, events,
 ' and logging features are available as members of the Dts property as shown in the following examples.
 '
 ' To reference a variable, call Dts.Variables("MyCaseSensitiveVariableName").Value
 ' To post a log entry, call Dts.Log("This is my log text", 999, Nothing)
 ' To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, True)
 '
 ' To use the connections collection use something like the following:
 ' ConnectionManager cm = Dts.Connections.Add("OLEDB")
 ' cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;"
 '
 ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
 '
 ' To open Help, press F1.


    Public Sub Main()

        Dim URL As String = "http://citind:28000/Excel%20Library/HelpdeskSoftware/Software%20usage%20report.xls"
        Dim Req As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(URL), System.Net.HttpWebRequest)
        'Req.Credentials = System.Net.CredentialCache.DefaultCredentials
        Req.Credentials = New System.Net.NetworkCredential("datawh", "500%sec", "cit")
        Req.Method = "GET"
        Dim path As String = "C:\DataFiles\HelpDesk\Software usage report.xls"
        Dim objResponse As System.Net.WebResponse = Req.GetResponse()
        Dim fs As New System.IO.FileStream(path, System.IO.FileMode.Create)
        Dim stream As System.IO.Stream = objResponse.GetResponseStream()
        Dim buf As Byte() = New Byte(1023) {}
        Dim len As Integer = stream.Read(buf, 0, 1024)
        While len > 0
            fs.Write(buf, 0, len)
            len = stream.Read(buf, 0, 1024)
        End While
        stream.Close()
        fs.Close()

        Dts.TaskResult = ScriptResults.Success
    End Sub

End Class

 

1 comment:

  1. Mahadevan,
    Nifty script worked 1st time! thanks for posting

    How easy would it be flip the process and write a file to sharepoint? I need to do both.

    Rick

    ReplyDelete