Large excel file is not generating, getting error: ERR_INVALID_RESPONSE - php

Using PHPExcel library in Codeigniter.
Small excel file of about 20k row lines are generating perfectly, while in the case of large (like 43k row lines) file it gets:
This site can’t be reached
The webpage at
https://exmple.com/
might be temporarily down or it may have moved permanently to a new web address.
ERR_INVALID_RESPONSE
I've tried-
ini_set('max_input_vars', 19999);
set_time_limit ( 6000 );
ini_set('max_execution_time', 6000);
memory_get_usage(true);
but didn't get result.
Codeigniter Version: 3.1.11
PHP Version: 7.4
Part of code if needed (its not exact code):
public function test(){
$this->load->library('Excel');
ob_start();
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$exlHeading = array(
'font' => array(
'bold' => true,
'size' => 12,
'name' => 'Verdana')
);
// Set document properties
$objPHPExcel->getProperties()->setCreator("BigDream India")
->setLastModifiedBy("TEST")
->setTitle("REPORT")
->setSubject("ATTENDANCE REPORT")
->setDescription("Attendance Monthly Report")
->setKeywords("ATT_REPORT")
->setCategory("Excel Sheet");
for($i=0; $i<=40000; $i++){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$i, 'Test content is here.');
$objPHPExcel->setActiveSheetIndex(0)->mergeCells("A$i:I$i");
$objPHPExcel->getActiveSheet()->getStyle("A$i:I$i")->applyFromArray($exlHeading);
}
$objPHPExcel->getActiveSheet()->setTitle('Attendance Monthly Report');
$objPHPExcel->setActiveSheetIndex(0);
ob_end_clean();
$filename = 'MyOfficeGuardian-Monthly_Report.xls';
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}

Issue resolved by just updating upload_max_filesize value from 2mb to 20mb in php.ini file.

Related

Server error downloading / generating Excel file in PHP

I am generating an Excel file with an sql query, this so that it shows me all the records, the problem is that it works correctly locally, the excel is generated and downloaded, but when uploading it to the server it sends me to the file where the code is and on screen it shows me strange symbols
This is my code from my file to generate the excel and download it.
<?php
// Declaramos la librería
require __DIR__ . "/../vendor/autoload.php";
// BD
require "../conexion/dbconect.php";
use PhpOffice\PhpSpreadsheet\{Spreadsheet, IOFactory};
$query = "SELECT * FROM siacc_usuarios";
$resultado = $con->query($query);
$excel = new Spreadsheet();
$activeSheet = $excel->getActiveSheet();
//set default font
$excel->getDefaultStyle()
->getFont()
->setName('Arial')
->setSize(10);
$excel->getActiveSheet()->getStyle('A1:V1')->getFont()->setBold(true);
$activeSheet->setTitle("usuarios");
$activeSheet->getColumnDimension('A')->setwidth(10);
$activeSheet->setCellValue('A1', 'ID Usuario');
$activeSheet->getColumnDimension('B')->setwidth(10);
$activeSheet->setCellValue('B1', 'CEC');
$activeSheet->getColumnDimension('C')->setwidth(80);
$activeSheet->setCellValue('C1', 'Adscripcion');
$activeSheet->getColumnDimension('D')->setwidth(20);
$activeSheet->setCellValue('D1', 'Numero Empleado');
$activeSheet->getColumnDimension('E')->setwidth(20);
$activeSheet->setCellValue('E1', 'Apellido Paterno');
$i = 2;
while($row = $resultado->fetch_assoc()) {
$activeSheet->setCellValue('A'.$i , $row['id_usuario']);
$activeSheet->setCellValue('B'.$i , $row['cec']);
$activeSheet->setCellValue('C'.$i , $row['adscripcion']);
$activeSheet->setCellValue('D'.$i , $row['emp']);
$activeSheet->setCellValue('E'.$i , $row['ape_pat']);
$i++;
}
$filename = 'usuarios.xlsx';
// redirect output to client browser
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($excel, 'Xlsx');
ob_end_clean();
$writer->save('php://output');
This is what it shows me on screen:

How to read a lot of csv file and write them into a part a xls. file with phpexcel

