Read excel file using PHPExcel and codeigniter resulted in blank page - php

I'm trying to read a .xlsx file using PHPExcel and the echo the results in my view, but when I run the file, the browser shows nothing but a blank page. Here is my controller:
class Input_data extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper("url");
$this->load->library('session');
$this->load->library('phpexcel');
$this->load->library('PHPexcel/iofactory');
}
function index()
{
$names=array();
$no=0;
$inputFileType = 'Excel2007';
$objReader = IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader ->load(FCPATH."/upload/Lap_Final_RLPS.xlsx");
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$maxRow = $objWorksheet->getHighestRow();
for ($i=14; $i<=$maxRow; $i++)
{
$names[$no] = $objWorksheet->getCell(6, $i)->getValue();
$no++;
}
$data['names'] = $names;
$data['no'] = $no;
$this->load->view('/teps/input_data_view',$data);
}}
It seems that I've got it wrong in this line:
$objPHPExcel = $objReader ->load(FCPATH."/upload/Lap_Final_RLPS.xlsx");
because every time I deleted that line and the lines below it (until the lines necessary to load my view), I got my view showed allright, unless of course, I can't read my excel file.
Can anyone show me what's wrong? Thank you.

Use this article to read data from xlsx file -
http://websixer.wordpress.com/2014/07/02/import-data-from-excel-file-in-php-support-both-xls-or-xlsx-format/

Enable php zip extension from Apache setting, in the php.ini

convert your Lap_Final_RLPS.xlsx file to Lap_Final_RLPS.xls and try, it will work for sure, it is working for me.
OR maybe save as Lap_Final_RLPS.xls

Related

Attempted to load class "Mpdf" from namespace "Mpdf"

I'm using PHPSpreadsheet to create Excel.
I want t generate the Excel file, then convert the Excel file in a PDF one.
So I've done the following :
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
use PhpOffice\PhpSpreadsheet\Reader\Exception;
class DevisGenerator
{
public function runDevis()
{
$spreadsheet = $this->loadexcelTemplate();
$uuid = $this->uniqidReal();
$filename = $this->writeName($spreadsheet, $uuid);
$this->convertPdf($spreadsheet, $filename);
}
public function writeName($spreadsheet, $uuid)
{
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->getCell('B2')->setValue('Toto');
try {
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$filename = $uuid;
$writer->save($filename.'.xlsx');
}catch (Exception $e)
{
//TODO gestion erreur
}
return $filename;
}
public function convertPdf($spreadsheet, $filename)
{
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
$writer->save($filename.'.pdf');
}
But whan I run the code the following error appear :
Attempted to load class "Mpdf" from namespace "Mpdf".
Did you forget a "use" statement for "PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf"?
I did not understand this error, I have correctly insert the use statement in my code.
Any idea ?
I've already got a similar issue with Mpdf.
PHPSpreadsheet supports multiple Librairies to generate PDF.
I'm nut using Mpdf but Tcpdf.
I'm also not sure but you need to install them manually.
composer require tecnickcom/tcpdf
Then in your code :
$writer = new Tcpdf($spreadsheet);
And don't forget the use statement ;)
Hope this help !
After use statement before class, you should use only:
public function convertPdf($spreadsheet, $filename)
{
$writer = new Mpdf($spreadsheet);
$writer->save($filename.'.pdf');
}
Since you use the fully qualified namespace when creating the instance, the use statement is not taken into account (thus the error message).
It seems like you have added a supplementary slash at the beginning of the namespace when creating your Mpdf instance, removing it will solve your issue.
$writer = new PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
But since you have added a use statement, you do not need to use the fully qualified namespace again, you can do
$writer = new Mpdf($spreadsheet);

Export With Box Spout

I am trying to implement Box/Spout into a project of mine and I want to export some data.
The thing is that when I tried to make a test file to get downloaded it doesn't work, it's just a simple white page and the .xlsx download is not triggered.
Below is my code:
<?php
require_once 'vendor/box/spout/src/Spout/Autoloader/autoload.php';
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Common\Entity\Row;
function xlsx(){
$writer = WriterEntityFactory::createXLSXWriter();
// $writer = WriterEntityFactory::createODSWriter();
// $writer = WriterEntityFactory::createCSVWriter();
$writer->setShouldUseInlineStrings(true); // default (and recommended) value
$writer->setTempFolder($customTempFolderPath);
$fileName = 'test.xlsx';
// $writer->openToFile($filePath); // write data to a file or to a PHP stream
$writer->openToBrowser($fileName); // stream data directly to the browser
$cells = [
WriterEntityFactory::createCell('Carl'),
WriterEntityFactory::createCell('is'),
WriterEntityFactory::createCell('great!'),
];
/** add a row at a time */
$singleRow = WriterEntityFactory::createRow($cells);
$writer->addRow($singleRow);
/** add multiple rows at a time */
$multipleRows = [
WriterEntityFactory::createRow($cells),
WriterEntityFactory::createRow($cells),
];
$writer->addRows($multipleRows);
/** Shortcut: add a row from an array of values */
$values = ['Carl', 'is', 'great!'];
$rowFromValues = WriterEntityFactory::createRowFromArray($values);
$writer->addRow($rowFromValues);
$writer->close();
}
This is a sample taken from the official documentation: https://opensource.box.com/spout/
Is there something I missed? What I am doing wrong?
It looks good to me. You just need to call the xlsx() function from your index.php to actually trigger the download.
To test if it really works, just move the contents of the xlsx() function directly inside index.php; that could help debug your issue

PHPspreadsheet show blank in browser tab

I have used https://github.com/PHPOffice/phpspreadsheet this package to download
csv files in codeigniter
When I try to download it shows blank in browser tab and not showing download option
there are no errors
Tried with below code
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class Reports extends Admin_Controller
{
public function download()
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
}
}
Can any anyone help on that please??.Thanks
Hope this will help you :
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class Reports extends Admin_Controller
{
public function index()
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$spreadsheet->setActiveSheetIndex(0);
$filename = 'hello world';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
}
}
for csv just change the extesion .xlsx to .csv (case-sensitive)
The download() function you have there is only for writing a new file. Not anything to do with downloading a file. However I managed to output a file using this code here. I outputted a csv file so I could check it more easily on my server but the premise is still the same with xlsx files.
public function writeFile() {
$this->load->library('Spreadsheet');
$output_file = getcwd() . "/test/hello_world.csv";
$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->setCellValueByColumnAndRow("1", "1", "Hello World!");
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Csv');
$writer->save($output_file);
}
I think part of your problem was not loading the library correctly using the CodeIgniter nomenclature. If you are trying to upload files I would reccomend using the upload library which comes with CodeIgniter. The documentation can be found here: https://www.codeigniter.com/userguide3/libraries/file_uploading.html
I would then use phpspreadsheet to edit it to your hearts content once it has been uploaded. Hope this helps.

