What is the expected behavior?
I tried to add the formula =SUMIF, but this is not working. An error occure and the formula is deleted when I open the excel file
What is the current behavior?
When the code try to calculate the formula, this is bugging
What are the steps to reproduce?
sumif.xlsx : Check this page : https://github.com/PHPOffice/PhpSpreadsheet/issues/892 to download the xlsx
<?php require 'vendor/autoload.php';
set_time_limit(-1);
//error_reporting(0);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$objPHPExcel = $reader->load("sumif.xlsx");
$objPHPExcel->setActiveSheetIndex(1);
for($count = 1 ; $count <= 10; $count++)
{
$objPHPExcel->getActiveSheet()->setCellValue('D'.$count, '=SUMIF(Résumé!D$2:D$22;B'.$count.';Résumé!F$2:F$22)');
}
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="paiement_'.date('Y-m-d').'.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
/**/
$objWriter = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($objPHPExcel);
$objWriter->setPreCalculateFormulas(false);
$objWriter->save('php://output');
/**/
?>
Which versions of PhpSpreadsheet and PHP are affected?
PHP 7.0 and PhpSpreadsheet 1.6.0
On the formula string, try using , instead of ; like this:
'=SUMIF(Résumé!D$2:D$22,B'.$count.',Résumé!F$2:F$22)'
Related
Im currently writing a data export using PHPExcel and keep getting an empty file.
I've made a test script that should give me an excel file with 3 test cells filled but it also gives me an empty excel file.
When i print_r the $objphpexcel it still shows that the cells are filled.
But when i print_r the writer, it only has the header data available and no cell content..
<?php
include_once 'models/PHPExcel.php';
include_once 'models/PHPExcel/IOFactory.php';
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator('Company')
->setLastModifiedBy('User')
->setTitle('Sample_title');
// Add header (first row) data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'EXCEL EXPORT')
->setCellValue('B1', 'TEST')
->setCellValue('C1', 'CEL');
$next_excelrow=2;
//ITERATION TROUGH DATA HERE
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('A');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
//echo'<pre>'.print_r($objPHPExcel,true).'</pre>';exit; <-- this print_r still has the data
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="sample_export.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
//header('Content-Type: text/html; charset=UTF-8');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
As you can see I've already narrowed it down to a simple export and still an empty corrupt file is what i get served as a download..
Can anyone help?
Thanks!
try to change $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
to $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="file.xlsx"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('file.xlsx');
try to save it to file like example, not to php://output. Works for me.
Since PHPExcel is deprecated I'm moving to PHPSpreadsheet
I use PHPExcel to export my MySQL data ta xls file, but when I run it echo somethings instead download file to my local. I use firefox on centos 7.
My error like this:
��ࡱ�;�� ?����#ABCDEFGHIJKL������������������������������������������������������������������������������������������������������������������������������������������������������������
here is my code:
foreach($this->items as $r => $dataRow) {
$row = $baseRow + $r;
$objPHPExcel->getActiveSheet()->insertNewRowBefore($row,1);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $r+1)
->setCellValue('B'.$row, $dataRow['a'])
->setCellValue('C'.$row, $dataRow['b_display'])
->setCellValue('D'.$row, $dataRow['c_count'])
->setCellValue('E'.$row, $dataRow['d'])
->setCellValue('F'.$row, $dataRow['e'])
->setCellValue('G'.$row, '=C'.$row.'*D'.$row);
}
$filename=mt_rand(1,100000).'.xls'; //just some random filename
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; filename=DoanhNghiep.xls");
header("Pragma: no-cache");
header("Expires: 0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //downloadable file is in Excel 2003 format (.xls)
$objWriter->save('php://output'); //send it to user, of course you can save it to disk also!
exit;
Anybody can help me ? tks for reading !
I assume that your code of foreach is correct you can try this modified header code
$filename=mt_rand(1,100000).'.xls'; //just some random filename
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; filename='".$filename."'");
header("Pragma: public");
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //downloadable file is in Excel 2003 format (.xls)
$objWriter->save('php://output'); //send it to user, of course you can save it to disk also!
exit;
Or You can also try Below code that is working for me in my project
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Booking Report.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
[This guy seems to have the same problem as me but it's Java and I don't understand anything :/ excel can not open the downloaded file through jsp ]
Hello everyone,
First of all, I want to say that English isn't my mother language so you might see some mistakes but I hope you will understand me.
So I'm a student and I have a 'student job', creating a website for a micro-bakery! They need to have a file with all their clients and I decided to use PhpExcel to generate it!
I did everything properly but it still doesn't work! I looked for help on google of course but nobody has the same error message as me. Here is what I get.
Error message I get when I try to open the file : In english : "Impossible to open the file "....xlsx" because it's format or extension is not valid. Please verify that the file is not broken and that it's extension match the file format"
The source code is here : `
if($action=="client-excel"){
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
/** Include PHPExcel */
require_once '../libs/phpExcel/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("BakeryName")
->setLastModifiedBy("Clients")
->setTitle("Liste Clients ")
->setSubject("Liste Clients ")
->setDescription("Liste Clients ")
->setKeywords("Liste Clients ")
->setCategory("Liste Clients");
$toSupp=array();
$objPHPExcel->createSheet();
$array=array();
$array['header'][]="#client";
$array['header'][]="Nom";
$array['header'][]="Mail";
$array['header'][]="Nombre de pain(s) acheté(s)";
$array['data']=array();
$list = Client::getAllClients($order="id_client",$letter="",$valid=false,$start=0,$limit=100000); foreach($list as $client){
$client_array=array(
$client->getId(),
$client->getName(),
$client->getEmail(),
$client->getNbPainsCmd(),
);
$array['data'][]=$client_array;
}
if(sizeof($array['data'])==0){
$toSupp = $z;
}
$objPHPExcel->setActiveSheetIndex(0);
$j=0;
foreach($array['header'] as $h){
$objRichText = new PHPExcel_RichText();
$objRichText->createTextRun($h)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ($j,1, $objRichText);
$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($j)->setAutoSize(true);
$j++;
}
for($i=0;$i<sizeof($array['data']);$i++){
for($j=0;$j<sizeof($array['data'][$i]);$j++){
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ($j,$i+2, $array['data'][$i][$j]);
}
}
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle("Liste clients");
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="filename.xlsx"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}`
Thank you very much for all your potential answers!
I am using PHPExcel to get Oracle SQL query results to an .xlsx file. I wrote my PHP code in node--18769.tpl.php and node--18769.tpl.xlsx file is downloading to webroot (/root/themes/bartik/templates) folder with result.
My requirement:
Can I rename node--18769.tpl.xlsx to report.xlsx?
Is it possible to prepend UNIX TIMESTAMP to file name? (like 1442223364_report.xlsx)
How can I download report.xlsx to my local system once after the file is generated?
This is my code:
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
// Add some data
$query = "SELECT DISTINCT TITLE, PID, TYPE, SUM(DAYCOUNT) AS tot, ROUND(SUM(DAYCOUNT)/( SELECT SUM(DAYCOUNT) FROM REPORT_LIST_VIEW), 4) AS per FROM REPORT_LIST_VIEW WHERE DAYCOUNT > '0' GROUP BY TITLE, PID, TYPE ORDER BY tot DESC";
//print $query; exit;
$res = db_query($query);
$rowNumber = 1;
while ( $dataFetched = $res->fetchAssoc() ) {
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$rowNumber, $dataFetched['title'])
->setCellValue('B'.$rowNumber, $dataFetched['tot'])
->setCellValue('C'.$rowNumber, $dataFetched['per']);
$rowNumber++;
}
// Miscellaneous glyphs, UTF-8
$objPHPExcel->getActiveSheet()->getRowDimension(8)->setRowHeight(-1);
$objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setWrapText(true);
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Page & Files Reports ');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A8',"Hello\nWorld");
$objPHPExcel->getActiveSheet()->getRowDimension(8)->setRowHeight(-1);
$objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setWrapText(true);
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
Code updated:
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
This line
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
tells PHPExcel what filename to use when you save the file.... simply change it to
$objWriter->save("WHATEVER_YOU_WANT_TO_CALL_THE_FILE.xlsx);
If you want to download the file to local, then save to php://output and send the appropriate headers to the browser, exactly as described in the documentation and shown in the examples provided with PHPExcel
// write the file
$objWriter->save('Excel_report/'filename');
//Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Contact.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
i have written code that is working in Mozilla firefox but not working in internet explorer
i cannot solve it. in internet explorer its complete page loding with out popup download .
i need to generete excel file with IE support also.please help me.
here is my code.
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(18);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Agent Code')
->setCellValue('B1', 'Month');
$i=2;
while($row1=mysql_fetch_array($rs))
{
$month = $row1['smonth']+1;
$month_name = date( 'F', mktime(0, 0, 0, $month) );
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i, $row1['scode'])
->setCellValue('B'.$i, $month_name)
->setCellValue('C'.$i, $row1['syear']);
$i++;
}
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
This is complete solution
use
header('Pragma: ');
Your issue is relate with headers were missing.
Try to replace your header code with this following code:
setcookie("fileLoading","true");
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$viNameFile.'.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');