<?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>天生我才必有用 &#187; Excel</title>
	<atom:link href="http://ray.imiddle.net/tag/excel/feed/" rel="self" type="application/rss+xml" />
	<link>http://ray.imiddle.net</link>
	<description>狂人的成长史</description>
	<lastBuildDate>Thu, 17 Nov 2011 01:01:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>使用PHP读取Excel文件</title>
		<link>http://ray.imiddle.net/2009/11/how-to-read-the-excel-with-php/</link>
		<comments>http://ray.imiddle.net/2009/11/how-to-read-the-excel-with-php/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 06:28:01 +0000</pubDate>
		<dc:creator>Ray</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[OLE]]></category>

		<guid isPermaLink="false">http://ray.imiddle.net/?p=601</guid>
		<description><![CDATA[可能这应该算得上一个很古老的问题，解决的也有多种多样，不过Internet上比较多的是Excel的保存的范例，所以写一篇如何读取Excel，可能对大家有所帮助。 使用COM接口调用，把Excel应用程序作为一个Server，PHP间接使用Excel来读取文件。 使用PHPExcel的程序包。它的优点不单单读取资料而且可以保存资料为Excel文件，同时还支持Excel 2007的格式。方法就是把Excel看作一个普通的Excel文件读取。 还有一种是PHP Excel Reader，它把Excel文件看作一个二进制文件读取，不过不支持新版本Excel文件。 方法2可能看起来不错，不过当你在读取资料大比较大的文件时，会有一个致命的缺点：内存溢出。因为它必须把整个Excel读入内存。有点像XML的DOM接口，不清楚是否有类型XML的SAX的方法。 方法3有类似方法2内存溢出的问题，同时对新版的支持较差。 方法1应该是属于非常传统的方式，只有支持COM程序接口就可。同时只有你能用当前安装的Excel打开此文件，你就可以通过COM接口读取文件。缺点就是你必须在你的电脑上安装Excel。不过对于大文件现在只能使用这个方法。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 $doc = &#34;test.xls&#34; ; $excel_app = new COM&#40;&#34;Excel.Application&#34;, NULL, CP_UTF8&#41; or die &#40;&#34;Please install the Excel. \n&#34;&#41;; &#160; #$excel_app-&#62;Visible = 1; $workbook = [...]]]></description>
			<content:encoded><![CDATA[<p>可能这应该算得上一个很古老的问题，解决的也有多种多样，不过Internet上比较多的是Excel的保存的范例，所以写一篇如何读取Excel，可能对大家有所帮助。</p>
<ol>
<li>使用COM接口调用，把Excel应用程序作为一个Server，PHP间接使用Excel来读取文件。</li>
<li>使用<a href="http://www.phpexcel.net/" target="_blank">PHPExcel</a>的程序包。它的优点不单单读取资料而且可以保存资料为Excel文件，同时还支持Excel 2007的格式。方法就是把Excel看作一个普通的Excel文件读取。</li>
<li>还有一种是<a href="http://sourceforge.net/projects/phpexcelreader">PHP Excel Reader</a>，它把Excel文件看作一个二进制文件读取，不过不支持新版本Excel文件。</li>
</ol>
<p>方法2可能看起来不错，不过当你在读取资料大比较大的文件时，会有一个致命的缺点：内存溢出。因为它必须把整个Excel读入内存。有点像XML的DOM接口，不清楚是否有类型XML的SAX的方法。</p>
<p>方法3有类似方法2内存溢出的问题，同时对新版的支持较差。</p>
<p>方法1应该是属于非常传统的方式，只有支持COM程序接口就可。同时只有你能用当前安装的Excel打开此文件，你就可以通过COM接口读取文件。缺点就是你必须在你的电脑上安装Excel。不过对于大文件现在只能使用这个方法。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$doc</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test.xls&quot;</span> <span style="color: #339933;">;</span>
<span style="color: #000088;">$excel_app</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> COM<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Excel.Application&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> CP_UTF8<span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Please install the Excel. <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#$excel_app-&gt;Visible = 1;
</span>
<span style="color: #000088;">$workbook</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$excel_app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Workbooks</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Open</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$doc</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Can't open &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$doc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$worksheet</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$workbook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">WorkSheets</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$worksheet</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">activate</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$min_row</span>    <span style="color: #339933;">=</span> <span style="color: #000088;">$worksheet</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">UsedRange</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Row</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$min_col</span>    <span style="color: #339933;">=</span> <span style="color: #000088;">$worksheet</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">UsedRange</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Column</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$max_row</span>    <span style="color: #339933;">=</span> <span style="color: #000088;">$worksheet</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">UsedRange</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Rows</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Count</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$max_col</span>    <span style="color: #339933;">=</span> <span style="color: #000088;">$worksheet</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">UsedRange</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Columns</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Count</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$min_row</span> <span style="color: #339933;">;</span> <span style="color: #000088;">$row</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$max_row</span><span style="color: #339933;">;</span> <span style="color: #000088;">$row</span> <span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$column</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$min_col</span> <span style="color: #339933;">;</span> <span style="color: #000088;">$column</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$max_col</span><span style="color: #339933;">;</span> <span style="color: #000088;">$column</span> <span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$worksheet</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Cells</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #339933;">,</span> <span style="color: #000088;">$column</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #339933;">;</span>
       <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$excel_app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Workbooks</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$excel_app</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Quit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://ray.imiddle.net/2009/11/how-to-read-the-excel-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

