Write to Log in VB.NET

Public Sub WriteToLog(ByVal pEffectiveDate As TypeOptionalDate, ByVal pSnapshotDate As TypeOptionalDate)
Dim strLogFileName As String = AppDomain.CurrentDomain.BaseDirectory.Replace(“/”, “\”)
strLogFileName = strLogFileName & “MyLogFile_” & System.DateTime.Now.Ticks.ToString() & “.log”
Dim sb As New StringBuilder()

sb.AppendLine(“SetDatabaseContext – pEffectiveDate == ” & Convert.ToString(pEffectiveDate.Value))
sb.AppendLine(“SetDatabaseContext – pSnapshotDate == ” & Convert.ToString(pSnapshotDate.Value))
sb.AppendLine(“= = = = = =”)
sb.AppendLine()
sb.AppendLine()

Using outfile As New System.IO.StreamWriter(strLogFileName)
outfile.Write(sb.ToString())
End Using
End Sub

Export Dataset to Excel in VB.NET

Public Sub ExportToFile(ByVal ds As DataSet)
Try
Dim ExcelApp As New Microsoft.Office.Interop.Excel.ApplicationClass()
Dim xlWorkbook As Microsoft.Office.Interop.Excel.Workbook = ExcelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet)

‘ Loop over DataTables in DataSet.
Dim collection As DataTableCollection = ds.Tables

For i As Integer = collection.Count To 1 Step -1
Dim xlSheets As Microsoft.Office.Interop.Excel.Sheets = Nothing
Dim xlWorksheet As Microsoft.Office.Interop.Excel.Worksheet = Nothing
‘Create Excel Sheets
xlSheets = ExcelApp.Sheets
xlWorksheet = DirectCast(xlSheets.Add(xlSheets(1), Type.Missing, Type.Missing, Type.Missing), Microsoft.Office.Interop.Excel.Worksheet)

Dim table As System.Data.DataTable = collection(i – 1)
xlWorksheet.Name = table.TableName

For j As Integer = 1 To table.Columns.Count
ExcelApp.Cells(1, j) = table.Columns(j – 1).ColumnName
Next

‘ Storing Each row and column value to excel sheet
For k As Integer = 0 To table.Rows.Count – 1
For l As Integer = 0 To table.Columns.Count – 1
ExcelApp.Cells(k + 2, l + 1) = table.Rows(k).ItemArray(l).ToString()
Next
Next
ExcelApp.Columns.AutoFit()
Next
DirectCast(ExcelApp.ActiveWorkbook.Sheets(ExcelApp.ActiveWorkbook.Sheets.Count), Microsoft.Office.Interop.Excel.Worksheet).Delete()
‘ExcelApp.Visible = True

Dim strFileName As String = AppDomain.CurrentDomain.BaseDirectory.Replace(“/”, “\”)
‘strFileName = strFileName & “MyExcelFile_” & System.DateTime.Now.Ticks.ToString() & “.xls”
strFileName = strFileName & “MyExcelFile_” & DateTime.Now.ToString(“yyyyMMdd”) & “_” & DateTime.Now.ToString(“HHmmss”) & “.xls”

ExcelApp.ActiveWorkbook.SaveAs(strFileName)
ExcelApp.Quit()
Catch ex As Exception
‘MessageBox.Show(ex.Message)
End Try

End Sub