Forum Discussion

anmolmalviya05's avatar
anmolmalviya05
Icon for Super User rankSuper User
2 years 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 ...
  • Anonymous's avatar
    Anonymous
    2 years 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.

  • anmolmalviya05's avatar
    anmolmalviya05
    2 years ago

    Hi lbendlin, Anonymous Thanks for your response.

    The powershell script worked.