Forum Discussion

anmolmalviya05's avatar
anmolmalviya05
Super User
1 year ago
Solved

SSRS Report in txt format

I have created a ssrs report and end user wants the result in txt format.
I tried to export the report in csv and then convert it into txt tab delimited format.
But due to this the data and column name are getting misplaced.
Is there any way so that the data will remain aligned to Column name.

  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi anmolmalviya05 

     

    Thanks for the reply from lbendlin, please allow me to provide another insight:

     

    You can use PowerShell to convert the csv file to txt file. The test is as follows for your reference:

     

    This is the cvs file I exported in SSRS.

     

    code in PowerShell:

    # Define CSV file path
    $csvFilePath = "Disk:\path\to\your\file.csv"
    # Define output TXT file path
    $txtFilePath = "Disk:\path\to\your\file.txt"
    
    # Read CSV file
    $csvData = Import-Csv -Path $csvFilePath -Delimiter ','
    
    # Get the maximum length of each column
    $maxLengths =$csvData[0].PSObject.Properties.Name.ForEach({
        $maxLength =$_.Length
        foreach ($row in $csvData) {
            $length =$row.$_.ToString().Length
            if ($length -gt $maxLength) {
                $maxLength =$length
            }
        }
        $maxLength
    })
    
    # Convert column names to a string with tab delimiters
    $columnNames =$csvData[0].PSObject.Properties.Name.ForEach({
        $_.PadRight($maxLengths[$csvData[0].PSObject.Properties.Name.IndexOf($_)])
    }) -join "`t"
    
    # Convert CSV data to strings with tab delimiters, ensuring alignment
    $txtData =$csvData.ForEach({
        $_.PSObject.Properties.ForEach({
            $_.Value.ToString().PadRight($maxLengths[$csvData[0].PSObject.Properties.Name.IndexOf($_.Name)])
        }) -join "`t"
    })
    
    # Combine column names and CSV data
    $txtContent =$columnNames + "`r`n" + ($txtData -join "`r`n")
    
    # Write content to TXT file
    $txtContent | Out-File -FilePath $txtFilePath -Encoding ASCII

     

    Output:

     

    Best Regards,
    Yulia Xu

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

3 Replies

  • The whole point of a SSRS report (paginated report) is the page formatting with headers and footers etc.  It will be very hard if not impossible to convert that back into plain CSV.   Can't you export the CSV from the data source directly?

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi anmolmalviya05 

     

    Thanks for the reply from lbendlin, please allow me to provide another insight:

     

    You can use PowerShell to convert the csv file to txt file. The test is as follows for your reference:

     

    This is the cvs file I exported in SSRS.

     

    code in PowerShell:

    # Define CSV file path
    $csvFilePath = "Disk:\path\to\your\file.csv"
    # Define output TXT file path
    $txtFilePath = "Disk:\path\to\your\file.txt"
    
    # Read CSV file
    $csvData = Import-Csv -Path $csvFilePath -Delimiter ','
    
    # Get the maximum length of each column
    $maxLengths =$csvData[0].PSObject.Properties.Name.ForEach({
        $maxLength =$_.Length
        foreach ($row in $csvData) {
            $length =$row.$_.ToString().Length
            if ($length -gt $maxLength) {
                $maxLength =$length
            }
        }
        $maxLength
    })
    
    # Convert column names to a string with tab delimiters
    $columnNames =$csvData[0].PSObject.Properties.Name.ForEach({
        $_.PadRight($maxLengths[$csvData[0].PSObject.Properties.Name.IndexOf($_)])
    }) -join "`t"
    
    # Convert CSV data to strings with tab delimiters, ensuring alignment
    $txtData =$csvData.ForEach({
        $_.PSObject.Properties.ForEach({
            $_.Value.ToString().PadRight($maxLengths[$csvData[0].PSObject.Properties.Name.IndexOf($_.Name)])
        }) -join "`t"
    })
    
    # Combine column names and CSV data
    $txtContent =$columnNames + "`r`n" + ($txtData -join "`r`n")
    
    # Write content to TXT file
    $txtContent | Out-File -FilePath $txtFilePath -Encoding ASCII

     

    Output:

     

    Best Regards,
    Yulia Xu

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.