I have a 5 csv file that I will convert into an excel file.
I need this, coz I have to save them into my format like image and header.
Now, I am using phpexcel.
This is my code
public function parse_csv_to_excel() {
$gaug = FCPATH . "./assets/uploads/EGT_GAUG.CSV"; /*Will become a sheet named : gauge*/
$in = FCPATH . "./assets/uploads/EGT_IN.CSV"; /*Will become a sheet named : in_record*/
$out = FCPATH . "./assets/uploads/EGT_OUT.CSV"; /*Will become a sheet named : out_record*/
$stok = FCPATH . "./assets/uploads/EGT_STOK.CSV"; /*Will become a sheet named : stock_record*/
$strg = FCPATH . "./assets/uploads/EGT_STRG.CSV"; /*Will become a sheet named : storage_record*/
if (is_file($gaug) && is_file($in) && is_file($out) && is_file($stok) && is_file($strg)):
$this->load->library('excel');
$style_merge_cell = array(
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
)
);
$this->excel->setActiveSheetIndex(0);
$this->excel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10); //default font
$inputFileType = 'CSV';
$inputFileNames = array($in, $stok, $gaug, $strg, $out);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$inputFileName = array_shift($inputFileNames);
$objPHPExcel = $objReader->load($inputFileName);
$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName, PATHINFO_BASENAME));
foreach ($inputFileNames as $sheet => $inputFileName) {
$objReader->setSheetIndex($sheet + 1);
$objReader->loadIntoExisting($inputFileName, $objPHPExcel);
$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName, PATHINFO_BASENAME));
}
$loadedSheetNames = $objPHPExcel->getSheetNames();
foreach ($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$objPHPExcel->setActiveSheetIndexByName($loadedSheetName);
$this->excel->getActiveSheet()->setTitle('Movement In');
$this->excel->createSheet();
$this->excel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$this->excel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(-1);
$this->excel->getActiveSheet()->getColumnDimension('A')->setWidth(1.29);
$logo = new PHPExcel_Worksheet_Drawing();
$logo->setPath('./assets/img/logo_tras_excel.png');
$logo->setWidth(325);
$logo->setHeight(59);
$logo->setCoordinates('B2');
$logo->setWorksheet($this->excel->getActiveSheet());
$this->excel->getActiveSheet()->setCellValue('B6', 'To');
$this->excel->getActiveSheet()->setCellValue('B7', 'ATTN');
$this->excel->getActiveSheet()->setCellValue('B8', 'Customer');
$this->excel->getActiveSheet()->setCellValue('B9', 'From');
$this->excel->getActiveSheet()->setCellValue('B10', 'Activity Date');
$this->excel->getActiveSheet()->mergeCells('B12:Q12');
$this->excel->getActiveSheet()->setCellValue('B12', 'EAGLETAINER DAILY MOVEMENT REPORT ');
$this->excel->getActiveSheet()->getStyle('B12')->applyFromArray($style_merge_cell);
$this->excel->getActiveSheet()->setCellValue('B14', 'Subject');
$this->excel->getActiveSheet()->setCellValue('D14', ': MOVE IN');
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
$this->excel->getActiveSheet()->fromArray($sheetData, NULL, 'B16');
}
$filename = 'Daily report.xls'; //save our workbook as this file name
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
//if you want to save it as .XLSX Excel 2007 format
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');
else :
echo " ONE OF FILE IS NOT FOUND";
endif;
}
Please, advise if my case :
How created a sheet that named like that I need of the top my code
I have 5 files, but the sheet was created is six.
Now, the csv is written just in first sheet with all element from array based 5 of csv files, the others is not.
Thanks,
Please Advise

exporting large files to excel using PHPExcel

I am trying to export around 40,000 rows of Mysql data in PHP(Laravel4) using PHPExcel library.
Below is my code:
($patList is an array of result columns)
set_time_limit ( 3000 );
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array( 'memoryCacheSize' => -1);
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$objPHPExcel = new PHPExcel();
$i = 1;
$patList = $result[0];
for ($r = 0; $r < count($patList); $r++) {
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("A$i", $patList[$r][0])
->setCellValue("B$i", $patList[$r][1])
->setCellValue("C$i", $patList[$r][2])
->setCellValue("D$i", $patList[$r][1])
->setCellValue("E$i", $patList[$r][2])
->setCellValue("F$i", $patList[$r][1])
->setCellValue("G$i", $patList[$r][2])
->setCellValue("H$i", $patList[$r][2])
->setCellValue("I$i", $patList[$r][1])
->setCellValue("J$i", $patList[$r][2])
->setCellValue("K$i", $patList[$r][5]);
$i++;
}
$objPHPExcel->getActiveSheet()->setTitle('Name of Sheet 1');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="result.xls"');
header('Cache-Control: max-age=0');
ob_clean();
flush();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
The above code runs fine if there are 3-4 columns in the excel and 15,000 rows. However, if I increase the no. of rows to 30,000 or the no. of columns to 10, the excel doesn't get generated.
$cacheSettings = array( 'memoryCacheSize' => -1);
isn't sensible.... I don't even know if using a value of -1 will work; but if it does, it will mean that you're storing everything in memory and nothing in php://temp
The memoryCacheSize value tells the cache stream how much data should be stored in memory before switching data out to php://temp ; so
$cacheSettings = array( 'memoryCacheSize' => '8MB');
would tell the cache stream to use 8MB of memory, and then if additional data needed to be stored to use php://temp instead

