I had the need to store data's into a Microsoft Excel compatible file. The first attempt is to use the Excel COM object model. This is not a good solution because: PowerShell runs very often on Servers or clients without a Microsoft Office / Excel installation. The use of the Excel COM Object can cause errors inside a Scheduled Task. Excel can read and store CSV data. The second attempt is to use CSV data (with Export-CSV) This is even not a good solution because:
$Null = [Reflection.Assembly]::LoadWithPartialName(
"WindowsBase"
)
# create the main package on disk with filemode create
$exPkg = [System.IO.Packaging.Package]::Open
"C:\test.xlsx"
, [System.IO.FileMode]::Create)
# create the Workbook.xml part XML document
# create empty XML Document
$xl_Workbook_xml = New-Object System.Xml.XmlDocument
# Obtain a reference to the root node, and then add the XML declaration.
$XmlDeclaration = $xl_Workbook_xml.CreateXmlDeclaration(
"1.0"
,
"UTF-8"
"yes"
$Null = $xl_Workbook_xml.InsertBefore($XmlDeclaration, $xl_Workbook_xml.DocumentElement)
# Create and append the workbook node to the document.
$workBookElement = $xl_Workbook_xml.CreateElement(
"workbook"
# add the office open xml namespaces to the XML document
$Null = $workBookElement.SetAttribute(
"xmlns"
"http://schemas.openxmlformats.org/spreadsheetml/2006/main"
"xmlns:r"
"http://schemas.openxmlformats.org/officeDocument/2006/relationships"
$Null = $xl_Workbook_xml.AppendChild($workBookElement)
# Create and append the sheets node to the workBook node.
$Null = $xl_Workbook_xml.DocumentElement.AppendChild($xl_Workbook_xml.CreateElement(
"sheets"
))
# create the workbook.xml package part
# create URI for workbook.xml package part
$Uri_xl_workbook_xml = New-Object System.Uri -ArgumentList (
"/xl/workbook.xml"
, [System.UriKind]::Relative)
# create workbook.xml part
$Part_xl_workbook_xml = $exPkg.CreatePart($Uri_xl_workbook_xml,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"
# get writeable stream from workbook.xml part
$dest = $part_xl_workbook_xml.GetStream([System.IO.FileMode]::Create,[System.IO.FileAccess]::Write)
# write workbook.xml XML document to part stream
$xl_workbook_xml.Save($dest)
# create package general main relationships
$Null = $exPkg.CreateRelationship($Uri_xl_workbook_xml, [System.IO.Packaging.TargetMode]::Internal,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
"rId1"
# create worksheet XML document
$New_Worksheet_xml = New-Object System.Xml.XmlDocument
# obtain a reference to the root node, and then add the XML declaration.
$XmlDeclaration = $New_Worksheet_xml.CreateXmlDeclaration(
$Null = $New_Worksheet_xml.InsertBefore($XmlDeclaration, $New_Worksheet_xml.DocumentElement)
# create and append the worksheet node to the document.
$workSheetElement = $New_Worksheet_xml.CreateElement(
"worksheet"
# add the Excel related office open xml namespaces to the XML document
$Null = $workSheetElement.SetAttribute(
$Null = $New_Worksheet_xml.AppendChild($workSheetElement)
# create and append the sheetData node to the worksheet node.
$Null = $New_Worksheet_xml.DocumentElement.AppendChild($New_Worksheet_xml.CreateElement(
"sheetData"
# create the worksheet package part
# create URI for worksheet package part
$Uri_xl_worksheets_sheet_xml = New-Object System.Uri -ArgumentList (
"/xl/worksheets/$NewWorkSheetPartName"
# create worksheet part
$Part_xl_worksheets_sheet_xml = $exPkg.CreatePart($Uri_xl_worksheets_sheet_xml,
"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"
# get writeable stream from part
$dest = $part_xl_worksheets_sheet_xml.GetStream([System.IO.FileMode]::Create,[System.IO.FileAccess]::Write)
# write $New_Worksheet_xml XML document to part stream
$New_Worksheet_xml.Save($dest)
# create workbook to worksheet relationship
$Null = $WorkBookPart.CreateRelationship($Uri_xl_worksheets_sheet_xml, [System.IO.Packaging.TargetMode]::Internal,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
, $NewWorkBookRelId)