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

时间:2022-10-24 00:03:46

I try to use PHPExcel library for my report, this is my first time i use PHPExcel ,but error dialog appear telling that file format / extension invalid ,this is the syntax :

我尝试使用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');

anyone can help me to solve it?

有人可以帮我解决吗?

1 个解决方案

#1


0  

Try this example

试试这个例子

<?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

?>

I think the problem you are having relates to the $objWriter variable.

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

#1


0  

Try this example

试试这个例子

<?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

?>

I think the problem you are having relates to the $objWriter variable.

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