{"id":152,"date":"2019-08-29T07:05:58","date_gmt":"2019-08-29T13:05:58","guid":{"rendered":"https:\/\/cdbackslash.wpengine.com\/?p=152"},"modified":"2019-08-29T07:10:13","modified_gmt":"2019-08-29T13:10:13","slug":"convert-csv-to-xlsx-via-powershell","status":"publish","type":"post","link":"https:\/\/www.cdbackslash.com\/?p=152","title":{"rendered":"Convert CSV to XLSX via PowerShell"},"content":{"rendered":"\n<p>Revisiting an old friend as some date format junk was needing adjustment and leading 0s from txt file were being dropped. Script originally brought txt into xlsx and all data was &#8220;General&#8221; format. Tweak noted below brought all data into xlsx as &#8220;Text&#8221; format.<\/p>\n\n\n\n<p>Changed:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$query.TextFileColumnDataTypes = ,1 * $worksheet.Cells.Columns.Count\nto\n$query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count<\/pre>\n\n\n\n<p>Entire script with stuff removed to protect the martians:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Define locations and delimiter\n$SourceFolder = \"\\\\server\\share\\path1\\path2\"\n$csv = $SourceFolder+\"\\sourcefile.txt\" #Location of the source file\n$xlsx = $SourceFolder+\"\\Cool Filename \"+(Get-Date -f dd)+\".\"+(Get-Date -f MM)+\".\"+(Get-Date -f yyyy)+\".xlsx\" #Desired location of output\n$delimiter = \"|\" #Specify the delimiter used in the file\n\n# REMOVE ALL .XLSX files in the folder\nRemove-Item $SourceFolder\\* -include *.xlsx\n\n# Create a new Excel workbook with one empty sheet\n$excel = New-Object -ComObject excel.application \n$workbook = $excel.Workbooks.Add(1)\n$worksheet = $workbook.worksheets.Item(1)\n\n# Build the QueryTables.Add command and reformat the data\n$TxtConnector = (\"TEXT;\" + $csv)\n$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range(\"A1\"))\n$query = $worksheet.QueryTables.item($Connector.name)\n$query.TextFileOtherDelimiter = $delimiter\n$query.TextFileParseType  = 1\n$query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count\n$query.AdjustColumnWidth = 1\n\n# Execute &amp; delete the import query\n$query.Refresh()\n$query.Delete()\n\n# Save &amp; close the Workbook as XLSX.\n$Workbook.SaveAs($xlsx,51)\n$excel.Quit()\n\n# Legacy file process rename tasks\ndir \\\\server\\share\\path1\\file.txt | Rename-Item -NewName {\"New_Filename_\"+(Get-Date -f yyyyMMdd)+\".csv\"}\ndir \\\\server\\share\\path1\\file2.txt | Rename-Item -NewName {\"Another_File_\"+(Get-Date -f yyyyMMdd)+\".csv\"}<\/pre>\n\n\n\n<p>Source: <a href=\"https:\/\/code.adonline.id.au\/csv-to-xlsx-powershell\/\">https:\/\/code.adonline.id.au\/csv-to-xlsx-powershell\/<\/a><\/p>\n\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=maxwellnation\" class=\"twitter-share-button\">Tweet<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Revisiting an old friend as some date format junk was needing adjustment and leading 0s from txt file were being dropped. Script originally brought txt into xlsx and all data was &#8220;General&#8221; format. Tweak noted below brought all data into xlsx as &#8220;Text&#8221; format. Changed: $query.TextFileColumnDataTypes = ,1 * $worksheet.Cells.Columns.Count to $query.TextFileColumnDataTypes = ,2 * &hellip; <a href=\"https:\/\/www.cdbackslash.com\/?p=152\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Convert CSV to XLSX via PowerShell&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-152","post","type-post","status-publish","format-standard","hentry","category-stuff"],"_links":{"self":[{"href":"https:\/\/www.cdbackslash.com\/index.php?rest_route=\/wp\/v2\/posts\/152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cdbackslash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cdbackslash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cdbackslash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cdbackslash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=152"}],"version-history":[{"count":0,"href":"https:\/\/www.cdbackslash.com\/index.php?rest_route=\/wp\/v2\/posts\/152\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.cdbackslash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cdbackslash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cdbackslash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}