how to delete image/ images from excel file using PHPExcel? - php

Can anybody help to solve this issue, i am new for PHPExcel Libraries,
I want to delete image which is on excel file using PHPExcel.
I did some code here but i am getting error of
Notice: main(): ArrayIterator::next(): Array was modified outside object and internal position
<?php
require_once '../Classes/PHPExcel/IOFactory.php';
require_once '../Classes/PHPExcel.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load(files/test.xlsx);
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel = $objPHPExcel->getActiveSheet(1)->getDrawingCollection();
foreach ($objPHPExcel as $key => $drawing){
$objPHPExcel->offsetUnset($key);
}
$objReader = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objReader->save('excel-files/new_test.xlsx');
?>

Related

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

PHPExcel xls file reading and HTML display

I'm losing time as hell using this PHPExcel lib.
My code is widely inspired from models which are available in the lib.
+The mission is :
-To read an *.xls file with PHP
-Display it in a navigator in HTML
For the moment, I just can read the file successfully (with a "createReader" method of PHPExcel_IOFactory instance). But for the HTML display, I'm in a pretty good crap. At attempt of code correction gives me this error:
" Fatal error: Uncaught exception 'PHPExcel_Calculation_Exception' with
message 'Réalisé S03!P374 -> Réalisé S03!P374 -> Formula Error:
Unexpected ,' "
Here is the code :
<?phperror_reporting(E_ALL);
set_time_limit(0);
date_default_timezone_set('Europe/London');
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHPExcel Reader Example #02</title>
</head>
<body>
<h1>Chargement fichier Facturation</h1>
<?php
$inputFileName = '../Docs_PDC/FACTURATION_Janvier.xls';
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using PHPExcel_Reader_Excel5<br />';
$objReader = new PHPExcel_Reader_Excel5();
// $objReader = new PHPExcel_Reader_Excel2007();
// $objReader = new PHPExcel_Reader_Excel2003XML();
// $objReader = new PHPExcel_Reader_OOCalc();
// $objReader = new PHPExcel_Reader_SYLK();
// $objReader = new PHPExcel_Reader_Gnumeric();
// $objReader = new PHPExcel_Reader_CSV();
$objPHPExcel = $objReader->load($inputFileName);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->save('Facturation.html');
echo '<hr />';
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);
$filename = "DownloadReport";
$table = $_POST['table'];
// save $table inside temporary file that will be deleted later
$tmpfile = tempnam(sys_get_temp_dir(), 'html');
file_put_contents($tmpfile, $table);
// insert $table into $objPHPExcel's Active Sheet through $excelHTMLReader
$objPHPExcel = new PHPExcel();
$excelHTMLReader = PHPExcel_IOFactory::createReader('HTML');
$excelHTMLReader->loadIntoExisting($tmpfile, $objPHPExcel);
$objPHPExcel->getActiveSheet()->setTitle('Facturation'); // Change sheet's title if you want
unlink($tmpfile); // delete temporary file because it isn't needed anymore
header('Content-Type: application/vnd.openxmlformats- officedocument.spreadsheetml.sheet'); // header for .xlxs file
header('Content-Disposition: attachment;filename='.$filename); // specify the download file name
exit;
?>
<body>
If someone has any idea I'll eternally grateful to him (or her ;) )
I dug a little more on this, and i noticed that: the exception is raised because
PHPExcel tries to resolve a formula with an Excel reference value (like "P$7") in argument. And the referenced value is not a number, it's just a column name.
So, Is it possible to disable PHPExcel's formula solving in some fields ? Or is there any other way to get around this ?

PHPExcel Writer create blank file

i try to create new excel file using phpexcel writer
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'Classes/PHPExcel/IOFactory.php';
include 'Classes/PHPExcel/Writer/Excel2007.php';
$inputFileName ='mak.xlsx';
try
{
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader -> load($inputFileName);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('MyExcel.xlsx');
}
catch(Exception $e)
{
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
?>
i first load exiting excel file then pass the file data into PHPExcel_Writer_Excel2007($objPHPExcel). new file is create as a blank file
Try this:
$sheet = $objPHPExcel->setActiveSheetIndex(0);
$sheet->setCellValue('A1','TEST');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
In your example $objPHPExcel is empty so that's why you get empty document

PHPExcel function getHighestRow() is NOT working for .xls but working for .xlsx

I am using PHPExcel to read its content based on no.of rows present.
To find no.of rows I am using following function.
$objSheet->getHighestRow();
It is working fine for .xlsx files.
But it is NOT working in .xls.
Fatal error: Call to a member function getHighestRow() on a non-object
So how i can get no.of rows of excel ?
PHP Code:
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
/** PHPExcel_IOFactory */
include 'www/PHPExcelReader/Classes/PHPExcel/IOFactory.php';
include 'www/PHPExcelReader/Classes/PHPExcel/Classes/PHPExcel.php';
try {
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_sqlite3;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadDataOnly(true);
$objReader->setLoadSheetsOnly( array("Sheet1") );
$objPHPExcel = $objReader->load($inputFileName);
$objSheet = $objPHPExcel->getActiveSheet();
$no_of_rows=$objSheet->getHighestRow();
//echo "no_of_rows=$no_of_rows";
I want both xls & .xlsx to be supported... Please guide me.
I solved this problem myself.
It is due excel version incompatibility.
Here is the code to resolve this issue.
/** Identify the type of $inputFileName **/
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
/** Create a new Reader of the type that has been identified **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);

PHPExcel converting multiple sheet of xlsx to csv

im using this few lines to convert an xlsx, which contain 4sheets to convert to .csv . but it only convert the first sheet of the xlsx file. how can i make it to convert every sheets in the xlsx. heres the code,
error_reporting(E_ALL);
date_default_timezone_set($this->vendor_timezone);
/** PHPExcel_IOFactory */
require_once sfConfig::get('sf_root_dir').'/lib/PHPExcel/IOFactory.php';
$file=sfConfig::get("sf_upload_dir").DIRECTORY_SEPARATOR."temp".DIRECTORY_SEPARATOR."1344500254_MyExcel.xlsx";
// Check prerequisites
//print sfConfig::get("sf_upload_dir").DIRECTORY_SEPARATOR."temp".DIRECTORY_SEPARATOR; exit;
if (!file_exists($file)) {
exit($file."Please run 06largescale.php first.\n");
}
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($file);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save(str_replace('.xlsx', '.csv',$file));
return "success";
Please check the tutorial here which has step by step code to achieve this
<?php
require_once 'PHPExcel/PHPExcel/IOFactory.php';
$excel = PHPExcel_IOFactory::load("test123.xlsx");
$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->setDelimiter(";");
$writer->setEnclosure("");
$writer->save("test123.csv");
?>
To convert all sheets you must iterate through all worksheets and set it as the active sheet and then write that to a file.
$inFile = 'fileWithMultipleWorksheet.xlsx';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($inFile);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$index = 0;
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$objPHPExcel->setActiveSheetIndex($index);
// write out each worksheet to it's name with CSV extension
$outFile = str_replace(array("-"," "), "_", $worksheet->getTitle()) .".csv";
$objWriter->setSheetIndex($index);
$objWriter->save($outFile);
$index++;
}
Write each sheet in turn to a different file, and then concatenate those files into one: PHPExcel does not provide an option to write multiple sheets to a single CSV file.

Categories