本文介紹了在SSIS腳本任務中激活特定Excel工作表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個腳本任務,它打開一個Excel文件并將其另存為.csv,但它會保存與該文件一起打開的工作表,而我想選擇將哪個工作表另存為.csv,這是我當前使用的腳本:
Public Sub Main()
Dim oMissing As Object = System.Reflection.Missing.Value
Dim xl As New Microsoft.Office.Interop.Excel.ApplicationClass()
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim laPath As String = "C:Filename.xlsx"
xlBook = DirectCast(xl.Workbooks.Open(laPath, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing), Workbook)
xl.DisplayAlerts = False
xlSheet = DirectCast(xlBook.Worksheets.Item(1), Worksheet)
xlBook.SaveAs("C:Filename.csv", 6, oMissing, oMissing, oMissing, oMissing,, oMissing, oMissing, oMissing, oMissing, oMissing)
xl.Application.Workbooks.Close()
Dts.TaskResult = ScriptResults.Success
End Sub
我想我必須使用xlbook.Worksheets("Sheet1").Activate
,但我似乎找不到它。
推薦答案
您必須按工作表的名稱打開它,然后使用Select()
函數選擇它:
xlSheet = xlBook.Worksheets("Sheet1")
xlSheet.Select()
您的代碼將如下所示:
Public Sub Main()
Dim oMissing As Object = System.Reflection.Missing.Value
Dim xl As New Microsoft.Office.Interop.Excel.ApplicationClass()
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim laPath As String = "C:Filename.xlsx"
xlBook = xl.Workbooks.Open(laPath)
xl.DisplayAlerts = False
xlSheet = xlBook.Worksheets("Sheet1")
xlSheet.Select()
xlBook.SaveAs("C:Filename.csv", Excel.XlFileFormat.xlCSV,Excel.XlSaveAsAccessMode.xlNoChange)
xl.Application.Workbooks.Close()
Dts.TaskResult = ScriptResults.Success
End Sub
這篇關于在SSIS腳本任務中激活特定Excel工作表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,