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

时间:2014-04-02 15:04:20

标签: php excel

我尝试将PHPExcel库用于我的报告,这是我第一次使用PHPExcel,但出现错误对话框,告诉文件格式/扩展名无效,这是语法:

include '../_class/PHPExcel.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
 ->setLastModifiedBy("Rizal")
 ->setTitle("Laporan Anggota")
 ->setSubject("anggota")
 ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
 ->setKeywords("office 2007 openxml php")
 ->setCategory("Test result file");

// Create the worksheet
$objPHPExcel->setActiveSheetIndex(0);

$objPHPExcel->getActiveSheet()->setCellValue('A7', "No")
 ->setCellValue('B7', "NIS")
 ->setCellValue('C7', "NISN")
 ->setCellValue('D7', "Name")
 ->setCellValue('E7', "J.K")
 ->setCellValue('F7', "Birth")
 ->setCellValue('G7', "Address")
 ->setCellValue('H7', "Job")


// Redirect output to a client’s web browser (Excel2007)
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Test.xlsx"');
header('Cache-Control: max-age=0');

任何人都可以帮我解决它?

1 个答案:

答案 0 :(得分:0)

试试这个例子

<?php
$name = rand('0','10000').'.xlsx';
$n = rand('0','100');
error_reporting(E_ALL);
include 'PHPExcel/Classes/PHPExcel.php';
include 'PHPExcel/Classes/PHPExcel/Writer/Excel2007.php';
$o = new PHPExcel();
$o->getProperties()->setCreator('Creator');
$o->getProperties()->setLastModifiedBy('Last Modified');
$o->getProperties()->setTitle("Office 2007 XLSX Document ");
$o->getProperties()->setSubject('Subject');
$o->getProperties()->setDescription("Office 2007 XLSX, generated using PHP classes.");
$o->setActiveSheetIndex(0);
$o->getActiveSheet()->SetCellValue('A1', 'Name');
$o->getActiveSheet()->SetCellValue('B1', 'Rating');
$o->getActiveSheet()->SetCellValue('A2', 'Shawshank Redemption');
$o->getActiveSheet()->SetCellValue('B2', $n);
$o->getActiveSheet()->SetCellValue('A3', 'A Clockwork Orange');
$o->getActiveSheet()->SetCellValue('B3', $n);

$o->getActiveSheet()->setTitle('Films');

$objWriter = new PHPExcel_Writer_Excel2007($o);

$objWriter->save('excel/'.$name); // exce/ = filepath

?>

我认为你遇到的问题与$ objWriter变量有关。