CSV zu HTML konvertieren

Wer eine CSV wie bei Convert-Table2HTML zu HTML konvertieren will, ohne den Zwischenweg des CSV-Imports zu gehen, kann diese Funktion nutzen.

function Convert-CSV2HTML
{
    <#
	.SYNOPSIS
	    Convert a CSV File to a HTML-Table.

	.DESCRIPTION
	    The Convert-CSV2HTML cmdlet converts a CSV File to a HTML-Table in order to send it via email and have it available directly within the email.
		
	.PARAMETER CSVFile
	    Specifies the path to the CSV file. 

	.PARAMETER Delimiter
	    Specifies the delimiter for the CSV File.
	
	.PARAMETER Encoding
	    Specifies the encoding of the CSV File. 
		
	.EXAMPLE
		PS C:\> Convert-CSV2HTML -CSVFile 'E:\Reports\DailyReport.csv' -Delimiter ';' -Encoding 'UTF8'

		
	.NOTES
		Author: Joachim Armbruster
		Blog  : http://PowerShell24.de/
	#>

	Param
	(
		[parameter(Mandatory=$true)][String]$CSVFile,
		[String]$Delimiter,
		[String]$Encoding
	)


	$Farbe1 = "bgcolor='#c5c5c5'"
	$Farbe2 = "bgcolor='#f0f0f0'"
	

	$CSVContent = Import-Csv $CSVFile -Delimiter $Delimiter -Encoding $Encoding
	$NoteProperties = $CSVContent | Get-Member -Type *Property*
	
	$html = "<table border='1'>"
	$html += "<tr>"
	
	
	foreach($NoteProperty in $NoteProperties)
	{
		$NotePropertyName = $NoteProperty.Name
		$html += "<th>$NotePropertyName</th>"
	}	
	$html += "</tr>"
	
	$FarbenCounter = 0
	
	
	for ($i = 0; $i -lt $CSVContent.Count; $i++)
	{
	
		if(($FarbenCounter % 2) -eq 0)
		{
			$bgcolor = $Farbe1
		}else
		{
			$bgcolor = $Farbe2
		}
		
		$html += "<tr "  + $bgcolor + ">"
			foreach($NoteProperty in $NoteProperties)
		{
			$NotePropertyName = $NoteProperty.Name
			$Eintrag = $CSVContent[$i].$NotePropertyName
			$html += "<td>$Eintrag</td>"
		}	
			
		$html += "</tr>"
		$FarbenCounter++
	
	}
	
	$html += "</table>"
	return $html

}