Created
February 14, 2016 07:46
-
-
Save fabiobruna/88dba7e6e64bca7e5647 to your computer and use it in GitHub Desktop.
Opzetjes met dynamische headers. Om uit de plukken. Schrijven naar Excel met C# From http://www.codeproject.com/Questions/516514/Addingplusdataplustoplusanplusexcelplusfileplususi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System | |
Imports System.Data | |
Imports System.Math | |
Imports Microsoft.SqlServer.Dts.Runtime | |
Imports Microsoft.Office.Interop.Excel | |
<system.addin.addin("scriptmain",> _ | |
<system.clscompliantattribute(false)> _ | |
Partial Public Class ScriptMain | |
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase | |
Enum ScriptResults | |
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success | |
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure | |
End Enum | |
Public Sub Main() | |
Dim cmConnMgr As ConnectionManager | |
Dim cmParams As Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSConnectionManagerDatabaseParameters100 | |
Dim ConnOledb As OleDb.OleDbConnection | |
cmConnMgr = Dts.Connections("OLEDBConn") | |
cmParams = CType(cmConnMgr.InnerObject, Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSConnectionManagerDatabaseParameters100) | |
ConnOledb = CType(cmParams.GetConnectionForSchema(), OleDb.OleDbConnection) | |
Dim strSQL As System.Text.StringBuilder = New System.Text.StringBuilder | |
Dim drReader As OleDb.OleDbDataReader | |
Dim cmdCommand As New OleDb.OleDbCommand | |
Dim dtDetails As New System.Data.DataTable | |
strSQL.Append("SELECT first_name,email FROM xxxx where user_id <=2000") | |
Try | |
cmdCommand.CommandText = strSQL.ToString | |
cmdCommand.CommandType = CommandType.Text | |
cmdCommand.Connection = ConnOledb | |
drReader = Nothing | |
drReader = cmdCommand.ExecuteReader() | |
If drReader.HasRows Then | |
dtDetails.Load(drReader) | |
End If | |
drReader.Close() | |
Catch ex As Exception | |
drReader = Nothing | |
End Try | |
MsgBox(dtDetails.Columns.Count) | |
MsgBox(dtDetails.Rows.Count) | |
Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass() | |
Dim Format As XlFileFormat = XlFileFormat.xlExcel8 | |
With excel | |
.SheetsInNewWorkbook = 1 | |
.Workbooks.Add() | |
.Worksheets(1).Select() | |
'Create Column Header | |
Dim i As Integer = 1 | |
For col = 0 To dtDetails.Columns.Count() - 1 | |
.Cells(1, i).value = dtDetails.Columns(col).ColumnName.ToString | |
.Cells(1, i).EntireRow.Font.Bold = True | |
i += 1 | |
Next | |
i = 2 | |
Dim k As Integer = 1 | |
For col = 0 To dtDetails.Columns.Count() - 1 | |
i = 2 | |
For row = 0 To dtDetails.Rows.Count() - 1 | |
.Cells(i, k).Value = dtDetails.Rows(row).Item(col).ToString | |
i += 1 | |
Next | |
k += 1 | |
Next | |
.ActiveCell.Worksheet.SaveAs("C:\testing.xls", Format) | |
.Workbooks.Close() | |
End With | |
excel.Quit() | |
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel) | |
excel = Nothing | |
Dts.TaskResult = ScriptResults.Success | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment