santosh kumar patro
santosh kumar patro

Reputation: 8233

How to add formatting to header column of csv file using powershell

I am able to run a stored procedure and export it to csv using the following code:

function TestSQLStoredPrc()
{
$connString = "Data Source=xxxxxx,1433;Initial Catalog=TestDB;User Id=TestUser; Password=YYYYYYY;"
$Reference = Read-Host "Enter Name";
$QueryText = "exec dbo.GetUsersCountByName 'Test'"; 
$SqlConnection = new-object System.Data.SqlClient.SqlConnection;
$SqlConnection.ConnectionString = $connString;

$SqlCommand = $SqlConnection.CreateCommand();
$SqlCommand.CommandText = "EXEC dbo.GetUsersCountByName 'Test'";
$DataAdapter = new-object System.Data.SqlClient.SqlDataAdapter $SqlCommand
$dataset = new-object System.Data.Dataset 
Write-Host $DataAdapter.Fill($dataset) ' records have been exported.' 
$dataset.Tables[0] | Export-CSV C:\MyReport.csv -Force -NoTypeInformation
Write-Host 'New report C:\MyReport.csv has been successfully generated'
}

TestSQLStoredPrc

I am able to get a csv file as output. But I need to apply background color and formatting to the header column of the output csv file.

Can anyone help me to resolve this issue by providing some sample example.

Thanks & Regards, Santosh Kumar Patro

Upvotes: 0

Views: 1261

Answers (1)

mjolinor
mjolinor

Reputation: 68331

You can format it and save it as HTML, but you cannot do that and have it remain a .csv file. CSV is, by definition plaint text - there is no provision for storing foratting information in the file, only header names and values.

Upvotes: 4

Related Questions