За последние 24 часа нас посетили 17570 программистов и 1722 робота. Сейчас ищут 933 программиста ...

Экспорт в csv или xls

Тема в разделе "Прочие вопросы по PHP", создана пользователем nudist, 27 июл 2015.

  1. nudist

    nudist Новичок

    С нами с:
    22 июл 2015
    Сообщения:
    3
    Симпатии:
    0
    при экспорте в excel из php переносит не только нужные данные но и всю страницу
    Код (PHP):
    1. function download_send_headers($filename) {
    2.  // disable caching
    3.  $now = gmdate("D, d M Y H:i:s");
    4.  header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
    5.  header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
    6.  header("Last-Modified: {$now} GMT");
    7.  
    8.  // force download
    9.  header("Content-Type: application/force-download");
    10.  header("Content-Type: application/octet-stream");
    11.  header("Content-Type: application/download");
    12.  
    13.  // disposition / encoding on response body
    14.  header("Content-Disposition: attachment;filename={$filename}");
    15.  header("Content-Transfer-Encoding: binary");
    16. }
    17.  
    18. function array2csv(array &$array, $titles) {
    19.  if (count($array) == 0) {
    20.  return null;
    21.  }
    22.  $df = fopen("php://output", 'w');
    23.  fputcsv($df, $titles, ';');
    24.  foreach ($array as $row) {
    25.  fputcsv($df, $row, ';');
    26.  }
    27.  fclose($df);
    28.  return ob_get_clean();
    29. }
    30.  
    31. $titles = array("id", "Название");
    32. $data = array(
    33.  array(1, 'Имя 1'),
    34.  array(2, 'Имя 2'),
    35.  array(3, 'Имя 3'),
    36.  array(4, 'Имя 4'),
    37.  array(5, 'Имя 5'),
    38.  array(5, 'Имя 6 с кавычкой " или \' '),
    39. );
    40.  
    41. download_send_headers("data_export.csv");
    42. echo array2csv($data, $titles);
    43. die();
    44.  
    PHP, JavaScript, SQL и другой код пишите внутри тегов
    Код ( (Unknown Language)):
    1. [b]php][/b]Тут код[b][/[/b][b]code][/b][/color]