The code below is to bind a dataset to an Excel
file in Vb.net
One core record also binds in a .csv
file
I need this code to be translated to C#
Public Function Write2CSV(ByVal ExlDs As Data.DataSet) As String
Dim strfilename As String, i As Integer = 0, p As Integer = 0
Dim dRandomNo As Integer = Rnd(1) * 10000
Dim sAppPath As String = System.AppDomain.CurrentDomain.BaseDirectory
Dim sFile As String = "RepFile\Dispatch" & Format(Now, "ddMMyyyyHHmmss") & dRandomNo & ".csv"
Dim sOpnURL As String
TextBox1.Text = sFile
strfilename = sAppPath & "Reports\" & sFile
Dim swObj As StreamWriter
Try
swObj = File.AppendText(strfilename)
For Each colObj As DataColumn In ExlDs.Tables(0).Columns
swObj.Write(colObj.ColumnName & ",")
Next
swObj.WriteLine()
For intRow As Integer = 0 To ExlDs.Tables(0).Rows.Count - 1
For intCol As Integer = 0 To ExlDs.Tables(0).Columns.Count - 1
swObj.Write(ExlDs.Tables(0).Rows(intRow)(intCol) & ",")
Next
swObj.WriteLine()
Next
Dim strFileURL() As String = Split(sFile, "\")
sOpnURL = "RepFile/" & strFileURL(UBound(strFileURL))
'sbObj.Save(strfilename)
Catch ex As Exception
'bError = True
Dim ErrContext As HttpContext = HttpContext.Current
ErrContext.Items.Add("ErrDesc", ex.Message)
ErrContext.Items.Add("ErrSrc", ex.Source)
ErrContext.Items.Add("ErrInfo", ex.StackTrace)
ErrContext.Items.Add("ErrFile", "T")
Finally
'sbObj = Nothing
swObj.Close()
swObj = Nothing
End Try
Return sOpnURL
End Function
Help is highly appreciated