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.
Related
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);
I need to edit a excel file in php. I've got a simple PHP Spreadsheet.
I use fat-free and the problem is
public function MakeExcel()
{
require 'vendor/autoload.php';
use \vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Spreadsheet ;
use \vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx ;
$Spreadsheet = new Spreadsheet();
$sheet = $Spreadsheet->getActiveSheet();
$sheet->setCellValue('A3', 'toto');
$writer = new Xlsx($Spreadsheet);
$writer->save("test.xlsx");
$f3->set('CONTENT','views/dashboard/dashboard_liste.html');
$f3->set('CONTENTJS','views/dashboard/dashboard_liste.js');
echo \Template::instance()->render('views/accueil/accueil.html','text/html');
$this->db = null;
}
use doesn't seem to exist in fat-free so i tried:
$f3->use(\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Spreadsheet);
Or
$f3->use("\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Spreadsheet");
and many other it return me the error :
ERROR 500 Call to a member function use() on null
Problem Solved thanks everyone :
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class MakeExcel extends Controller {
public function MakeExcel(){
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
}
}
First of all seems you use Fat Free wrong. Because $f3 is null.
Second. Probably you are importing classes with wrong namespaces. You can read documentation of PhpSpreadsheet.
Right way:
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
You can phisically find this classes and look on the namespace.
Put your import of composer autoload and use block outside of this function.
Make sure you ran composet install
Make your function work without PhpSpreadsheet. And after that try to add other functionality
You need to declare those files in php top area
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
Then in method create object for spreadsheet
$spreadsheet = new Spreadsheet();
Then you can define the border and alignment for xls file like that
$cell_st =[
'font' =>['bold' => true],
'alignment' =>['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER],
'borders'=>['bottom' =>['style'=> \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN],'top' =>['style'=> \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN]]
];
To Define the Header you can use below code
$spreadsheet->setActiveSheetIndex(0)->mergeCells('A2:D2');
$spreadsheet->setActiveSheetIndex(0)->setCellValue('A2', "<YOUR HEADER>");
$spreadsheet->getActiveSheet()->getStyle('A2:D2')->applyFromArray($cell_st);
And Then define the column names
$spreadsheet->setActiveSheetIndex(0)
->setCellValue('A3', 'ID')
->setCellValue('B3', 'NAME')
->setCellValue('C3', 'CITY')
->setCellValue('D3', 'SALARY');
After all above code now you can start your content in loop like that
$rowcount = 4;
foreach($data_array as $key=>$value){
$spreadsheet->setActiveSheetIndex(0)
->setCellValue('A'.$rowcount, $value['id'])
->setCellValue('B'.$rowcount, $value['name'])
->setCellValue('C'.$rowcount, $value['city'])
->setCellValue('D'.$rowcount, $value['salary']);
$rowcount++;
}
and in last now make object of the Xlsx class to save the excel file
$writer = new Xlsx($spreadsheet);
$file_name = "report.xls';
$file_root_path = "assets/reports/xls/".$file_name;
$file_site_path = "localhost/xls_generate/assets/reports/xls/".$file_name;
$writer->save($file_root_path);
// final xls file path is here
echo $file_site_path;
That's all, Hope this work for you..
I am using the latest master of PHPExcel (most recent commit March 28th 2015) to parse an xlsx file for a client, the following code reads the file without complaining but finds no data (the only output I currently get is NULL):
<?php
include 'PHPExcel/IOFactory.php';
$inputFileName = 'foo.xlsx';
try {
$objReader = PHPExcel_IOFactory::createReaderForFile($inputFileName);
$objPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
print_r('Error loading file "' . pathinfo($inputFileName,
PATHINFO_BASENAME) . '": ' . $e->getMessage());
}
$sheet = $objPHPExcel->getSheetByName("Orders");
var_dump($sheet);
?>
The workbook definition inside the xlsx file looks like this:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<x:workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4506" />
<x:workbookPr defaultThemeVersion="124226" />
<x:bookViews>
<x:workbookView xWindow="480" yWindow="345" windowWidth="24495" windowHeight="11955" />
</x:bookViews>
<x:sheets>
<x:sheet name="Orders" sheetId="4" r:id="relId4" />
</x:sheets>
<x:calcPr calcId="125725" />
</x:workbook>
I think it's not working out that there's an "Orders" sheet because it's looking for "sheets" in the default namespace rather than the "x" namespace. Sorry if terminology is a bit gibberish there but hopefully you can see what I mean.
There is definitely data there, in that worksheet, because I can open the file in LibreOffice and see it.
Is there a way to get PHPExcel to use the x namespace as the default or in some other way get it to load worksheets defined in this way?
In my quest to find out I found a bug report from 20101 regarding a similar issue which doesn't seem to have been addressed.
This issue is tracked at https://github.com/PHPOffice/PHPExcel/issues/571
What follows is a workaround, it is ugly but may help until this issue has been fixed...
Create a new class PHPExcel_Reader_Excel2007_XNamespace.php:
<?php
class PHPExcel_Reader_Excel2007_XNamespace extends PHPExcel_Reader_Excel2007
{
public function securityScan($xml)
{
$xml = parent::securityScan($xml);
return str_replace(['<x:', '</x:'], ['<', '</'], $xml);
}
}
Then load your excel file using this reader:
$excelReader = new PHPExcel_Reader_Excel2007_XNamespace();
$objPHPExcel = $excelReader->load($inputFileName);
UPDATE: A more general issue for handling of non-standard namespaces has been filed and an updated workaround (covering the "d" namespace as well) is found at https://github.com/PHPOffice/PHPExcel/issues/1187#issuecomment-295032648
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
I'm using Yii framework , and I have to load PHPExcel classes.
in my main.php I set the configuration as
'import'=>array(
'application.models.*',
'application.components.*',
'application.extensions.*',
'ext.PHPExcel.PHPExcel',
),
in I was edit the Register function inside Autoloader.php as follow
public static function Register() {
$functions = spl_autoload_functions();
foreach($functions as $function)
spl_autoload_unregister($function);
$functions=array_merge(array(array('PHPExcel_Autoloader', 'Load')), $functions);
foreach($functions as $function)
$x = spl_autoload_register($function);
return $x;
}
when I call $objPHPExcel =new PHPExcel(); it's working well,
but when I call $objDrawing = new PHPExcel_Worksheet_Drawing(); its return
include(PHPExcel_Worksheet_Drawing.php): failed to open stream: No such file or directory
my directory structure is :
extensions
-----PHPExcel
-------PHPExcel
------ PHPExcel.php
Thanks for help
Try this code out if it works, notice there are dual *
Yii::import('ext.PHPExcel.**', true);
$objDrawing = new PHPExcel_Worksheet_Drawing();
var_dump($objDrawing);
[Tested in Yii version: 1.1.13]
this module worked for me initially. but then I added Yii2 and spent a long time looking for a solution to the problem. for those who find this topic, as I did and also added Yii2 to Yii1, I will leave this solution.
for me first helped this.
spl_autoload_unregister(['YiiBase', 'autoload']);
require_once Yii::app()->params['rootPath'] . '/PHPExcel/Classes/PHPExcel.php';
spl_autoload_register(['YiiBase', 'autoload']);
when i added Yii2 i changed
spl_autoload_unregister(['Yii', 'autoload']);
spl_autoload_unregister(['YiiBase', 'autoload']);
require_once Yii::app()->params['rootPath'] . '/PHPExcel/Classes/PHPExcel.php';
spl_autoload_register(['YiiBase', 'autoload']);
spl_autoload_register(['Yii', 'autoload']);
and use
$objPHPExcel = new \PHPExcel();