{"id":2549,"date":"2015-02-24T21:03:42","date_gmt":"2015-02-24T20:03:42","guid":{"rendered":"http:\/\/michlstechblog.info\/blog\/?p=2549"},"modified":"2015-02-25T16:22:56","modified_gmt":"2015-02-25T15:22:56","slug":"powershell-export-excel-workbook-as-csv-file","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/powershell-export-excel-workbook-as-csv-file\/","title":{"rendered":"Powershell: Export a Excel Worksheet as csv file"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_2549 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_2549')){$('.twoclick_social_bookmarks_post_2549').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"Powershell%3A%20Export%20a%20Excel%20Worksheet%20as%20csv%20file\",\"the_excerpt\":\"Hi%2C%0D%0A%0D%0Awith%20a%20few%20lines%20Powershell%20code%20it%20is%20possible%20to%20export%20a%20Excel%20Workbook%20as%20a%20csv%20file.%0D%0ADefine%20your%20excel%20list%0D%0A%0D%0A%5Bpowershell%5D%0D%0A%24sExcelFile%3D%26quot%3BD%3A%5Ctemp%5CExcelFile.xlsx%26quot%3B%0D%0A%24sCSVFile%3D%26quot%3BD%3A%5Ctemp%5Clist.csv%26quot%3B%0D%0A%5B%2Fpowershell%5D%0D%0A%0D%0AOpen%20Excel%20using%20DCOM%0D%0A%20%28more%26hellip%3B%29\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/powershell-export-excel-workbook-as-csv-file\\\/\",\"post_id\":2549,\"post_title_referrer_track\":\"Powershell%3A+Export+a+Excel+Worksheet+as+csv+file\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi,<\/p>\n<p>with a few lines Powershell code it is possible to export a Excel Workbook as a csv file.<br \/>\nDefine your excel list<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$sExcelFile=&quot;D:\\temp\\ExcelFile.xlsx&quot;\r\n$sCSVFile=&quot;D:\\temp\\list.csv&quot;\r\n<\/pre>\n<p>Open Excel using DCOM<br \/>\n<!--more--><\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n# Get COM Object\r\n$oExcel = New-Object -ComObject &quot;Excel.Application&quot;\r\n# Should Excel be visible?\r\n$oExcel.Visible = $true\r\n# and open excel file\r\n$oExcelDoc = $oExcel.Workbooks.Open($sExcelFile)\r\n<\/pre>\n<p>Now you have to select and activate the worksheet you want to export. Index is starting by 1<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n# Open 1st Worksheet\r\n$oWorksheet = $oExcelDoc.Worksheets.item(1)\r\n# Activate, show it\r\n$oWorksheet.Activate()\r\n<\/pre>\n<p>And use the SaveAs method to export the worksheet as (windows) csv<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$oExcelDoc.SaveAs($sCSVFile,&#x5B;Microsoft.Office.Interop.Excel.XlFileFormat]::xlCSVWindows)\r\n<\/pre>\n<p>To get a list of all possible export formats\u00a0 see my my previous <a title=\"Powershell: Error while calling the SaveAs Method of some Office Applications\" href=\"http:\/\/michlstechblog.info\/blog\/powershell-error-while-calling-the-saveas-method-of-some-office-applications\/\">post<\/a>.<\/p>\n<p>Finally close Excel<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$oExcelDoc.Close($false)\r\nStart-Sleep 1\r\n# Cleanup COM\r\n&#x5B;System.Runtime.InteropServices.Marshal]::ReleaseComObject($oWorksheet)|out-null\r\n$oWorksheet=$null\r\nStart-Sleep 1\r\n# Cleanup COM\r\n&#x5B;System.Runtime.InteropServices.Marshal]::ReleaseComObject($oExcelDoc)|out-null\r\n$oExcelDoc=$null\r\n# Close Excel\r\n$oExcel.Quit()\r\nStart-Sleep 1\r\n# Cleanup COM\r\n&#x5B;System.Runtime.InteropServices.Marshal]::ReleaseComObject($oExcel)|out-null\r\n$oExcel=$null\r\n&#x5B;GC]::Collect()\r\n&#x5B;GC]::WaitForPendingFinalizers()\r\n<\/pre>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, with a few lines Powershell code it is possible to export a Excel Workbook as a csv file. Define your excel list $sExcelFile=&quot;D:\\temp\\ExcelFile.xlsx&quot; $sCSVFile=&quot;D:\\temp\\list.csv&quot; Open Excel using DCOM<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[344],"tags":[395,134,394,133,20],"class_list":["post-2549","post","type-post","status-publish","format-standard","hentry","category-powershell-scripting","tag-csv","tag-excel","tag-export","tag-powershell","tag-windows-2"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/2549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=2549"}],"version-history":[{"count":6,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/2549\/revisions"}],"predecessor-version":[{"id":6564,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/2549\/revisions\/6564"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=2549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=2549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=2549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}