CSV生成通过Class只在一列中生成结果

时间:2012-04-04 14:39:48

标签: php zend-framework export-to-csv fputcsv

      <?php


            class Zend_Controller_Action_Helper_Csv extends Zend_Controller_Action_Helper_Abstract
            {

              /**
               * Perform helper when called as $this->_helper->Csv() from an action controller
               *
               * @param  array $aryData
               * @param  string $strName
               * @param  bool $bolCols; default true; zeigt Spaltenüberschriften
               * @return void
               */
              public function direct($aryData = array(), $strName = "csv", $bolCols = true)
              {
                $this->printExcel($aryData, $strName, $bolCols);
              }

              /**
               * array via fputcsv() zu csv
               *
               * @param  array $aryData
               * @param  string $strName
               * @param  bool $bolCols
               * @return void
               */
              public function printExcel($aryData = array(), $strName = "csv", $bolCols = true)
              {

                if (!is_array($aryData) || empty($aryData))
                {
                  exit(1);
                }

                // header
                header('Content-Description: File Transfer');
                header('Content-Type: text/csv; charset=utf-8');
                header("Content-Disposition: attachment; filename=" . $strName . "-export.csv");
                header('Content-Transfer-Encoding: binary');
                header('Expires: 0');
                header('Cache-control: private, must-revalidate');
                header("Pragma: public");

                // Spaltenüberschriften
                if ($bolCols)
                {
                  $aryCols = array_keys($aryData[0]);
                  array_unshift($aryData, $aryCols);
                }

                // Ausgabepuffer für fputcsv
                ob_start();

                // output Stream für fputcsv
                $fp = fopen("php://output", "w");
                if (is_resource($fp))
                {
                  foreach ($aryData as $aryLine)
                  {
                    // ";" für Excel
                    fputcsv($fp, $aryLine, ';', '"');
                  }

                  $strContent = ob_get_clean();

                  // Excel SYLK-Bug
                  // http://support.microsoft.com/kb/323626/de
                  $strContent = preg_replace('/^ID/', 'id', $strContent);

                  $strContent = utf8_decode($strContent);
                  $intLength = mb_strlen($strContent, 'utf-8');

                  // length
                  header('Content-Length: ' . $intLength);

                  // kein fclose($fp);

                  echo $strContent;
                  exit(0);
                }
                ob_end_clean();
                exit(1);
              }
            }

我的控制器操作

    public function smscsvAction(){
            $this->_helper->viewRenderer->setNeverRender();
            $user                   = new Zend_Session_Namespace('user');
            $pid= $user->pid;
            $instance               = new Sms();
            $select                 = $instance->get_csv($pid);
           $this->_helper->Csv($select,"SmsHistory");
            }

这是get_csv定义

function get_csv($pid){
    $DB = Zend_Db_Table_Abstract::getDefaultAdapter();

    $select = $DB->select()
         ->from('sms', array('sms_id','sb','ss','sr','sc','st'))
         ->where('pd= ?', $pid)
         ->order('dcDESC');
         return $select = $DB->fetchAll($select);
    }

这很有效但是我只在一个专栏中获得了所有记录。我想要像inour phpmyadmin 一样的单独列 如何在班上或其他地方改变?

输出示例

          sms_id;sb;ss;sr;dc;st         
          6525;Kjhhgfdsaerghvddfghb;154008221;;"2012-04-04 18:20:54";Inbox          
          6526;Hi;;03224615015;"2012-04-04 18:20:54";Sent           
          6527;Shgdowsbvdowqopwqndx;3008496482;;"2012-04-04 18:20:54";Inbox         
          6528;Terka;3008496482;;"2012-04-04 18:20:54";Inbox            
          6529;"Never miss a call with Warid Missed Call Alerts! Type 'MCA ON' in an SMS & send it to 129 to activate. Charges Rs.30+tax/month for Postpaid users.";129;;"2012-04-04 18:20:54";Inbox            
          6530;"Make as many calls as you want to 5 friends & family numbers for just Rs. 500+tax/ month. To activate visit nearest Warid Business Center. Conditions Apply";Warid;;"2012-04-04 18:20:54";Inbox         
          6531;Hii;;03324427584;"2012-04-04 18:20:54";Sent          
         6532;.K;3324427584;;"2012-04-04 18:20:54";Inbox            

1 个答案:

答案 0 :(得分:0)

更改此行

fputcsv($fp, $aryLine, ';', '"');

这一个

fputcsv($fp, $aryLine, ',', '"');

,它会根据需要输出