Forum Discussion

MarkBird's avatar
MarkBird
Contributor III
9 months ago
Solved

Export data using a Method Query

Hi I have a method query that returns a data table of roughly 300k of records. Is there anyway to export the result of the method query to a csv file? I'm not keen on using a spreadsheet busine...
  • FredLucas's avatar
    9 months ago

    Hi MarkBird 

    You could use the following snippet:

    		#Region "PrintDataTable"
    		Public Function PrintDataTable(ByVal si As SessionInfo, ByVal dt As DataTable, ByVal Optional strSeparator As String = "|", ByVal Optional limit As Integer = 0, ByVal Optional boolPrintTableName As Boolean = True, ByVal Optional list As List(Of String) = Nothing, ByVal Optional strTextSep As String = Nothing) As String
    	        Dim strLog As New text.StringBuilder
    			Dim strBRName As String = System.Reflection.MethodInfo.GetCurrentMethod().Name
    			Dim listColToSkip As New List(Of String) ({"None"})
    		    If Not IsNothing(list) Then
    		        listColToSkip = list
    		    End If
    			strLog.AppendLine(String.Format("{0} - Sep: {1}, Limit: {2}, Print Table Name: {3}, Col(s) To Skip: {4}", strBRName, strSeparator, limit.ToString, boolPrintTableName.ToString, String.Join(", ", listColToSkip)))
    			
    			If strSeparator.XFEqualsIgnoreCase("tab") Then
    				strSeparator = vbTab
    			End If
    			
    			Try
    	            Dim result As New Text.StringBuilder
    				
    				If boolPrintTableName Then 
    					result.AppendLine("Table name: " & dt.TableName)
    				End If
    
    	            Dim header As String = Nothing
    	            For Each dc As DataColumn In dt.Columns
    					strLog.AppendLine(String.Format("{0} - Col Name: {1}, To Skip: {2}", strBRName, dc.ColumnName, listColToSkip.Contains(dc.ColumnName)))
    					If Not listColToSkip.Contains(dc.ColumnName) Then 
    	                	header = header & dc.ColumnName & strSeparator
    					End If
    	            Next
    				If Not String.IsNullOrEmpty(header) Then
    	            	result.AppendLine(header)
    				End If
    
    				If limit = 0 Then limit = dt.Rows.Count -1
    				If limit > dt.Rows.Count -1 Then limit = dt.Rows.Count -1
    	            For curRow As Integer = 0 To limit
    	                Dim row As String = String.Empty
    	                For curCol As Integer = 0 To dt.Columns.Count -1
    						If Not listColToSkip.Contains(dt.Columns(curCol).ColumnName) Then
    	                    	If strTextSep Is Nothing Then 
    	                    		row = row & dt.Rows(curRow)(curCol).ToString.Trim & strSeparator
    							Else
    								row = row & strTextSep & dt.Rows(curRow)(curCol).ToString.Trim & strTextSep & strSeparator
    							End If
    						End If
    	                Next
    					If Not String.IsNullOrEmpty(row) Then
    	                	result.AppendLine(row)
    					End If
    	            Next
    
    	            Return result.ToString
    	            
    	        Catch ex As Exception
    				BRApi.ErrorLog.LogMessage(si, strLog.ToString)
    	            Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
    	        End Try
    	        
    	    End Function	
    		#End Region
    		
    		
    		#Region "DataTableToFile"
    		Public Function DataTableToFile(ByVal si As SessionInfo, ByVal dt As DataTable, ByVal strFilePath As String, ByVal listColToSkip As List(Of String), ByVal Optional strSeparator As String = ",") As XFFileEx
    			
    			Dim strDt As String = Me.PrintDataTable(si, dt, strSeparator, 0, False, listColToSkip, """")
    			Dim bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(strDt)
    			
    			Dim fileinfo As New XFFileInfo(fileSystemLocation.ApplicationDatabase, strFilePath)
    			Dim xfFile As New XFFile(fileinfo, String.Empty, bytes)
    			BRApi.FileSystem.InsertOrUpdateFile(si, xffile)
    			
    			Dim xfeFile As New XFFileEx(xffile, DataAccessLevel.AllAccess, False,String.Empty)
    			Return xfeFile
    			
    		End Function
    		#End Region