如何在 SSIS 数据流源 Visual Basic 脚本中测试 Excel 空白单元格

问题描述 投票:0回答:1

我需要阅读一组“表单”,其中每个表单都是单个 Excel 文档中的第一个工作表。这些文档都集中在一个文件夹中。我从每张工作表中选择某些单元格来形成一条记录,然后将其写入 SQL Server 表以供以后分析。

在我的 SSIS 包中,这是通过数据流实现的,该数据流使用以 Visual Basic 2015 编写的脚本组件作为数据源,并作为 ADO 网络目标连接到 SQL Server 表。

源脚本获取文件夹中 Excel 文件的列表,然后循环遍历每个文件,在 Excel 中打开它,选择第一个工作表,并将各个单元格中保存的值分配给输出字段,如下所示:

OutputBuffer.CreditPeriod = Sheet.Range("C10").Value.ToString

OutputBuffer.CreditReason = Sheet.Range("C12").Value.ToString

一切都运行良好,直到代码遇到用户留空的单元格,此时分配错误为“对象引用未设置为对象的实例”。

显然,我想捕获这种情况并为输出字段输入替代值。不幸的是,我无法在 SSIS 中使用 Visual Basic 2015 找到适用于这种情况的函数或语法形式。我找到的所有示例都是针对具有 IsBlank() 或 IsEmpty() 函数的 Visual Basic for Applications。这些在 VB 中不起作用,只会生成诸如“IsEmpty 未声明”之类的错误。

我在 VB 2015 中发现的唯一似乎相关的函数是 String.IsNullOrEmpty()。例如:

如果 String.IsNullOrEmpty(Sheet.Range("C13").Value.ToString) 那么 OutputBuffer.CreditReasonOther = "" 别的 OutputBuffer.CreditReasonOther = Sheet.Range("C13").Value.ToString 结束如果

遗憾的是,这未能捕获条件,脚本仍然出错。在我尝试读取并导致错误之前,我也无法在模型中发现表明 Excel 单元格为空白的属性。

是否有其他人遇到过此问题,或者是否有人知道在 If 语句中检测 Excel 单元格的“对象引用未设置到对象实例”条件的正确方法?我在 VB 中没有找到可能有帮助的 SQL ISNULL() 函数的等效项。

如果有帮助,为了获得我需要的功能,我在脚本顶部使用了以下语句(从示例中复制):

导入系统.IO

导入 Excel = Microsoft.Office.Interop.Excel

这是从我的 SSIS 数据流源脚本中提取的代码示例:

#Region "Imports"
Imports System
Imports System.Data
Imports System.Math
Imports System.IO
Imports Excel = Microsoft.Office.Interop.Excel
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
#End Region

Public Overrides Sub CreateNewOutputRows()
        '
        ' Add rows by calling the AddRow method on the member variable named "<Output Name>Buffer".
        ' For example, call MyOutputBuffer.AddRow() if your output was named "MyOutput".
        '
        Dim XL As Excel.Application = Nothing
        Dim WorkBooks As Excel.Workbooks = Nothing
        Dim CreditNote As Excel._Workbook = Nothing
        Dim Sheets As Excel.Sheets = Nothing
        Dim Sheet As Excel._Worksheet = Nothing
        Dim CreditNoteFile As FileInfo = Nothing

        ' Get a list of Excel files in the Credit Notes folder.

        Dim CreditNotesFolder As New DirectoryInfo(Variables.CreditNotesFolder)
        Dim ListOfCreditNotes As FileInfo() = CreditNotesFolder.GetFiles("*.XL*")

        ' Initialse Excel and loop through all the Excel files in the directory list.

        XL = New Excel.Application
        WorkBooks = XL.Workbooks

        For Each CreditNoteFile In ListOfCreditNotes

            ' Ignore temporary files
            If CreditNoteFile.Name.StartsWith("~$") Then
                Continue For
            End If

            ' Open the Excel spreadsheet.

            CreditNote = WorkBooks.Open(CreditNoteFile.FullName)
            Sheets = CreditNote.Worksheets
            Sheet = CType(Sheets(1), Excel.Worksheet) ' This CType is because of bug in .NET

            ' Pick values from individual cells as outputs

            OutputBuffer.AddRow()
            OutputBuffer.Filename = CreditNoteFile.Name
            OutputBuffer.AccountName = Sheet.Range("C6").Value.ToString
            OutputBuffer.CreditReason = Sheet.Range("C12").Value.ToString
            OutputBuffer.CreditReasonOther = Sheet.Range("C13").Value.ToString

            ' Close the spreadsheet

            CreditNote.Close()
            
        Next CreditNoteFile

        ' Mark the end of the dataflow
        
        OutputBuffer.SetEndOfRowset()
    End Sub
excel vb.net ssis
1个回答
0
投票

我最终发现,当单元格为空时,.ToString 函数会出错。所以,你不能这样做:

Sheet.Range("C6").Value.ToString

...然后测试结果。您要做的就是在执行其他操作之前测试单元,而“Is Nothing”就是您必须用作测试的内容:

If Sheet.Range("C6").Value Is Nothing Then                         
    OutputBuffer.AccountName = ""
Else
    OutputBuffer.AccountName = Sheet.Range("C6").Value.ToString
End If

不客气。

© www.soinside.com 2019 - 2024. All rights reserved.