Résolu: Formater feuille excel
J'ai trouvé une solution à mon problème et je vous la partage:
Le code suivant est inséré dans un "Script Task" de SSIS.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports Microsoft.Office.Interop.Excel
<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
Public Sub Main()
Dim Excel As New Microsoft.Office.Interop.Excel.Application
Dim FileName As String = "\\BLMCIK\BWAsset\BW_GWC.xls"
Dim MyIntPtr As IntPtr = Excel.Hwnd
Excel.DisplayAlerts = False 'Nous ne voulons pas voir un alerte pop-up
Try
'Déclaration du classeur Excel.
Dim Workbook As Microsoft.Office.Interop.Excel.Workbook
Workbook = Excel.Workbooks.Open(FileName, , False) 'False = read-write.
'Déclaration de la feuille à être manipulée.
Dim Worksheet As Microsoft.Office.Interop.Excel.Worksheet = Nothing
Worksheet = Workbook.Sheets(1)
'Forcer le type de la cellule en format texte
Worksheet.Range(Worksheet.Cells(2, 1), Worksheet.Cells(3200, 12)).Select()
Excel.Selection.numberFormat = "@"
'Sauvegarde du fichier.
Workbook.Save()
'Nettoyage de la mémoire.
FileName = Nothing
Worksheet = Nothing
Workbook = Nothing
'On quitte Excel
Excel.Quit()
'S'assure que l'exécutable excel.exe est bien terminé avant de quitter.
For Each proc As System.Diagnostics.Process In System.Diagnostics.Process.GetProcessesByName("Excel")
If proc.MainWindowHandle = MyIntPtr Then
proc.Kill()
End If
Next
Excel = Nothing
Dts.TaskResult = ScriptResults.Success
Catch ex As Exception
Excel.Quit()
For Each proc As System.Diagnostics.Process In System.Diagnostics.Process.GetProcessesByName("Excel")
If proc.MainWindowHandle = MyIntPtr Then
proc.Kill()
End If
Next
Excel = Nothing
FileName = Nothing
Dts.TaskResult = ScriptResults.Failure
End Try
End Sub
End Class |