PHPExcel文件格式或扩展名无效2

时间:2015-09-17 17:49:57

标签: php excel phpexcel

我在excel文件中使用phpexcel导出我的查询;但是在我创建文件(xslx格式)之后,我无法在excel中打开我的文件。它给出了#34;文件格式或扩展名无效。如果我尝试使用OpenOffice Calc打开它正确打开。但是excelviewer没有打开。

    <?php
require 'Classes/PHPExcel.php';
require 'Classes/PHPExcel/Writer/Excel2007.php';
require "database.php";
require "C_TahsilatGetir.php";

$obj = new TahsilatGetir;
$bitis = $obj->getExcelLastDate();
$baslangic = date("d/m/Y");
$tahsilatlar = $obj->tahsilatGetirTarih($bitis["son_tarihi"], $baslangic); 

// Create new PHPExcel object
echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();

// Set properties
echo date('H:i:s') . " Set properties\n";
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");

// Add some data
echo date('H:i:s') . " Add some data\n";
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'SIRA');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'MÜŞTERİ NO');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'ÜNVAN');
$objPHPExcel->getActiveSheet()->SetCellValue('D1', 'BÖLGE');
$objPHPExcel->getActiveSheet()->SetCellValue('E1', 'GELEN TUTAR');
$objPHPExcel->getActiveSheet()->SetCellValue('F1', 'TARİH');
$objPHPExcel->getActiveSheet()->SetCellValue('G1', 'AÇIKLAMA');
$k = 2;
for ($i = 0; $i < count($tahsilatlar); $i++) {
    if ($tahsilatlar[$i]["tahsilat_turu"] == "Kredi Kartı") {
        $cell1 = "A".$k;
        $cell2 = "B".$k;
        $cell3 = "C".$k;
        $cell4 = "D".$k;
        $cell5 = "E".$k;
        $cell6 = "F".$k;
        $cell7 = "G".$k;
        $sira = $k - 1;
        $objPHPExcel->getActiveSheet()->SetCellValue($cell1, $sira);
        $objPHPExcel->getActiveSheet()->SetCellValue($cell2, $tahsilatlar[$i]["musteri_no"]);
        $objPHPExcel->getActiveSheet()->SetCellValue($cell3, $tahsilatlar[$i]["musteri_unvani"]);
        $objPHPExcel->getActiveSheet()->SetCellValue($cell4, $tahsilatlar[$i]["bolge"]);
        $objPHPExcel->getActiveSheet()->SetCellValue($cell5, $tahsilatlar[$i]["tutar"]);
        $objPHPExcel->getActiveSheet()->SetCellValue($cell6, $tahsilatlar[$i]["tahsilat_tarihi"]);
        $objPHPExcel->getActiveSheet()->SetCellValue($cell7, $tahsilatlar[$i]["banka"]);
        $k++;
    }
}

// Rename sheet
echo date('H:i:s') . " Rename sheet\n";
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="KREDIKARTI.xlsx"');
$objWriter->save('php://output');

// Echo done
echo date('H:i:s') . " Done writing file.\r\n";
?>

1 个答案:

答案 0 :(得分:3)

始终确保将输出直接发送到浏览器(php://output)时生成的PHPExcel存储流是 输出。这意味着任何回声或打印语句如

echo date('H:i:s') . " Create new PHPExcel object\n";

echo date('H:i:s') . " Done writing file.\r\n";

不得在您的脚本

中使用