{"id":274,"date":"2011-06-07T20:40:25","date_gmt":"2011-06-07T20:40:25","guid":{"rendered":"http:\/\/craftydba.com\/?p=274"},"modified":"2024-02-17T14:26:25","modified_gmt":"2024-02-17T14:26:25","slug":"excel-files","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=274","title":{"rendered":"Excel Files"},"content":{"rendered":"<p>Business Users like to have data in a spreadsheet format. Since Microsoft Office is installed world-wide, it will be a common request for data in a <a href=\"http:\/\/en.wikipedia.org\/wiki\/XLS_file\">XLS <\/a>format.<\/p>\n<p>Let&#8217;s make believe that we have the business problem to write information about the first five presidents to an XLS format.<\/p>\n<p>The <a href=\"http:\/\/en.wikipedia.org\/wiki\/Clich%C3%A9\">Clich\u00e9<\/a>, there are many ways to skin a cat, does apply to technical solutions. I have abstracted the details behind reading and writing files by encapsulating the logic into two classes for your use.\u00a0 These classes can be implemented in at least two ways.<\/p>\n<p>The <span style=\"color: #800000;\"><strong>first <\/strong><\/span>solution is to create a class using object oriented principles. The attributes of the class are very specific to the problem at hand, describing US presidents. Therefore, last name and first name are attributes.<\/p>\n<p>The <span style=\"color: #800000;\"><strong>second <\/strong><\/span>solution is to create a class using generic \/ abstract principles. The attributes of the class are not specific to the problem at hand. Therefore, spreadsheet name and definition are attributes.<\/p>\n<p>While I like the first solution since it describes the problem at hand in detail, I implemented the second class since it can be applied to a wider set of business problems.<\/p>\n<p>The write xls file class has the following properties and methods.<\/p>\n<ul>\n<li>OpenDataFile &#8211; Open a given file in write or append mode.<\/li>\n<li>CreateWorkSheet &#8211; Create a named worksheet with column attributes.<\/li>\n<li>PushData &#8211; Write a line of data to the given file.<\/li>\n<li>CloseDataFile &#8211; Close the given file.<\/li>\n<li>RemoveFile &#8211; Remove a given file.<\/li>\n<\/ul>\n<ul>\n<li>OneRec &#8211; Pipe delimited data stream to write as columns.<\/li>\n<li>SheetName &#8211; Name given to the worksheet<\/li>\n<li>SheetDef &#8211; Pipe delimited worksheet definition on SQL like code<\/li>\n<\/ul>\n<p>The read text file class has the following properties and methods.<\/p>\n<ul>\n<li>OpenDataFile &#8211; Open a given file in read mode.<\/li>\n<li>PullData &#8211; Return a line of data if not EOF.<\/li>\n<li>CloseDataFile &#8211; Close the given file.<\/li>\n<li>FileExists &#8211; Does a given file exist?<\/li>\n<\/ul>\n<ul>\n<li>EOF &#8211; a property that reflects if we are at the end of file.<\/li>\n<li>RECS &#8211; the total number of records (0 = nothing read, -1 = EOF, or current count).<\/li>\n<\/ul>\n<p>Both classes use <a href=\"http:\/\/en.wikipedia.org\/wiki\/ActiveX_Data_Objects\">ActiveX Data Objects<\/a> to manipulate the XLS files.\u00a0 If you need finer control over formatting, please look into the Microsoft Excel.Application object.<\/p>\n<p>Last but not least, you should get very familiar with creating Excel worksheets and summarizing data with pivot tables.\u00a0 These skills will make you an invaluable resource to the Business Line Community.<\/p>\n<p>The table below has starter code that you can use in your next Visual Basic (VB) Script.<\/p>\n<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\" width=\"600\" align=\"left\">\n<tbody>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/mod-read-xls-file.vbs_.txt\">mod-read-xls-file.vbs<\/a><\/td>\n<td style=\"border: thin solid gray;\">Read Xls File Class<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/mod-write-xls-file.vbs_.txt\">mod-write-xls-file.vbs<\/a><\/td>\n<td style=\"border: thin solid gray;\">Write Xls File Class<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/tst-read-xls-file.vbs.txt\">tst-read-xls-file.vbs<\/a><\/td>\n<td style=\"border: thin solid gray;\">Read Sample Program<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/tst-write-xls-file.vbs.txt\">tst-write-xls-file.vbs<\/a><\/td>\n<td style=\"border: thin solid gray;\">Write Sample Program<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/read.cmd.txt\">read.cmd<\/a><\/td>\n<td style=\"border: thin solid gray;\">Read Batch File<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/write.cmd.txt\">write.cmd<\/a><\/td>\n<td style=\"border: thin solid gray;\">Write Batch File<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/presidents.xls\">presidents.xls<\/a><\/td>\n<td style=\"border: thin solid gray;\">XLS Sample File<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\nThis is a screen shot of the read sample VB Script excuting in a MS DOS command shell.<br \/>\n<\/P><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/read-xls-screen-output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-286\" title=\"read-xls-screen-output\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/read-xls-screen-output-300x152.jpg\" alt=\"\" width=\"300\" height=\"152\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/read-xls-screen-output-300x152.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/read-xls-screen-output.jpg 678w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Business Users like to have data in a spreadsheet format. Since Microsoft Office is installed world-wide, it will be a common request for data in a XLS format. Let&#8217;s make believe that we have the business problem to write information about the first five presidents to an XLS format. The Clich\u00e9, there are many ways to skin a cat, does apply to technical solutions. I have abstracted the details behind reading and writing files by encapsulating the logic into two classes for your use.\u00a0 These classes can be implemented in&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[20,12,15,13,19],"class_list":["post-274","post","type-post","status-publish","format-standard","hentry","category-other","tag-excel-file","tag-free-code","tag-john-f-miner-iii","tag-vb-script-2","tag-xls-file"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=274"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}