<?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>超群.com的博客 &#187; excel</title>
	<atom:link href="http://www.fuchaoqun.com/tag/excel/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fuchaoqun.com</link>
	<description></description>
	<lastBuildDate>Thu, 08 Sep 2011 15:08:19 +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导出MySQL数据到Excel文件</title>
		<link>http://www.fuchaoqun.com/2010/05/php-export-mysql-excel/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=php-export-mysql-excel</link>
		<comments>http://www.fuchaoqun.com/2010/05/php-export-mysql-excel/#comments</comments>
		<pubDate>Wed, 12 May 2010 01:37:07 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[excel]]></category>
		<guid isPermaLink="false">http://www.fuchaoqun.com/?p=352</guid>
		<description><![CDATA[经常会碰到需要从数据库中导出数据到Excel文件，用一些开源的类库，比如PHPExcel，确实比较容易实现，但对大量数据的支持很不好，很容易到达PHP内存使用上限。这里的方法是利用fputcsv写CSV文件的方法，直接向浏览器输出Excel文件。 &#60;?php // 输出Excel文件头，可把user.csv换成你要的文件名 header&#40;'Content-Type: application/vnd.ms-excel'&#41;; header&#40;'Content-Disposition: attachment;filename=&#34;user.csv&#34;'&#41;; header&#40;'Cache-Control: max-age=0'&#41;; &#160; // 从数据库中获取数据，为了节省内存，不要把数据一次性读到内存，从句柄中一行一行读即可 $sql = 'select * from tbl where ……'; $stmt = $db-&#62;query&#40;$sql&#41;; &#160; // 打开PHP文件句柄，php://output 表示直接输出到浏览器 $fp = fopen&#40;'php://output', 'a'&#41;; &#160; // 输出Excel列名信息 $head = array&#40;'姓名', '性别', '年龄', 'Email', '电话', '……'&#41;; foreach &#40;$head as $i =&#62; $v&#41; &#123; // CSV的Excel支持GBK编码，一定要转换，否则乱码 $head&#91;$i&#93; = iconv&#40;'utf-8', [...]]]></description>
			<content:encoded><![CDATA[<p>经常会碰到需要从数据库中导出数据到Excel文件，用一些开源的类库，比如<a href="http://phpexcel.codeplex.com/" target="_blank">PHPExcel</a>，确实比较容易实现，但对大量数据的支持很不好，很容易到达PHP内存使用上限。这里的方法是利用fputcsv写CSV文件的方法，直接向浏览器输出Excel文件。</p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// 输出Excel文件头，可把user.csv换成你要的文件名</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: application/vnd.ms-excel'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Disposition: attachment;filename=&quot;user.csv&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cache-Control: max-age=0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 从数据库中获取数据，为了节省内存，不要把数据一次性读到内存，从句柄中一行一行读即可</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'select * from tbl where ……'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 打开PHP文件句柄，php://output 表示直接输出到浏览器</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'php://output'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 输出Excel列名信息</span>
<span style="color: #000088;">$head</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'姓名'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'性别'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'年龄'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Email'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'电话'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'……'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$head</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// CSV的Excel支持GBK编码，一定要转换，否则乱码</span>
    <span style="color: #000088;">$head</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">iconv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'utf-8'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gbk'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 将数据通过fputcsv写到文件句柄</span>
<span style="color: #990000;">fputcsv</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$head</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 计数器</span>
<span style="color: #000088;">$cnt</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// 每隔$limit行，刷新一下输出buffer，不要太大，也不要太小</span>
<span style="color: #000088;">$limit</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100000</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 逐行取出数据，不浪费内存</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch</span><span style="color: #009900;">&#40;</span>Zend_Db<span style="color: #339933;">::</span><span style="color: #004000;">FETCH_NUM</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$cnt</span> <span style="color: #339933;">++;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$limit</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$cnt</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//刷新一下输出buffer，防止由于数据过多造成问题</span>
        <span style="color: #990000;">ob_flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$cnt</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">iconv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'utf-8'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gbk'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #990000;">fputcsv</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>
<p>简单易用，非常节省内存，不依赖第三方类库。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2010/05/php-export-mysql-excel/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
