<?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; csv</title>
	<atom:link href="http://wp.foliz.net/archives/tag/csv/feed" rel="self" type="application/rss+xml" />
	<link>http://wp.foliz.net</link>
	<description>webプログラム　php,Smarty,Zend Framework,ajax,pearネタなど～</description>
	<lastBuildDate>Sat, 21 Jan 2012 06:32:52 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ZendFremeworkでCSVファイル出力</title>
		<link>http://wp.foliz.net/archives/112</link>
		<comments>http://wp.foliz.net/archives/112#comments</comments>
		<pubDate>Thu, 05 Feb 2009 07:02:04 +0000</pubDate>
		<dc:creator>Fou</dc:creator>
				<category><![CDATA[ZendFramework]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[エクスポート]]></category>

		<guid isPermaLink="false">http://wp.foliz.net/?p=112</guid>
		<description><![CDATA[超ひさびさの更新です。 ダウンロードボタンを押すと、DBからCSVファイルにして出力するメモ。 単独phpでechoでもすればさほどでもないけど、ZendFremeworkで組んでいるとき、この出力phpファイルが本体と [...]]]></description>
			<content:encoded><![CDATA[<p>超ひさびさの更新です。</p>
<p>ダウンロードボタンを押すと、DBからCSVファイルにして出力するメモ。<br />
<span id="more-112"></span><br />
単独phpでechoでもすればさほどでもないけど、ZendFremeworkで組んでいるとき、この出力phpファイルが本体とバラバラになるのを避けたかったのでフレームワーク内で組みたかった。</p>
<p>とはいえ、Zend_Controller_Actionだけを継承したCSV出力用の単独コントローラーをつくり、アクション内で解決する。<br />
肝になるのはヘッダー情報の出力方法と、このコントローラーではレンダラー系を全て切って余計な出力が無いようにしておくことかな。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> preDispatch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> postDispatch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>まず、ファイル名を付けて、ダウンロードできるようにヘッダー情報を出力</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-disposition'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'attachment; filename=&quot;data_'</span><span style="color: #339933;">.</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'YmdHis'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.csv&quot;'</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'test/x-csv'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">sendHeaders</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>そしてSQLなんかで出力用の配列を作って、CSV用に整形するループ内でコンテンツ内容を保存していく</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">mb_convert_variables</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SJIS&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;EUC-JP&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$csv</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;">$csv</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendBody</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">outputBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://wp.foliz.net/archives/112/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

