PHPExcel can't have 2 reader in the same web page? - php

I'm using PHPExcel to read 3 files. 2 of them are xls and 1 of the is xlsx. I create 2 reader for the task but encountered error.
HTTP Error 500.0 - Internal Server Error
C:\AppServ\php5\php-cgi.exe - The FastCGI process exited unexpectedly
Here is my code
$inputFileType = PHPExcel_IOFactory::identify($conPath);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($conPath);
$objReader = PHPExcel_IOFactory::createReader("Excel5");
$objPHPExcelShip = $objReader->load("Ship to mapping.xls");
$objPHPExcelMat = $objReader->load("Material Mapping.xls");
The $inputFileType is variable for the type of uploaded file(which is xlsx.). The code works fine if I upload xls. but give error if the uploaded file is xlsx.
Please help.

You can't if you use the same variable for each reader instance: you need to use a separate reader instance for each reader that you instantiate:
$objReader1 = PHPExcel_IOFactory::createReader("Excel5");
$objPHPExcelShip = $objReader1->load("Ship to mapping.xls");
$objReader2 = PHPExcel_IOFactory::createReader("Excel5");
$objPHPExcelMat = $objReader2->load("Material Mapping.xls");
or (at least) instantiate a new reader for each file as you load it
$objReader = PHPExcel_IOFactory::createReader("Excel5");
$objPHPExcelShip = $objReader->load("Ship to mapping.xls");
$objReader = PHPExcel_IOFactory::createReader("Excel5");
$objPHPExcelMat = $objReader->load("Material Mapping.xls");

Related

PHPExcel fails on a heavy file import

I am trying to import excel files (xlsx) using PHPExcel library and store the data in a database. Following is my code:
require_once $_SERVER['DOCUMENT_ROOT']."/application/views/subfolder/PHPExcel/Classes/PHPExcel/IOFactory.php";
$file_name = $_FILES['controlname']['tmp_name'];
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$inputFileName = $file_name;
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();
When I import a 1MB+ file having more than 1000 records, the code fails without returning any error with a strange behavior. However when I reupload that file by splitting it into multiple excels such that each excel contains only 1000 records, the same code works successfully.
To test, I tried echoing some message before and after the following line:
$objPHPExcel = $objReader->load($inputFileName);
The message echoed before this line is printed, but the message written after this line does not work.
In order to allow heavy loads I have made settings of ini in MBs as well as GBs, but nothing works.
ini_set("pcre.backtrack_limit", "100000000");
ini_set("max_allowed_packet ", "2G");
ini_set("max_execution_time ", "20000");
ini_set('max_input_time','20000');
ini_set('memory_limit', '2G');
ini_set('upload_max_filesize','20M');
ini_set('post_max_size', '2G');
set_time_limit(0);

Error when i wont to save file as PDF using PHPExcel

I'm using the PHPExcel library in my project and its work fine when i wont to save files format (xls/xlsx). But I need to save files as a PDF format and i found some examples but none of them is working for me.
here the code which i use to generate file with pdf format
<?php
error_reporting(E_ALL);
ini_set('include_path', ini_get('include_path').';../Classes/');
include 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$inputFileName = 'file.xls';
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
$objPHPExcel->setActiveSheetIndex(0);
$row = $objPHPExcel->getActiveSheet()->getHighestRow()+1;
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objSheet = $objPHPExcel->getActiveSheet();
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'test test');
$objWriter->save('test100.pdf');
?>
and here what the browser show me
( ! ) Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'PDF Rendering library has not been defined.' in C:\wamp\www\projet\PHPExcel\Classes\PHPExcel\Writer\PDF.php on line 56
what did i do wrong
PHPExcel doesn't have a PDF Rendering library built-in as part of the code/distribution. Instead, it allows you to use any of 3 different 3rd party PDF libraries (tcPDF, DomPDF or mPDF).
You have to install that PDF Rendering library separately, and then you need to tell PHPExcel which PDF library you have installed before you can save a file as PDF.
This is explained in the PHPExcel documentation and shown in the examples like 21pdf.php

reading xls and xlsx file using PHPExcel not working

when i reading xls or xlsx using PHPExcel it leads to a browser message The connection was reset
but when i read csv file it works fine.
my code is
/* checking extension*/
if($ext=="xls")
{
$objReader = new PHPExcel_Reader_Excel5();
}
else if($ext=="csv")
$objReader = new PHPExcel_Reader_CSV();
else
$objReader = new PHPExcel_Reader_Excel2007();
/* ........end checking .............. */
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($name);

PHPExcel not working with xls file

I am using PHPExcel to read excel file in php,it is working fine with xlsx but when try to read xls file,It shows an error
Fatal error: Call to undefined method PHPExcel_Reader_CSV::setReadDataOnly() in /var/www/....
Here is my code
$file_path='/var/www/html/site/sample.xls';
$inputFileType = PHPExcel_IOFactory::identify( $file_path);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load( $file_path);
$total_sheets=$objPHPExcel->getSheetCount();
$allSheetName=$objPHPExcel->getSheetNames();
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
for ($row = 1; $row <= $highestRow;++$row)
{
for ($col = 0; $col <$highestColumnIndex;++$col)
{
$value=$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
$arraydata[$row-1][$col]=trim($value);
}
}
The file that you're loading is identified as a CSV file, even though it has an .xls extension... it is quite a common practise for some developers to save files formatted as csv or with html markup with a .xls extension, but that doesn't make them BIFF-format .xls files.
The setReadDataOnly() method isn't available for the CSV Reader, because a CSV file cannot contain anything other than data.
The most recent versions of PHPExcel provide a stub for setReadDataOnly() in the CSV Reader to prevent an error in this situation, and I'd certainly recommend that you upgrade to the latest code; but if you can't do that, then the simplest fix for you is simply to wrap the call to setReadDataOnly() in an if test:
$inputFileType = PHPExcel_IOFactory::identify( $file_path);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
if ($inputFileType !== 'CSV') {
$objReader->setReadDataOnly(true);
}
For binary Excel files (xls) you may have better luck using the (old) PHP-ExcelReader. It's the most reliable one I've found so far.

PHP code can insert image to excel file and open it correctly in MS Excel?

I already have a document of excel and I want to use php to insert an image to that excel.
Is it possible to do that? How to implement it (code)?
Thanks,
$fileType = 'Excel2007';
$fileName = 'test.xlsx';
// Load the workbook
$objPHPExcelReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objPHPExcelReader->load($fileName);
// Add an image to the worksheet
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('My Image');
$objDrawing->setDescription('The Image that I am inserting');
$objDrawing->setPath('./images/myImage.png');
$objDrawing->setCoordinates('B2');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
// Save the workbook
$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,$fileType);
$objPHPExcelWriter->save($fileName);

Categories