Csv.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage Zend_Controller_Action_Helper
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Controller_Action_Helper_Abstract
  24. */
  25. require_once 'Zend/Controller/Action/Helper/Abstract.php';
  26. /**
  27. * Simplify Csv context switching based on requested format
  28. *
  29. * @uses Zend_Controller_Action_Helper_Abstract
  30. * @category Zend
  31. * @package Zend_Controller
  32. * @subpackage Zend_Controller_Action_Helper
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Controller_Action_Helper_Csv extends Zend_Controller_Action_Helper_Abstract
  37. {
  38. /**
  39. * Perform helper when called as $this->_helper->Csv() from an action controller
  40. *
  41. * @param array $aryData
  42. * @param string $strName
  43. * @param bool $bolCols; default true; zeigt Spaltenüberschriften
  44. * @return void
  45. */
  46. public function direct($aryData = array(), $strName = "csv", $bolCols = true) {
  47. $this->printExcel($aryData, $strName, $bolCols);
  48. }
  49. /**
  50. * array via fputcsv() zu csv
  51. *
  52. * @param array $aryData
  53. * @param string $strName
  54. * @param bool $bolCols
  55. * @return void
  56. */
  57. public function printExcel($aryData = array(), $strName = "csv", $bolCols = true) {
  58. if (!is_array($aryData) || empty($aryData)) {
  59. exit(1);
  60. }
  61. // header
  62. header('Content-Description: File Transfer');
  63. header('Content-Type: text/csv; charset=utf-8');
  64. header("Content-Disposition: attachment; filename=export_" . $strName . "_" . date("Ymd_H-i") . ".csv");
  65. header('Content-Transfer-Encoding: binary');
  66. header('Expires: 0');
  67. header('Cache-control: private, must-revalidate');
  68. header("Pragma: public");
  69. // Spaltenüberschriften
  70. if ($bolCols) {
  71. $aryCols = array_keys($aryData[0]);
  72. array_unshift($aryData, $aryCols);
  73. }
  74. // Ausgabepuffer für fputcsv
  75. ob_start();
  76. // output Stream für fputcsv
  77. $fp = fopen("php://output", "w");
  78. if (is_resource($fp)) {
  79. foreach ($aryData as $aryLine) {
  80. // ";" für Excel
  81. fputcsv($fp, $aryLine, ';', '"');
  82. }
  83. $strContent = ob_get_clean();
  84. // Excel SYLK-Bug
  85. // http://support.microsoft.com/kb/323626/de
  86. $strContent = preg_replace('/^ID/', 'id', $strContent);
  87. $strContent = utf8_decode($strContent);
  88. $intLength = mb_strlen($strContent, 'utf-8');
  89. // length
  90. header('Content-Length: ' . $intLength);
  91. // kein fclose($fp);
  92. echo $strContent;
  93. exit(0);
  94. }
  95. ob_end_clean();
  96. exit(1);
  97. }
  98. }