File cannot be downloaded error when exporting to xlsx using PHPExcel

Good Morning.
I need help regarding exporting data to XLSX format.
I always get this message box error message:
<IP Address> - download.phpをダウンロードできません。
Which says that the download.php cannot be downloaded.
On the other hand, when I try to change this to XLS format, I was able to download the file correctly.
here is my simple code:
index.php
<script>
function ExportDataTemplate()
{
window.location = "download.php";
}
</script>
<input type="button" class="MyButton" value="Data With Template" style="width: 150px;" onClick="ExportDataTemplate();"/>
download.php
<?php
include 'Classes/PHPExcel.php';
include 'Classes/PHPExcel/IOFactory.php';
error_reporting(E_ALL ^E_NOTICE);
ini_set('memory_limit', -1);
set_time_limit (0) ;
$objReader = PHPExcel_IOFactory::createReader("Excel2007");
$objPHPExcel = $objReader->load("Templates/tl_export_xlsx.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();
$objPHPExcel->getDefaultStyle()->getFont()->setName('MS Pゴシック')->setSize(10);
$worksheet = $objPHPExcel->setActiveSheetIndex(0);
$newFilename = "TL_Export_". date("YmdHi").".xlsx";
$styleArray = array(
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
)
)
);
$objPHPExcel->getActiveSheet()->getStyle('A1:C1')->applyFromArray($styleArray);
$worksheet->setCellValueByColumnAndRow(0, 2, "1");
$worksheet->setCellValueByColumnAndRow(1, 2, "Department");
$worksheet->setCellValueByColumnAndRow(2, 2, Team Leader");
unset($styleArray);
ob_clean();
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"".$newFilename."\"");
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
?>
Thank you in advance.
By the way, I am using Linux Cent OS,
PHPExcel v1.7.6 and
PHP 5.3.3
You have simple typo in your code. This:
$worksheet->setCellValueByColumnAndRow(2, 2, Team Leader");
Should be this:
$worksheet->setCellValueByColumnAndRow(2, 2, "Team Leader");
After that, everything seems to work properly.
EDIT:
Because you have IE 8, you should try resolving known compatiblity issues:
Try setting Content-Type to application/vnd.ms-excel
Try setting filename in Content-Disposition without quotes or with single quotes
Also, if you're using javascript (eg. window.open) to download XLSX file in IE 8, then the file may not be downloaded as XLSX is considered "not displayable" in browser.

export file php to xls uses class PHPExcel

I want to export some file php to XLS file use class PHPEXcel, I don't used this before.
notif on my browser:
"Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 1056 bytes) in C:\AppServ\www\kjjp2\Classes\PHPExcel\Cell.php on line 1124"
code:
<?php
include "config/koneksi.php";
error_reporting(E_ALL);
require_once 'Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$query = "SELECT * FROM `tabeldata`";
$hasil = mysql_query($query);
// Set properties
$objPHPExcel->getProperties()->setCreator("Erik")
->setLastModifiedBy("Erik")
->setTitle("Office 2007 XLSX ")
->setSubject("Office 2007 XLSX ")
->setDescription("Document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Jenis Report')
->setCellValue('B1', 'Pembayaran')
->setCellValue('C1', 'No')
->setCellValue('D1', 'Cabang')
//and some files
->setCellValue('AG1', 'Surveyor');
$rowNya = 3;
$no = 0;
while($row=mysql_fetch_array($hasil)){
$no = $no +1;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("A$rowNya", $row['jenReport'])
->setCellValue("B$rowNya", $row['pembayaran'])
->setCellValue("C$rowNya", $row['no'])
->setCellValue("D$rowNya", $row['cabang'])
->setCellValue("E$rowNya", $row['namaSales'])
->setCellValue("F$rowNya", $row['jenLaporan'])
//and some files
->setCellValue("AG$rowNya", $row['surveyor']);
$rowNya = $rowNya + 1;
}
// Rename sheet
$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 (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="database.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>
You are running out of RAM that can be used by PHP. You can set how much RAM that PHP will use in php.ini. You currently have this limit set to 24MB, which is pretty low. Try increasing it.
ini_set('memory_limit', '256M');
PHPExcel is known for being memory hungry. The website has a discussions with some workarounds, see http://phpexcel.codeplex.com/discussions/242712?ProjectName=phpexcel
You could also consider using another library like the old Spreadsheet_Excel_Writer lib (which has its drawbacks as well)
http://pear.php.net/package/Spreadsheet_Excel_Writer/redirected

Categories