phpexcel - 由于文件格式或者文件扩展名无效,PHPExcel文件无法打开文件

  显示原文与译文双语对照的内容
141 4

这里问题不存在,无法显示实际的excel文件。请检查下面的代码:

/** Error reporting */
error_reporting(E_ALL);
/** PHPExcel */
require_once 'PHPExcel.php';
include 'PHPExcel/Writer/Excel2007.php';
//Create new PHPExcel object
#echo date('H:i:s')." Create new PHPExcel objectn";
$objPHPExcel = new PHPExcel();
$excel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("Payroll");
if(!$result){
die("Error");
}
$col = 0; 
$row = 2; 
while($mrow = mysql_fetch_assoc($result)) { 
$col = 0; 
foreach($mrow as $key=>$value) { 
 $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value); 
 $col++; 
} 
$row++; 
} 
//Set active sheet index to the first sheet, so Excel opens this as the first sheet
 $objPHPExcel->setActiveSheetIndex(0)
 ->setCellValue('A1', 'Scholar Id')
 ->setCellValue('B1', 'Lastname')
 ->setCellValue('C1', 'Middlename')
 ->setCellValue('D1', 'Firstname')
 ->setCellValue('E1', 'Barangay')
 ->setCellValue('F1', 'Level')
 ->setCellValue('G1', 'Allowance')
 ->setCellValue('H1', 'Has claimed?');
 $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true);
 $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(12);
 $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(18);
 $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(18);
 $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(18);
 $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(18);
 $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(14);
$objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getAlignment()-> setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->setShowGridlines(true);
$objPHPExcel->getActiveSheet()->getStyle('A1:H1')->applyFromArray(
array(
 'fill' => array(
 'type' => PHPExcel_Style_Fill::FILL_SOLID,
 'color' => array('rgb' => 'FFFF00')
 )
)
); 
//Save Excel 2007 file
echo date('H:i:s')." Write to Excel2007 formatn";
#$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="payroll.xlsx"');
header('Cache-Control: max-age=0'); 
$writer->save('php://output');

请帮我解决这个问题

时间:原作者:0个回答

53 0

我现在开始工作了 !感谢这里 phpexcel下载。

我将代码修改为:

//Save Excel 2007 file
#echo date('H:i:s')." Write to Excel2007 formatn";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
//We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="payroll.xlsx"');
$objWriter->save('php://output');

我想这一行:

ob_end_clean();

解决了我的问题。

原作者:
100 0

我不知道我是否能有同样的问题,并且我用

ob_end_clean();

我把它放在

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

因此,不要更改标题并将它的保存为 xslx,问题是缓冲区 !

原作者:
...