2013-07-30 46 views
1

我想創建一個SSIS包,它將從SQL Server中的表中選擇所有值到Excel表中。該表是在運行時創建的,因爲每次運行時它都會更改。我可以在Excel工作表中創建新表格,但我在那裏獲取數據時遇到了很多麻煩。我不能做openrowset查詢,因爲我工作的公司不會允許它。它不能通過數據任務流完成,因爲我不知道開始時的標題。ssis腳本任務動態從SQL Server數據庫導出到Excel工作表

我已經嘗試了一些腳本任務,但無法弄清楚如何得到它的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中,我只是因與麻煩,我認爲這會工作,我不知道。如果有人有一個更好的想法,我很樂意聽到它

回答

2

這裏有一個快速下降和骯髒的方式從一個DataTable到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 
+0

非常感謝你這是excaly我被問了什麼,但是這個我會去在沒有安裝辦公室的服務器上,因爲我不能使用Microsoft.Office.Interop.Excel – user2615302

+0

你可以下載互操作程序集來安裝(不需要辦公室): http://www.microsoft.com/en-us/download/details.aspx?id=3508 –

相關問題