{"id":65653,"date":"2007-01-26T00:28:00","date_gmt":"2007-01-26T00:28:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/01\/26\/hey-scripting-guy-how-can-i-convert-an-office-excel-spreadsheet-to-xml\/"},"modified":"2007-01-26T00:28:00","modified_gmt":"2007-01-26T00:28:00","slug":"hey-scripting-guy-how-can-i-convert-an-office-excel-spreadsheet-to-xml","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-convert-an-office-excel-spreadsheet-to-xml\/","title":{"rendered":"Hey, Scripting Guy! How Can I Convert an Office Excel Spreadsheet to XML?"},"content":{"rendered":"<h2><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" class=\"nearGraphic\" \/> <\/h2>\n<p>Hey, Scripting Guy! How can I convert an Office Excel spreadsheet to XML?<\/p>\n<p>&#8212; KE<\/p>\n<p><img decoding=\"async\" height=\"5\" width=\"5\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" alt=\"Spacer\" border=\"0\" \/><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" class=\"nearGraphic\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" height=\"288\" width=\"120\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" align=\"right\" alt=\"Script Center\" border=\"0\" title=\"Script Center\" class=\"farGraphic\" \/><\/a><\/p>\n<p>Hey, KE. We apologize in advance if today&rsquo;s column seems a little rushed; at the moment, we&rsquo;re sitting in an office, surrounded by boxes and packing crates and waiting for the movers to come in and clear everything out. And no, the Scripting Guy who writes this column hasn&rsquo;t been fired, he &ndash; just a second, we need to double-check something &hellip;.<\/p>\n<p>No, we were right: the Scripting Guy who writes this column hasn&rsquo;t been fired. Instead, it&rsquo;s Moving Day, and he and his fellow Scripting Guys are about to move to new offices in a new building. For those of you who like to keep track of this sort of thing, for the Scripting Guy who writes this column that will make, in the past six years, 10 offices in 4 different buildings. <\/p>\n<p>And, sadly, no, that&rsquo;s <i>not<\/i> a record for Microsoft. Not even close.<\/p>\n<p>At any rate, the reason we&rsquo;re feeling a little rushed is because we don&rsquo;t exactly know when the movers will show up. Adopting the same highly-beloved customer service strategy employed by the TV cable companies, today&rsquo;s move will start promptly at noon. Unless, of course, it starts later. Or maybe even earlier; after all, we were required to have everything packed up a day in advance. If today&rsquo;s column suddenly breaks off in the middle of a sentence, well, then you&rsquo;ll know the movers came a little earlier than expected.<\/p>\n<p>And because we may (or may not) be pressed for time today we decided to pick an easy question to answer. How easy? This easy:<\/p>\n<pre class=\"codeSample\">Const xlXMLSpreadsheet = 46\n\nSet objExcel = CreateObject(\"Excel.Application\")\nobjExcel.Visible = True\n\nSet objWorkbook = objExcel.Workbooks.Add()\nSet objWorksheet = objWorkbook.Worksheets(1)\n\nk = 1\n\nFor i = 1 to 5\n    For j = 1 to 3\n        objWorksheet.Cells(i,j) = k\n        k = k + 1\n    Next\nNext\n\nobjWorkbook.SaveAs \"C:\\Scripts\\Test.xml\", xlXMLSpreadsheet        <\/pre>\n<p>Sure, we can explain how this all works; we have lots of time. (Unless we don&rsquo;t.) As you can see, we start out by defining a constant named xlXMLSpreadsheet and setting the value to 46; we&rsquo;ll use this constant to tell Excel that we want to save the spreadsheet as XML. After defining the constant we create an instance of the <b>Excel.Application<\/b> object and then set the <b>Visible<\/b> property to True; that gives us a running instance of Excel that we can see onscreen.<\/p>\n<p>Of course, it&rsquo;s no fun to have a running instance of Excel unless we actually <i>do<\/i> something with that instance. Consequently, we use the following two lines of code to add a new workbook and a new worksheet to our running instance of Excel:<\/p>\n<pre class=\"codeSample\">Set objWorkbook = objExcel.Workbooks.Add()\nSet objWorksheet = objWorkbook.Worksheets(1)<\/pre>\n<p>We then use this block of code to add some (in this case, meaningless) data to the worksheet:<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">k = 1\n\nFor i = 1 to 5\n    For j = 1 to 3\n        objWorksheet.Cells(i,j) = k\n        k = k + 1\n    Next\nNext\n<\/pre>\n<p>Now we have an Excel spreadsheet filled with all sorts of interesting data. That&rsquo;s nice, but how do we save that data as XML? Believe it or not, that takes only a single line of code:<\/p>\n<pre class=\"codeSample\">objWorkbook.SaveAs \"C:\\Scripts\\Test.xml\", xlXMLSpreadsheet<\/pre>\n<p>As you can see, all we&rsquo;re doing here is calling the <b>SaveAs<\/b> method and passing the method a pair of parameters: the complete path to our new XML file (C:\\Scripts\\Test.xml) and the constant xlXMLSpreadsheet. Will that <i>really<\/i> give us a file that can be opened not only by Excel but also by Notepad or by an XML editor? Of course it will; here&rsquo;s an excerpt from the data section of the file we created:<\/p>\n<pre class=\"codeSample\"><pre class=\"codeSample\">&lt;Worksheet ss:Name=\"Sheet1\"&gt;\n  &lt;Table ss:ExpandedColumnCount=\"3\" ss:ExpandedRowCount=\"5\" x:FullColumns=\"1\"\n   x:FullRows=\"1\"&gt;\n   &lt;Row&gt;\n    &lt;Cell&gt;&lt;Data ss:Type=\"Number\"&gt;1&lt;\/Data&gt;&lt;\/Cell&gt;\n    &lt;Cell&gt;&lt;Data ss:Type=\"Number\"&gt;2&lt;\/Data&gt;&lt;\/Cell&gt;\n    &lt;Cell&gt;&lt;Data ss:Type=\"Number\"&gt;3&lt;\/Data&gt;&lt;\/Cell&gt;\n   &lt;\/Row&gt;\n   &lt;Row&gt;\n    &lt;Cell&gt;&lt;Data ss:Type=\"Number\"&gt;4&lt;\/Data&gt;&lt;\/Cell&gt;\n    &lt;Cell&gt;&lt;Data ss:Type=\"Number\"&gt;5&lt;\/Data&gt;&lt;\/Cell&gt;\n    &lt;Cell&gt;&lt;Data ss:Type=\"Number\"&gt;6&lt;\/Data&gt;&lt;\/Cell&gt;\n   &lt;\/Row&gt;\n<\/pre>\n<p>Yes, we know; what you&rsquo;re <i>really<\/i> curious about is this: after years of working out of tiny, windowless offices is the Scripting Guy who writes this column finally going to get the working environment he deserves? Let&rsquo;s put it this way: the <i>last<\/i> thing the Scripting Guy who writes this column wants to get is what he deserves. If you know what we mean.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I convert an Office Excel spreadsheet to XML? &#8212; KE Hey, KE. We apologize in advance if today&rsquo;s column seems a little rushed; at the moment, we&rsquo;re sitting in an office, surrounded by boxes and packing crates and waiting for the movers to come in and clear everything out. And [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[710,711,48,49,3,5],"class_list":["post-65653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-excel-spreadsheet","tag-excel-application","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I convert an Office Excel spreadsheet to XML? &#8212; KE Hey, KE. We apologize in advance if today&rsquo;s column seems a little rushed; at the moment, we&rsquo;re sitting in an office, surrounded by boxes and packing crates and waiting for the movers to come in and clear everything out. And [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=65653"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65653\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=65653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}