Forum Discussion
SSRS Report in txt format
- Anonymous2 years ago
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 ASCIIOutput:
Best Regards,
Yulia XuIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- 2 years ago
Hi lbendlin, Anonymous Thanks for your response.
The powershell script worked.
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.
Hi lbendlin, Anonymous Thanks for your response.
The powershell script worked.