Supplies are limited. Contact info@espc.tech right away to save your spot before the conference sells out.
Get your discountScore big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount
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.
Solved! Go to Solution.
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.
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.
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?