-1

Trying page.İt's working.And downloading excel(xlsx) file.But some code,calling another php file in tableCreate.php=>class TableCreate => function Excele_Aktar() .İt's not working.And return nonsense ajax return.I'm sharing ajax return,bottom.

exportExcel_Php.php

<?php
session_start();
$rapor_adi="deneme_rapor";
header( 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' );
header( 'Content-Disposition: attachment;filename="'.$rapor_adi.'.xlsx"' );
header( 'Cache-Control: max-age=0' );

$excel_yol = $_SESSION[ 'kok_dizin' ] . "Excel_Islemler/PHPExcel/Classes/PHPExcel.php";
require_once( $excel_yol  );
$phpexcel = new PHPExcel();
$phpexcel->setActiveSheetIndex( 0 );
$sheet = $phpexcel->getActiveSheet();
$phpexcel->getActiveSheet()->setTitle( 'Sayfa1' );
$phpexcel->getActiveSheet()->setCellValue( "A1", "Tarih" );
$phpexcel->getActiveSheet()->setCellValue( "B1", "Saat" );
$phpexcel->getActiveSheet()->setCellValue( "C1", "Order Adı" );
$phpexcel->getActiveSheet()->setCellValue( "D1", "Band No" );
$topac5 = 2;

$phpexcel->getActiveSheet()->setCellValue( "A$topac5", "Tarih Veri" );
$phpexcel->getActiveSheet()->setCellValue( "B$topac5", "Saat Veri" );
$phpexcel->getActiveSheet()->setCellValue( "C$topac5", "Order Adı Veri" );
$phpexcel->getActiveSheet()->setCellValue( "D$topac5", "Band No Veri" );

$style_header = array(
  'font' => array(
    'bold' => true,
    'size' => "12",
  ),
  'alignment' => array(
    'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  )
);

$phpexcel->getActiveSheet()->getStyle( "A1:D1" )->applyFromArray( $style_header );
$phpexcel->getActiveSheet()->getColumnDimension( 'A' )->setWidth( "13" );
$phpexcel->getActiveSheet()->getColumnDimension( 'B' )->setWidth( "7" );
$phpexcel->getActiveSheet()->getColumnDimension( 'C' )->setWidth( "12" );
$phpexcel->getActiveSheet()->getColumnDimension( 'D' )->setWidth( "10" );


$writer = PHPExcel_IOFactory::createWriter( $phpexcel, 'Excel2007' );
$writer->setIncludeCharts( TRUE );
$writer->save( 'php://output' );
return "Rapor Excele Aktarıldı";

?>

I calling tableCreate =>Excele_Aktar() method from here another php page.

tableCreate.php

<?php
session_start();
class TableCreate {
  function Excele_Aktar( $rapor_adi, $rapor_tablo_columns, $rapor_tablo_data, $periyot ) {
    header( 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' );
    header( 'Content-Disposition: attachment;filename="Detayli_Operasyon_Raporu.xlsx"' );
    header( 'Cache-Control: max-age=0' );
    $excel_classes_yol = $_SESSION[ 'kok_dizin' ] ."Excel_Islemler/PHPExcel/Classes/PHPExcel.php";
    require( $excel_classes_yol );


    $phpexcel = new PHPExcel();
    $phpexcel->setActiveSheetIndex( 0 );
    $sheet = $phpexcel->getActiveSheet();
    $phpexcel->getActiveSheet()->setTitle( 'Sayfa1' );
    $phpexcel->getActiveSheet()->setCellValue( "A1", "Tarih" );
    $phpexcel->getActiveSheet()->setCellValue( "B1", "Saat" );
    $phpexcel->getActiveSheet()->setCellValue( "C1", "Order Adı" );
    $phpexcel->getActiveSheet()->setCellValue( "D1", "Band No" );
    $topac5 = 2;

    $phpexcel->getActiveSheet()->setCellValue( "A$topac5", "Tarih Veri" );
    $phpexcel->getActiveSheet()->setCellValue( "B$topac5", "Saat Veri" );
    $phpexcel->getActiveSheet()->setCellValue( "C$topac5", "Order Adı Veri" );
    $phpexcel->getActiveSheet()->setCellValue( "D$topac5", "Band No Veri" );

    $style_header = array(
      'font' => array(
        'bold' => true,
        'size' => "12",
      ),
      'alignment' => array(
        'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
      )
    );

    $phpexcel->getActiveSheet()->getStyle( "A1:D1" )->applyFromArray( $style_header );
    $phpexcel->getActiveSheet()->getColumnDimension( 'A' )->setWidth( "13" );
    $phpexcel->getActiveSheet()->getColumnDimension( 'B' )->setWidth( "7" );
    $phpexcel->getActiveSheet()->getColumnDimension( 'C' )->setWidth( "12" );
    $phpexcel->getActiveSheet()->getColumnDimension( 'D' )->setWidth( "10" );


    $writer = PHPExcel_IOFactory::createWriter( $phpexcel, 'Excel2007' );
    $writer->setIncludeCharts( TRUE );
    $writer->save( 'php://output' );
    return "Rapor Excele Aktarıldı";
  }
}
?>

Ajax Page

function PersonelVerimRaporExceleAktar()
  {
    Preloader_Baslat();
    var raporCekmeYontem="Excel";
    var personel_adi=$("#personel" ).val();
    $.ajax({
      type:"POST",
      url:"./Pages/Personel_Islemler/Personel_Islemleri.php",
      data:{baslangic_tarih:baslangic_tarih,bitis_tarih:bitis_tarih,personel_adi:personel_adi,raporCekmeYontem:raporCekmeYontem,paramPersonel_Verim_Raporu_Islem:"Personel_Verim_Raporu_Getir"},

    })
    .done(function(gelen_cevap){

      Preloader_Bitir();
      document.write(gelen_cevap);



    })
    .fail(function(jqXHR,textStatus){
      alert("Bir hata oluştu:"+textStatus);
    });


  }

Return nonsense ajax result

I'm edit.Ajax code.But not download.I'm sharing console log ss.

enter image description here

function PersonelVerimRaporExceleAktar()
  {
    //Preloader_Baslat();
    var raporCekmeYontem="Excel";
    var personel_adi=$("#personel" ).val();
    $.ajax({
      type:"POST",
      url:"./Pages/Personel_Islemler/Personel_Islemleri.php",
      data:{baslangic_tarih:baslangic_tarih,bitis_tarih:bitis_tarih,personel_adi:personel_adi,raporCekmeYontem:raporCekmeYontem,paramPersonel_Verim_Raporu_Islem:"Personel_Verim_Raporu_Getir"},

    })
    .done(function(gelen_cevap){
     var a = document.createElement('a');
            var url = window.URL.createObjectURL(gelen_cevap);
            a.href = url;
            a.download = 'myfile.xlsx';
            document.body.append(a);
            a.click();
            a.remove();
            window.URL.revokeObjectURL(url);
      //Preloader_Bitir();
    })
    .fail(function(jqXHR,textStatus){
      alert("Bir hata oluştu:"+textStatus);
    });


  }
  • 2
    That's not nonsense, that's the binary data of the excel file. You've just written into your web page instead of downloading it – ADyson Jun 10 '21 at 07:30
  • 1
    Nice to see you man.You helped me before.So how can i download this file – Emrah Çalışkan Jun 10 '21 at 07:31
  • 1
    Start with this guide: https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax – ADyson Jun 10 '21 at 07:36
  • Thank you bro, I'm checking it out right now – Emrah Çalışkan Jun 10 '21 at 07:37
  • I was edited ajax code.But not downloading.I was sharing edited ajax code and console log.Can you check it out dude – Emrah Çalışkan Jun 10 '21 at 07:47
  • I'm fixed problem.Tahnk you adyson and Lelio Faieta.But I have one last question.I'm setting filename in php page.But ajax setting withh this a.download = 'myfile.xlsx'; How to fixed it? – Emrah Çalışkan Jun 10 '21 at 08:03
  • Does this answer your question? [Failed to execute 'createObjectURL' on 'URL':](https://stackoverflow.com/questions/27120757/failed-to-execute-createobjecturl-on-url) – Lelio Faieta Jun 10 '21 at 08:03
  • The filename set by PHP goes into a response header of the Ajax call so JavaScript should be able to retrieve the header information and re-use the filename from it. I'm sure Google will help you to find a guide about how to read the headers from an Ajax response. – ADyson Jun 10 '21 at 08:12
  • Thank you bro.I finded and edited my questions answer.See you later :) – Emrah Çalışkan Jun 10 '21 at 08:16

1 Answers1

1

I'm fixed problem.adyson one thank you.Edited Last:

    function PersonelVerimRaporExceleAktar()
  {
      var filename;
    Preloader_Baslat();
    var raporCekmeYontem="Excel";
    var personel_adi=$("#personel" ).val();
    $.ajax({
      type:"POST",
         xhrFields: {
            responseType: 'blob'
        },
      url:"./Pages/Personel_Islemler/Personel_Islemleri.php",
      data:{baslangic_tarih:baslangic_tarih,bitis_tarih:bitis_tarih,personel_adi:personel_adi,raporCekmeYontem:raporCekmeYontem,paramPersonel_Verim_Raporu_Islem:"Personel_Verim_Raporu_Getir"},

    })
    .done(function(gelen_cevap,status, xhr){
         var filename = "";
        var disposition = xhr.getResponseHeader('Content-Disposition');
        if (disposition && disposition.indexOf('attachment') !== -1) {
            var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
            var matches = filenameRegex.exec(disposition);
            if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
        }
     var a = document.createElement('a');
           var a = document.createElement('a');
            var url = window.URL.createObjectURL(gelen_cevap);
            a.href = url;
            a.download = filename;
            document.body.append(a);
            a.click();
            a.remove();
            window.URL.revokeObjectURL(url);
      Preloader_Bitir();
    })
    .fail(function(jqXHR,textStatus){
      alert("Bir hata oluştu:"+textStatus);
    });


  }