<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Convert csv to xls (excel) in php</title>
	<atom:link href="http://www.westwideweb.com/wp/2009/01/12/convert-csv-to-xls-excel-in-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.westwideweb.com/wp/2009/01/12/convert-csv-to-xls-excel-in-php/</link>
	<description>Vox Bass Tech Etc.</description>
	<lastBuildDate>Sun, 31 Jan 2010 08:06:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Francisco Garmendia</title>
		<link>http://www.westwideweb.com/wp/2009/01/12/convert-csv-to-xls-excel-in-php/comment-page-1/#comment-734</link>
		<dc:creator>Francisco Garmendia</dc:creator>
		<pubDate>Sun, 01 Nov 2009 22:54:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.westwideweb.com/wp/?p=489#comment-734</guid>
		<description>What Happened? Warning: fopen(xlsfile:/studentdetails.xls) [function.fopen]: failed to open stream: Invalid argument in C:\Inetpub\AppServ\www\excel\csv.php on line 45
Error opening xlsfile:/studentdetails.xls</description>
		<content:encoded><![CDATA[<p>What Happened? Warning: fopen(xlsfile:/studentdetails.xls) [function.fopen]: failed to open stream: Invalid argument in C:\Inetpub\AppServ\www\excel\csv.php on line 45<br />
Error opening xlsfile:/studentdetails.xls</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Poehler</title>
		<link>http://www.westwideweb.com/wp/2009/01/12/convert-csv-to-xls-excel-in-php/comment-page-1/#comment-665</link>
		<dc:creator>Robert Poehler</dc:creator>
		<pubDate>Sun, 02 Aug 2009 10:19:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.westwideweb.com/wp/?p=489#comment-665</guid>
		<description>you can extend the limits by editing your php.ini placed in /etc.</description>
		<content:encoded><![CDATA[<p>you can extend the limits by editing your php.ini placed in /etc.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jamie</title>
		<link>http://www.westwideweb.com/wp/2009/01/12/convert-csv-to-xls-excel-in-php/comment-page-1/#comment-649</link>
		<dc:creator>Jamie</dc:creator>
		<pubDate>Thu, 02 Jul 2009 03:48:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.westwideweb.com/wp/?p=489#comment-649</guid>
		<description>What happens when the csv file is greater than the 65K ish amount for excel 2003?   Does it continue writing the lines in a new sheet?</description>
		<content:encoded><![CDATA[<p>What happens when the csv file is greater than the 65K ish amount for excel 2003?   Does it continue writing the lines in a new sheet?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kireetpola</title>
		<link>http://www.westwideweb.com/wp/2009/01/12/convert-csv-to-xls-excel-in-php/comment-page-1/#comment-379</link>
		<dc:creator>kireetpola</dc:creator>
		<pubDate>Wed, 04 Feb 2009 02:35:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.westwideweb.com/wp/?p=489#comment-379</guid>
		<description>In my csv file the fields are separated by commas and I tried the above code directly specifying the file names instead of passing them as the command line arguments But I am getting the error

Warning: fopen(xlsfile:/studentdetails.xls) [function.fopen]: failed to open stream: Invalid argument in C:\webapplications\wamp\www\csvtoexcel.php on line 291
Error opening xlsfile:/studentdetails.xls 

there student details is the file to which the data needs to get entered and I would like to know whether the file needs to be created by us or the program itself creates it?

Here is the code:
Instead of including xls.php I have pasted the whole code directly.

= 4.3.2
 * @author      Ignatius Teo            
 * @copyright   (C)2004 act28.com       
 * @version     0.3
 * @date        20 Jan 2005
 * $Id: excel.php,v 1.3 2005/01/20 09:58:58 Owner Exp $
 */
class xlsStream
{
    /* private */
    var $position = 0;          // stream pointer
    var $mode = &quot;rb&quot;;           // default stream open mode
    var $xlsfilename = null;    // stream name
    var $fp = null;             // internal stream pointer to physical file
    var $buffer = null;         // internal write buffer
    var $endian = &quot;unknown&quot;;	// little &#124; unknown &#124; big endian mode
	var $bin = array(
		&quot;big&quot; =&gt; &quot;v&quot;,
		&quot;little&quot; =&gt; &quot;s&quot;,
		&quot;unknown&quot; =&gt; &quot;s&quot;,
	);

	/**
	 * detect server endian mode
	 * thanks to Charles Turner for picking this one up
	 * @access	private
	 * @params	void
	 * @returns	void
	 * @see		http://www.phpdig.net/ref/rn45re877.html
	 */
	function _detect()
	{
		// A hex number that may represent &#039;abyz&#039;
		$abyz = 0x6162797A;

		// Convert $abyz to a binary string containing 32 bits
		// Do the conversion the way that the system architecture wants to
		switch (pack (&#039;L&#039;, $abyz))
		{
		    // Compare the value to the same value converted in a Little-Endian fashion
		    case pack (&#039;V&#039;, $abyz):
		        $this-&gt;endian = &quot;little&quot;;
		        break;

		    // Compare the value to the same value converted in a Big-Endian fashion
		    case pack (&#039;N&#039;, $abyz):
		        $this-&gt;endian = &quot;big&quot;;
		        break;

		    default:
		        $this-&gt;endian = &quot;unknown&quot;;
		        break;
		}
	}

    /**
     * called by fopen() to the stream
     * @param   (string)    $path           file path
     * @param   (string)    $mode           stream open mode
     * @param   (int)       $options        stream options (STREAM_USE_PATH &#124;
     *                                      STREAM_REPORT_ERRORS)
     * @param   (string)    $opened_path    stream opened path
     */
    function stream_open($path, $mode, $options, &amp;$opened_path)
    {
        $url = parse_url($path);
        $this-&gt;xlsfilename = &#039;/&#039; . $url[&#039;host&#039;] . $url[&#039;path&#039;];
        $this-&gt;position = 0;
        $this-&gt;mode = $mode;

		$this-&gt;_detect();	// detect endian mode

        //@TODO: test for invalid mode and trigger error if required

        // open underlying resource
        $this-&gt;fp = @fopen($this-&gt;xlsfilename, $this-&gt;mode);
        if (is_resource($this-&gt;fp))
        {
            // empty the buffer
            $this-&gt;buffer = &quot;&quot;;

            if (preg_match(&quot;/^w&#124;x/&quot;, $this-&gt;mode))
            {
                // write an Excel stream header
                $str = pack(str_repeat($this-&gt;bin[$this-&gt;endian], 6), 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
                fwrite($this-&gt;fp, $str);
                $opened_path = $this-&gt;xlsfilename;
                $this-&gt;position = strlen($str);
            }
        }
        return is_resource($this-&gt;fp);
    }

    /**
     * read the underlying stream resource (automatically called by fread/fgets)
     * @todo    modify this to convert an excel stream to an array
     * @param   (int)       $byte_count     number of bytes to read (in 8192 byte blocks)
     */
    function stream_read($byte_count)
    {
        if (is_resource($this-&gt;fp) &amp;&amp; !feof($this-&gt;fp))
        {
            $data .= fread($this-&gt;fp, $byte_count);
            $this-&gt;position = strlen($data);
        }
        return $data;
    }

    /**
     * called automatically by an fwrite() to the stream
     * @param   (string)    $data           serialized array data string
     *                                      representing a tabular worksheet
     */
    function stream_write($data)
    {
        // buffer the data
        $this-&gt;buffer .= $data;
        $bufsize = strlen($data);
        return $bufsize;
    }

    /**
     * pseudo write function to manipulate the data
     * stream before writing it
     * modify this to suit your data array
     * @access  private
     * @param   (array)     $data           associative array representing
     *                                      a tabular worksheet
     */
    function _xls_stream_write($data)
    {
        if (is_array($data) &amp;&amp; !empty($data))
        {
            $row = 0;
            foreach (array_values($data) as $_data)
            {
                if (is_array($_data) &amp;&amp; !empty($_data))
                {
                    if ($row == 0)
                    {
                        // write the column headers
                        foreach (array_keys($_data) as $col =&gt; $val)
                        {
                            // next line intentionally commented out
                            // since we don&#039;t want a warning about the
                            // extra bytes written
                            // $size += $this-&gt;write($row, $col, $val);
                            $this-&gt;_xlsWriteCell($row, $col, $val);
                        }
                        $row++;
                    }

                    foreach (array_values($_data) as $col =&gt; $val)
                    {
                        $size += $this-&gt;_xlsWriteCell($row, $col, $val);
                    }
                    $row++;
                }
            }
        }
        return $size;
    }

    /**
     * Excel worksheet cell insertion
     * (single-worksheet supported only)
     * @access  private
     * @param   (int)       $row            worksheet row number (0...65536)
     * @param   (int)       $col            worksheet column number (0..255)
     * @param   (mixed)     $val            worksheet row number
     */
    function _xlsWriteCell($row, $col, $val)
    {
        if (is_float($val) &#124;&#124; is_int($val))
        {
            // doubles, floats, integers
            $str  = pack(str_repeat($this-&gt;bin[$this-&gt;endian], 5), 0x203, 14, $row, $col, 0x0);
            $str .= pack(&quot;d&quot;, $val);
        }
        else
        {
            // everything else is treated as a string
            $l    = strlen($val);
            $str  = pack(str_repeat($this-&gt;bin[$this-&gt;endian], 6), 0x204, 8 + $l, $row, $col, 0x0, $l);
            $str .= $val;
        }
        fwrite($this-&gt;fp, $str);
        $this-&gt;position += strlen($str);
        return strlen($str);
    }

    /**
     * called by an fclose() on the stream
     */
    function stream_close()
    {
        if (preg_match(&quot;/^w&#124;x/&quot;, $this-&gt;mode))
        {
            // flush the buffer
            $bufsize = $this-&gt;_xls_stream_write(unserialize($this-&gt;buffer));

            // ...and empty it
            $this-&gt;buffer = null;

            // write the xls EOF
            $str = pack(str_repeat($this-&gt;bin[$this-&gt;endian], 2), 0x0A, 0x00);
            $this-&gt;position += strlen($str);
            fwrite($this-&gt;fp, $str);
        }

        // ...and close the internal stream
        return fclose($this-&gt;fp);
    }

    function stream_eof()
    {
        $eof = true;
        if (is_resource($this-&gt;fp))
        {
            $eof = feof($this-&gt;fp);
        }
        return $eof;
    }
}

stream_wrapper_register(&quot;xlsfile&quot;, &quot;xlsStream&quot;)
    or die(&quot;Failed to register protocol: xlsfile&quot;);


//THE ACTUAL CODE FOR THE CONVERSION

/*The code shown here is really simple. You include the excel.php class; by including that file you register a stream wrapper for protocol xlsfile - that’s the last thing the excel.php does.

After that, it’s all you. In this working example below, I am running the php program from a command line. Argument 1 is the input file, a tab separated csv file, and argument 2 is the xls file to be written. The code expects the first row to be column headings.

First, we read the first row to set the column headers. Then we reach each line and form an associative array that will be an element of the Big Array ($xlsArray) that contains the data that will become the xls file.

Finally, we write the Big Array, and *poof* we’re done! */


//require_once(&quot;../classes/excel.php&quot;);
$inputFile=&quot;file.txt&quot;;
$xlsFile=&quot;studentdetails.xls&quot;;
 
//$inputFile=$argv[1];
///$xlsFile=$argv[2];
 
if( empty($inputFile) &#124;&#124; empty($xlsFile) ) {
    die(&quot;Usage: &quot;. basename($argv[0]) . &quot; in.csv out.xls\n&quot; );
}
 
$fh = fopen( $inputFile, &quot;r&quot; );
if( !is_resource($fh) ) {
    die(&quot;Error opening $inputFile\n&quot; );
}
 
/* Assuming that first line is column headings */
if( ($columns = fgetcsv($fh, 1024, &quot;\t&quot;)) == false ) {
    print( &quot;Error, couldn&#039;t get header row\n&quot; );
    exit(-2);
}
$numColumns = count($columns);
 
/* Now read each of the rows, and construct a
    big Array that holds the data to be Excel-ified: */
$xlsArray = array();
$xlsArray[] = $columns;
while( ($rows = fgetcsv($fh, 1024, &quot;\t&quot;)) != FALSE ) {
    $rowArray = array();
    for( $i=0; $i&lt;$numColumns;$i++ ) {
        $key = $columns[$i];
        $val = $rows[$i];
        $rowArray[&quot;$key&quot;] = $val;
    }
    $xlsArray[] = $rowArray;
    unset($rowArray);
}
fclose($fh);
 
/* Now let the excel class work its magic. excel.php
    has registered a stream wrapper for &quot;xlsfile:/&quot;
    and that&#039;s what triggers its &#039;magic&#039;: */
$xlsFile = &quot;xlsfile:/&quot;.$xlsFile;
$fOut = fopen( $xlsFile, &quot;wb&quot; );
if( !is_resource($fOut) ) {
    die( &quot;Error opening $xlsFile\n&quot; );
}
fwrite($fOut, serialize($xlsArray));
fclose($fOut);
 
exit(0);








?&gt;</description>
		<content:encoded><![CDATA[<p>In my csv file the fields are separated by commas and I tried the above code directly specifying the file names instead of passing them as the command line arguments But I am getting the error</p>
<p>Warning: fopen(xlsfile:/studentdetails.xls) [function.fopen]: failed to open stream: Invalid argument in C:\webapplications\wamp\www\csvtoexcel.php on line 291<br />
Error opening xlsfile:/studentdetails.xls </p>
<p>there student details is the file to which the data needs to get entered and I would like to know whether the file needs to be created by us or the program itself creates it?</p>
<p>Here is the code:<br />
Instead of including xls.php I have pasted the whole code directly.</p>
<p>= 4.3.2<br />
 * @author      Ignatius Teo<br />
 * @copyright   (C)2004 act28.com<br />
 * @version     0.3<br />
 * @date        20 Jan 2005<br />
 * $Id: excel.php,v 1.3 2005/01/20 09:58:58 Owner Exp $<br />
 */<br />
class xlsStream<br />
{<br />
    /* private */<br />
    var $position = 0;          // stream pointer<br />
    var $mode = &#8220;rb&#8221;;           // default stream open mode<br />
    var $xlsfilename = null;    // stream name<br />
    var $fp = null;             // internal stream pointer to physical file<br />
    var $buffer = null;         // internal write buffer<br />
    var $endian = &#8220;unknown&#8221;;	// little | unknown | big endian mode<br />
	var $bin = array(<br />
		&#8220;big&#8221; =&gt; &#8220;v&#8221;,<br />
		&#8220;little&#8221; =&gt; &#8220;s&#8221;,<br />
		&#8220;unknown&#8221; =&gt; &#8220;s&#8221;,<br />
	);</p>
<p>	/**<br />
	 * detect server endian mode<br />
	 * thanks to Charles Turner for picking this one up<br />
	 * @access	private<br />
	 * @params	void<br />
	 * @returns	void<br />
	 * @see		<a href="http://www.phpdig.net/ref/rn45re877.html" rel="nofollow">http://www.phpdig.net/ref/rn45re877.html</a><br />
	 */<br />
	function _detect()<br />
	{<br />
		// A hex number that may represent &#8216;abyz&#8217;<br />
		$abyz = 0&#215;6162797A;</p>
<p>		// Convert $abyz to a binary string containing 32 bits<br />
		// Do the conversion the way that the system architecture wants to<br />
		switch (pack (&#8216;L&#8217;, $abyz))<br />
		{<br />
		    // Compare the value to the same value converted in a Little-Endian fashion<br />
		    case pack (&#8216;V&#8217;, $abyz):<br />
		        $this-&gt;endian = &#8220;little&#8221;;<br />
		        break;</p>
<p>		    // Compare the value to the same value converted in a Big-Endian fashion<br />
		    case pack (&#8216;N&#8217;, $abyz):<br />
		        $this-&gt;endian = &#8220;big&#8221;;<br />
		        break;</p>
<p>		    default:<br />
		        $this-&gt;endian = &#8220;unknown&#8221;;<br />
		        break;<br />
		}<br />
	}</p>
<p>    /**<br />
     * called by fopen() to the stream<br />
     * @param   (string)    $path           file path<br />
     * @param   (string)    $mode           stream open mode<br />
     * @param   (int)       $options        stream options (STREAM_USE_PATH |<br />
     *                                      STREAM_REPORT_ERRORS)<br />
     * @param   (string)    $opened_path    stream opened path<br />
     */<br />
    function stream_open($path, $mode, $options, &amp;$opened_path)<br />
    {<br />
        $url = parse_url($path);<br />
        $this-&gt;xlsfilename = &#8216;/&#8217; . $url['host'] . $url['path'];<br />
        $this-&gt;position = 0;<br />
        $this-&gt;mode = $mode;</p>
<p>		$this-&gt;_detect();	// detect endian mode</p>
<p>        //@TODO: test for invalid mode and trigger error if required</p>
<p>        // open underlying resource<br />
        $this-&gt;fp = @fopen($this-&gt;xlsfilename, $this-&gt;mode);<br />
        if (is_resource($this-&gt;fp))<br />
        {<br />
            // empty the buffer<br />
            $this-&gt;buffer = &#8220;&#8221;;</p>
<p>            if (preg_match(&#8220;/^w|x/&#8221;, $this-&gt;mode))<br />
            {<br />
                // write an Excel stream header<br />
                $str = pack(str_repeat($this-&gt;bin[$this-&gt;endian], 6), 0&#215;809, 0&#215;8, 0&#215;0, 0&#215;10, 0&#215;0, 0&#215;0);<br />
                fwrite($this-&gt;fp, $str);<br />
                $opened_path = $this-&gt;xlsfilename;<br />
                $this-&gt;position = strlen($str);<br />
            }<br />
        }<br />
        return is_resource($this-&gt;fp);<br />
    }</p>
<p>    /**<br />
     * read the underlying stream resource (automatically called by fread/fgets)<br />
     * @todo    modify this to convert an excel stream to an array<br />
     * @param   (int)       $byte_count     number of bytes to read (in 8192 byte blocks)<br />
     */<br />
    function stream_read($byte_count)<br />
    {<br />
        if (is_resource($this-&gt;fp) &amp;&amp; !feof($this-&gt;fp))<br />
        {<br />
            $data .= fread($this-&gt;fp, $byte_count);<br />
            $this-&gt;position = strlen($data);<br />
        }<br />
        return $data;<br />
    }</p>
<p>    /**<br />
     * called automatically by an fwrite() to the stream<br />
     * @param   (string)    $data           serialized array data string<br />
     *                                      representing a tabular worksheet<br />
     */<br />
    function stream_write($data)<br />
    {<br />
        // buffer the data<br />
        $this-&gt;buffer .= $data;<br />
        $bufsize = strlen($data);<br />
        return $bufsize;<br />
    }</p>
<p>    /**<br />
     * pseudo write function to manipulate the data<br />
     * stream before writing it<br />
     * modify this to suit your data array<br />
     * @access  private<br />
     * @param   (array)     $data           associative array representing<br />
     *                                      a tabular worksheet<br />
     */<br />
    function _xls_stream_write($data)<br />
    {<br />
        if (is_array($data) &amp;&amp; !empty($data))<br />
        {<br />
            $row = 0;<br />
            foreach (array_values($data) as $_data)<br />
            {<br />
                if (is_array($_data) &amp;&amp; !empty($_data))<br />
                {<br />
                    if ($row == 0)<br />
                    {<br />
                        // write the column headers<br />
                        foreach (array_keys($_data) as $col =&gt; $val)<br />
                        {<br />
                            // next line intentionally commented out<br />
                            // since we don&#8217;t want a warning about the<br />
                            // extra bytes written<br />
                            // $size += $this-&gt;write($row, $col, $val);<br />
                            $this-&gt;_xlsWriteCell($row, $col, $val);<br />
                        }<br />
                        $row++;<br />
                    }</p>
<p>                    foreach (array_values($_data) as $col =&gt; $val)<br />
                    {<br />
                        $size += $this-&gt;_xlsWriteCell($row, $col, $val);<br />
                    }<br />
                    $row++;<br />
                }<br />
            }<br />
        }<br />
        return $size;<br />
    }</p>
<p>    /**<br />
     * Excel worksheet cell insertion<br />
     * (single-worksheet supported only)<br />
     * @access  private<br />
     * @param   (int)       $row            worksheet row number (0&#8230;65536)<br />
     * @param   (int)       $col            worksheet column number (0..255)<br />
     * @param   (mixed)     $val            worksheet row number<br />
     */<br />
    function _xlsWriteCell($row, $col, $val)<br />
    {<br />
        if (is_float($val) || is_int($val))<br />
        {<br />
            // doubles, floats, integers<br />
            $str  = pack(str_repeat($this-&gt;bin[$this-&gt;endian], 5), 0&#215;203, 14, $row, $col, 0&#215;0);<br />
            $str .= pack(&#8220;d&#8221;, $val);<br />
        }<br />
        else<br />
        {<br />
            // everything else is treated as a string<br />
            $l    = strlen($val);<br />
            $str  = pack(str_repeat($this-&gt;bin[$this-&gt;endian], 6), 0&#215;204, 8 + $l, $row, $col, 0&#215;0, $l);<br />
            $str .= $val;<br />
        }<br />
        fwrite($this-&gt;fp, $str);<br />
        $this-&gt;position += strlen($str);<br />
        return strlen($str);<br />
    }</p>
<p>    /**<br />
     * called by an fclose() on the stream<br />
     */<br />
    function stream_close()<br />
    {<br />
        if (preg_match(&#8220;/^w|x/&#8221;, $this-&gt;mode))<br />
        {<br />
            // flush the buffer<br />
            $bufsize = $this-&gt;_xls_stream_write(unserialize($this-&gt;buffer));</p>
<p>            // &#8230;and empty it<br />
            $this-&gt;buffer = null;</p>
<p>            // write the xls EOF<br />
            $str = pack(str_repeat($this-&gt;bin[$this-&gt;endian], 2), 0&#215;0A, 0&#215;00);<br />
            $this-&gt;position += strlen($str);<br />
            fwrite($this-&gt;fp, $str);<br />
        }</p>
<p>        // &#8230;and close the internal stream<br />
        return fclose($this-&gt;fp);<br />
    }</p>
<p>    function stream_eof()<br />
    {<br />
        $eof = true;<br />
        if (is_resource($this-&gt;fp))<br />
        {<br />
            $eof = feof($this-&gt;fp);<br />
        }<br />
        return $eof;<br />
    }<br />
}</p>
<p>stream_wrapper_register(&#8220;xlsfile&#8221;, &#8220;xlsStream&#8221;)<br />
    or die(&#8220;Failed to register protocol: xlsfile&#8221;);</p>
<p>//THE ACTUAL CODE FOR THE CONVERSION</p>
<p>/*The code shown here is really simple. You include the excel.php class; by including that file you register a stream wrapper for protocol xlsfile &#8211; that’s the last thing the excel.php does.</p>
<p>After that, it’s all you. In this working example below, I am running the php program from a command line. Argument 1 is the input file, a tab separated csv file, and argument 2 is the xls file to be written. The code expects the first row to be column headings.</p>
<p>First, we read the first row to set the column headers. Then we reach each line and form an associative array that will be an element of the Big Array ($xlsArray) that contains the data that will become the xls file.</p>
<p>Finally, we write the Big Array, and *poof* we’re done! */</p>
<p>//require_once(&#8220;../classes/excel.php&#8221;);<br />
$inputFile=&#8221;file.txt&#8221;;<br />
$xlsFile=&#8221;studentdetails.xls&#8221;;</p>
<p>//$inputFile=$argv[1];<br />
///$xlsFile=$argv[2];</p>
<p>if( empty($inputFile) || empty($xlsFile) ) {<br />
    die(&#8220;Usage: &#8220;. basename($argv[0]) . &#8221; in.csv out.xls\n&#8221; );<br />
}</p>
<p>$fh = fopen( $inputFile, &#8220;r&#8221; );<br />
if( !is_resource($fh) ) {<br />
    die(&#8220;Error opening $inputFile\n&#8221; );<br />
}</p>
<p>/* Assuming that first line is column headings */<br />
if( ($columns = fgetcsv($fh, 1024, &#8220;\t&#8221;)) == false ) {<br />
    print( &#8220;Error, couldn&#8217;t get header row\n&#8221; );<br />
    exit(-2);<br />
}<br />
$numColumns = count($columns);</p>
<p>/* Now read each of the rows, and construct a<br />
    big Array that holds the data to be Excel-ified: */<br />
$xlsArray = array();<br />
$xlsArray[] = $columns;<br />
while( ($rows = fgetcsv($fh, 1024, &#8220;\t&#8221;)) != FALSE ) {<br />
    $rowArray = array();<br />
    for( $i=0; $i&lt;$numColumns;$i++ ) {<br />
        $key = $columns[$i];<br />
        $val = $rows[$i];<br />
        $rowArray["$key"] = $val;<br />
    }<br />
    $xlsArray[] = $rowArray;<br />
    unset($rowArray);<br />
}<br />
fclose($fh);</p>
<p>/* Now let the excel class work its magic. excel.php<br />
    has registered a stream wrapper for &#8220;xlsfile:/&#8221;<br />
    and that&#8217;s what triggers its &#8216;magic&#8217;: */<br />
$xlsFile = &#8220;xlsfile:/&#8221;.$xlsFile;<br />
$fOut = fopen( $xlsFile, &#8220;wb&#8221; );<br />
if( !is_resource($fOut) ) {<br />
    die( &#8220;Error opening $xlsFile\n&#8221; );<br />
}<br />
fwrite($fOut, serialize($xlsArray));<br />
fclose($fOut);</p>
<p>exit(0);</p>
<p>?&gt;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
