<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" ><channel><title>Do Know Evil - A Blog by Tyler Mulligan &#187; Excel</title> <atom:link href="http://www.doknowevil.net/category/computers/software/microsoft/excel/feed/" rel="self" type="application/rss+xml" /><link>http://www.doknowevil.net</link> <description>Tips and Tricks About Computers, Web Development, Linux, the Internet and the Like</description> <lastBuildDate>Sat, 16 Jul 2011 01:25:35 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.5</generator> <item><title>Using PHP to Make Excel Easier</title><link>http://www.doknowevil.net/2007/05/12/using-php-to-make-excel-easier/</link> <comments>http://www.doknowevil.net/2007/05/12/using-php-to-make-excel-easier/#comments</comments> <pubDate>Sat, 12 May 2007 15:38:39 +0000</pubDate> <dc:creator>Tyler Mulligan</dc:creator> <category><![CDATA[Excel]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://www.doknowevil.net/?p=12</guid> <description><![CDATA[Recently, I was presented with the task of counting the number times filenames repeated in an excel document. Being a 16,000+ row file, with well over 500 unique filenames, there was no simple solution in excel. Having some experience exporting excel documents using PHP, I knew importing them wouldn’t be too hard. A few google]]></description> <content:encoded><![CDATA[<p>Recently, I was presented with the task of counting the number times filenames repeated in an excel document.  Being a 16,000+ row file, with well over 500 unique filenames, there was no simple solution in excel.</p><p>Having some experience exporting excel documents using PHP, I knew importing them wouldn’t be too hard.  A few google searches later and I came across <a href="http://sourceforge.net/projects/xlreader" target="_blank">Excel Reader</a> which fit the bill perfectly.</p><p>After sorting my excel document by filename, I created a loop in PHP that would read the first filename, count the number of times it repeated until it changed, then append that data to a string that would be exported to a new excel document once it was done reading the data.</p><pre class="brush:php">
// Starting at 2 to skip the headers | +1 so it compares the last row
for ($i = 2; $i &lt;= $data-&gt;sheets[0][&#039;numRows&#039;]+1; $i++) {
	if ($data-&gt;sheets[0][&#039;cells&#039;][$i][3]==$curFileName) {
		$curFileCount++;
	} else {
		// Output the data line:
&quot;Device&quot;,&quot;User&quot;,&quot;Filename&quot;,&quot;Filecount&quot;
		$stringData =
&quot;\&quot;&quot;.$data-&gt;sheets[0][&#039;cells&#039;][$i-1][1].&quot;\&quot;,\&quot;&quot;.$data-&gt;sheets[0][&#039;cells&#039;][$i
-1][2].&quot;\&quot;,\&quot;&quot;.$data-&gt;sheets[0][&#039;cells&#039;][$i-1][3].&quot;\&quot;,\&quot;&quot;.$curFileCount.&quot;\&quot;\
n&quot;;
		fwrite($fh, $stringData);

		// Reset the count and update the curFileName
		$curFileName = $data-&gt;sheets[0][&#039;cells&#039;][$i][3];
		$curFileCount=1;
	}
	echo &quot;Row: &quot;.$i.&quot;\n&quot;;
}</pre><p>Of course&#8230; I didn&#8217;t have just one of these files but 6 and manually entering each filename would be a waste of my time.  My software solution for this was to create an &#8220;Input&#8221; and &#8220;Output&#8221; folder and have PHP cycle through the &#8220;Input&#8221; folder.</p><pre class="brush:php">
// For every file in the dir &quot;Input&quot;, generate a report
if ($handle = opendir(&#039;Input&#039;)) {
	while (false !== ($orgFileName = readdir($handle))) {
		if($orgFileName != &#039;..&#039; &amp;&amp; $orgFileName != &#039;.&#039;) {

			// Echo the file we&#039;re reading, then read that file
			echo $orgFileName.&quot;\n&quot;;
			$data-&gt;read(&quot;Input/&quot;.$orgFileName);</pre><p>Eventually it all came together as such:</p><pre class="brush:php">
&lt;?php

// Don&#039;t change any of this
require_once &#039;Excel/reader.php&#039;;
$data = new Spreadsheet_Excel_Reader();
$data-&gt;setOutputEncoding(&#039;CP1251&#039;);

// For every file in the dir &quot;Input&quot;, generate a report
if ($handle = opendir(&#039;Input&#039;)) {
	while (false !== ($orgFileName = readdir($handle))) {
		if($orgFileName != &#039;..&#039; &amp;&amp; $orgFileName != &#039;.&#039;) {

			// Echo the file we&#039;re reading
			echo $orgFileName.&quot;\n&quot;;
			$data-&gt;read(&quot;Input/&quot;.$orgFileName);

			// Set the first filename to compare
			$curFileName =
$data-&gt;sheets[0][&#039;cells&#039;][2][3]==$curFileName;
			$curFileCount = 0;

			// Create a file and open it for writing
			$myFile =
&quot;Output/File_Count-&quot;.str_replace(&quot;.xls&quot;,&quot;.csv&quot;,$orgFileName);
			$fh = fopen($myFile, &#039;w&#039;) or die(&quot;can&#039;t open file&quot;);

			// Starting at 2 to skip the headers | +1 so it
compares the last row
			for ($i = 2; $i &lt;= $data-&gt;sheets[0][&#039;numRows&#039;]+1;
$i++) {
				if
($data-&gt;sheets[0][&#039;cells&#039;][$i][3]==$curFileName) {
					$curFileCount++;
				} else {
					// Output the data line:
&quot;Device&quot;,&quot;User&quot;,&quot;Filename&quot;,&quot;Filecount&quot;
					$stringData =
&quot;\&quot;&quot;.$data-&gt;sheets[0][&#039;cells&#039;][$i-1][1].&quot;\&quot;,\&quot;&quot;.$data-&gt;sheets[0][&#039;cells&#039;][$i
-1][2].&quot;\&quot;,\&quot;&quot;.$data-&gt;sheets[0][&#039;cells&#039;][$i-1][3].&quot;\&quot;,\&quot;&quot;.$curFileCount.&quot;\&quot;\
n&quot;;
					fwrite($fh, $stringData);

					// Reset the count and update the
curFileName
					$curFileName =
$data-&gt;sheets[0][&#039;cells&#039;][$i][3];
					$curFileCount=1;
				}
				echo &quot;Row: &quot;.$i.&quot;\n&quot;;
			}
			// Close file
			fclose($fh);
		}
	}
}
?&gt;</pre><p>My apologies for the less than perfect code, I was on a deadline and perfect code wasn&#8217;t a requirement :).</p><p>Here are some screenshots of the script in action.</p><p><a href="http://www.doknowevil.net/wp-content/uploads/2007/05/php_filecount-11.png" title="PHP Excel File Count - Screenshot 1"><img src="http://www.doknowevil.net/wp-content/uploads/2007/05/php_filecount-11.png" alt="PHP Excel File Count - Screenshot 1" border="0" width="50%" height="50%" /></a></p><p><a href="http://www.doknowevil.net/wp-content/uploads/2007/05/php_filecount-21.png" title="PHP Excel File Count - Screenshot 2"><img src="http://www.doknowevil.net/wp-content/uploads/2007/05/php_filecount-21.png" alt="PHP Excel File Count - Screenshot 2" border="0" width="50%" height="50%" /></a></p><p><a href="http://www.doknowevil.net/wp-content/uploads/2007/05/php_filecount-31.png" title="PHP Excel File Count - Screenshot 3"><img src="http://www.doknowevil.net/wp-content/uploads/2007/05/php_filecount-31.png" alt="PHP Excel File Count - Screenshot 3" border="0" /></a></p><p></p> ]]></content:encoded> <wfw:commentRss>http://www.doknowevil.net/2007/05/12/using-php-to-make-excel-easier/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Object Caching 383/393 objects using disk

Served from: www.doknowevil.net @ 2012-02-04 04:05:00 -->
