ssis脚本任务,用于从SQL Server数据库动态导出到Excel工作表

时间:2013-07-30 19:33:09

标签: sql vb.net excel ssis

我正在尝试创建一个SSIS包,它将从SQL Server中的表中选择所有值到Excel中的表。该表是在运行时创建的,因为它每次运行时都会更改。我可以在Excel工作表中创建新表,但我在那里获取数据时遇到了很多麻烦。

我无法进行openrowset查询,因为我工作的公司不允许这样做。它无法通过数据任务流完成,因为我不知道开头的标题是什么。

我已尝试过一些脚本任务但无法弄清楚如何将其放到Excel工作表中

有没有人有任何示例代码或任何能告诉我如何动态地从SQL Server导出到Excel的内容?

    Dim cn As New OleDbConnection
    Dim sqlcn As New SqlConnection
    Dim adapter As New OleDbDataAdapter
    Dim dtset As New DataSet
    Dim dt As New DataTable
    Dim cmd As New OleDbCommand
    Dim sqlcmd As New SqlCommand
    Dim dr As DataRow

    cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Excel 8.0;Database=E:\sheet.xls;" + "HDR=Yes;Readonly=False;IMEX=0;"
    sqlcn.ConnectionString = "Data Source=DB_NAME;Initial Catalog=Main;Integrated Security=True"
    cn.Open()
    sqlcn.Open()


    Dim da As New SqlDataAdapter("Select * from Temp_Totals", sqlcn)
    da.Fill(dt)

到目前为止,我需要从dt插入到Excel中,我只是遇到了麻烦,我觉得这样可行,我不确定。如果有人有更好的主意,我很乐意听到它

1 个答案:

答案 0 :(得分:2)

这是一种从数据表复制到excel的快速,简单和脏的方式,而无需遍历数据表的每个列/行:

Private Sub ExportToExcel(ByVal dt As DataTable, ByVal outputPath As String)
    Dim xlApp As Application = CreateObject("Excel.Application") 
    Dim xlWorkbook As Workbook = xlApp.Workbooks.Add(Type.Missing)
    Dim sheetIndex As Integer = 0
    Dim col, row As Integer
    Dim xlSheet As Worksheet
    Dim rawData(dt.Rows.Count, dt.Columns.Count - 1) As Object

    For col = 0 To dt.Columns.Count - 1
        rawData(0, col) = dt.Columns(col).ColumnName
    Next

    For col = 0 To dt.Columns.Count - 1
        For row = 0 To dt.Rows.Count - 1
            rawData(row + 1, col) = dt.Rows(row).ItemArray(col).ToString
        Next
    Next

     Dim finalColLetter As String = String.Empty
    Dim colCharset As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Dim colCharsetLen As Integer = colCharset.Length

    If dt.Columns.Count > colCharsetLen Then
        finalColLetter = colCharset.Substring((dt.Columns.Count - 1) \ colCharsetLen - 1, 1)
    End If

    finalColLetter += colCharset.Substring((dt.Columns.Count - 1) Mod colCharsetLen, 1)


    xlSheet = CType(xlWorkbook.Sheets.Add(xlWorkbook.Sheets(sheetIndex), Type.Missing, 1, XlSheetType.xlWorksheet), Worksheet)

    xlSheet.Name = dt.TableName


    Dim xlRange As String = String.Format("A1:{0}{1}", finalColLetter, dt.Rows.Count + 1)
    xlSheet.Range(xlRange, Type.Missing).Value2 = rawData


    Dim firstrow As Range = CType(xlSheet.Rows(1, Type.Missing), Range)
    firstrow.Font.Bold = True
    firstrow.Select()
    firstrow.AutoFilter(1, Type.Missing, XlAutoFilterOperator.xlAnd, Type.Missing, True)
    xlSheet.Application.ActiveWindow.SplitRow = 1
    xlSheet.Application.ActiveWindow.FreezePanes = True
    xlSheet.Columns.EntireColumn.AutoFit()
    xlSheet.Range("A1").Select()
    xlSheet.PageSetup.Orientation = XlPageOrientation.xlLandscape
    With xlSheet.PageSetup
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .BottomMargin = 0.25
        .TopMargin = 0.25
        .LeftMargin = 0.25
        .RightMargin = 0.25
        .HeaderMargin = 0
        .FooterMargin = 0
    End With
    firstrow = Nothing
    xlSheet = Nothing


    For Each xls As Worksheet In xlWorkbook.Worksheets
        If xls.Name.Contains("Sheet") = True Then xls.Delete()
    Next

    xlWorkbook.SaveAs(outputPath, XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
    xlWorkbook.Close(True, Type.Missing, Type.Missing)
    xlWorkbook = Nothing


    xlApp.Quit()
    xlApp = Nothing


    GC.Collect()
    GC.WaitForPendingFinalizers()

End Sub