Codeigniter PHPExcel Class 'PHPExcel_IOFactory' not found

I have followed this tutorial here
https://arjunphp.com/how-to-use-phpexcel-with-codeigniter/
and here
http://www.ahowto.net/php/easily-integrateload-phpexcel-into-codeigniter-framework/
However, I still get the PHPExcel_IOFactory not found error. I haven't found much help in this area. Is there an alternative excel plugin or is there a way to solve this?
This is my controllers construct
public function __construct() {
parent::__construct();
$this->load->library('Excel');
}
This is my controllers upload function
$objReader= PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel= PHPExcel_IOFactory::load($document_folder."/".$file_name.".".$file_extension);
// $this->response($objPHPExcel);
$objPHPExcel=$objReader->load($document_folder."/".$file_name.".".$file_extension);
$totalrows=$objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$objWorksheet=$objPHPExcel->setActiveSheetIndex(0);
for($i=2;$i<=$totalrows;$i++){ //2 is one row after the header rows
$title= $objWorksheet->getCellByColumnAndRow(0,$i)->getValue();
$first_name= $objWorksheet->getCellByColumnAndRow(1,$i)->getValue();
$last_name= $objWorksheet->getCellByColumnAndRow(2,$i)->getValue();
$date_of_birth= $objWorksheet->getCellByColumnAndRow(3,$i)->getValue();
$email=$objWorksheet->getCellByColumnAndRow(4,$i)->getValue();
$phone_number=$objWorksheet->getCellByColumnAndRow(5,$i)->getValue();
$company=$objWorksheet->getCellByColumnAndRow(6,$i)->getValue();
$input=array(
'title'=>$title,
'first_name'=>$first_name,
'last_name'=>$last_name,
'date_of_birth'=>$date_of_birth,
'email'=>$email,
'phone_number'=>$phone_number,
'company'=>$company,
);
You have to remember that PHPExcel is just PHP, and so is CodeIgniter. You don't have to load PHPExcel through another library. If fact, it doesn't do anything for you. I put all of the PHPExcel files in /third_party/, and then do this:
require_once( APPPATH . 'third_party/PHPExcel-1.8/Classes/PHPExcel.php');
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
Notice that in the example I am using v1.8
If you want to use $objReader in multiple methods, then just make it a class property. So $this->objReader instead.

How to close excel file in php-excel-reader

I am reading two excel file, using php-excel-reader (From this)
After reading two files of 1st row, I am comparing it. If they are same then I am appending contain of on file to other. To write the file I am using this
Now for doing this I want to close one file, but that function is not available in php-excel-reader
here is my code
compare file
{
$data = new Spreadsheet_Excel_Reader($filepath);
$data1 = new Spreadsheet_Excel_Reader($destinationfilepath);
}
unset($data);
unset($data1);
if($flag==0)
{
$excel = new ExcelWriter($destinationfilepath);
// read the source file
$finalarray= array();
for($m=1;$m<$sourcefilerowcount;$m++)
{
$charvalue='A';
$temprow=$m+1;
for($n=0;$n<$destinationcolnum;$n++)
{
$data = new Spreadsheet_Excel_Reader($filepath);
$finalarray[$n]=$data->val($temprow,$charvalue);
$charvalue++;
}
print_r($finalarray)."<br/>";
$excel->writeLine($finalarray);
}
There is no need to explicitly call close() function, because the file is automatically closed in the load() method. If you look at Excel2007.php, where PHPExcel_Reader_Excel2007 is defined, you'll see:
public function load($pFilename)
{
...
$zip = new ZipArchive;
$zip->open($pFilename);
...
$zip->close();
return $excel;
}
Just unset your PHPExcel_Reader object, and the data will be removed from memory:
$objReader = PHPExcel_IOFactory::createReader('Excel2003XML');
$objPHPExcel = $objReader->load("Excel2003XMLTest.xml");
...
unset($objPHPExcel);
unset($objReader);

Categories