在SSIS脚本任务中激活特定Excel工作表

Activate specific Excel sheet in SSIS Script Task(在SSIS脚本任务中激活特定Excel工作表)

本文介绍了在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工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在SSIS脚本任务中激活特定Excel工作表

基础教程推荐