PHPOffice and Read XLS in PHP - php

just a php beginner question.
How can I create an input to load the XLS file without having to download it on the server and use it in the code below where I have the variable $inputFileName.
<?php
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
**$inputFileName = './sampleData/example1.xls';**
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory to identify the format<br />';
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
echo '<hr />';
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);
?>
Thank you all...

Related

How to disable Multibyte function overloading in PHP for string functions

I got a problem when call the PHPExcel library AutoLoader.php file.
I try to upload the Excel file, then after clicking on the upload button, it saves data into the database. I get an error, as shown in the photo after uploading the file. How can I fix this?
<?php
require('library/Classes/PHPExcel/IOFactory.php');
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/');
if(isset($_POST['submit'])){
if((isset($_POST['file'])) && !empty($_POST['file']))
{
$file = $_POST['file'];
}
$fileName= $_FILES["file"]["name"];
echo ('fileName +'.$fileName);
//$uploadPath = $_SERVER['DOCUMENT_ROOT'].'/SMS/excel/' ;
$fileTempName= $_FILES["name"]["temp_name"];
//echo ('fileTempName +'.$fileTempName);
$fileExtension= pathinfo($fileName,PATHINFO_EXTENSION);
$allowedtype= array('xlsx','sls','xlsm');
if(!in_array($fileExtension,$allowedtype)){
echo("<br/>Sorry, File type is not allowed. Only Excel file.");
}
else {
echo("Correct File Extension");
try
{
$inputfiletype = PHPExcel_IOFactory::identify($fileName);
$objReader = PHPExcel_IOFactory::createReader($inputfiletype);
$objPHPExcel = $objReader->load($fileName);
echo 'Reading the number of Worksheets in the WorkBook<br />';
/** Use the PHPExcel object's getSheetCount() method to get a count of the number of WorkSheets in the WorkBook */
$sheetCount = $objPHPExcel->getSheetCount();
echo 'There ',(($sheetCount == 1) ? 'is' : 'are'),' ',$sheetCount,' WorkSheet',(($sheetCount == 1) ? '' : 's'),' in the WorkBook<br /><br />';
echo 'Reading the names of Worksheets in the WorkBook<br />';
/** Use the PHPExcel object's getSheetNames() method to get an array listing the names/titles of the WorkSheets in the WorkBook */
$sheetNames = $objPHPExcel->getSheetNames();
foreach($sheetNames as $sheetIndex => $sheetName) {
echo 'WorkSheet #',$sheetIndex,' is named "',$sheetName,'"<br />';
}
}
catch(Exception $e)
{
die('Error loading file "'.pathinfo($fileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
}
}
?>

phpexcel not hiding column

<?php
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
$inputFileType = 'Excel5';
$inputFileName = './source/marks.xls'; //using this in the meantime. Will replace later with an import feature
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load($inputFileName);
echo '<hr />';
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);
$objWorksheet = $objPHPExcel->getActiveSheet();
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setVisible(false);
echo '<table>' . "\n";
foreach ($objWorksheet->getRowIterator() as $row) {
echo '<tr>' . "\n";
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // This loops all cells,
foreach ($cellIterator as $cell) {
echo '<td>' . $cell->getValue() . '</td>' . "\n";
}
echo '</tr>' . "\n";
}
echo '</table>' . "\n";
?>
Please am working with this code for a school work. everything works well except the
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setVisible(false);
which is suppose to hide column D.
Function of the code is to read the individual marks from an excel file and display it in html view. eg.
My aim is to hide the Index no column from displaying in the html view. "removeColumn('A')" works but i am doing a search with the values in column A. So just want to hide that column.

Excel PHP - PHPExcel error

I'm trying to read excel file. but it's showing me error. I tried with this.
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
// Set the Excel file name and path
$inputFileName = 'uploads/aaa.xlsx'; // this is 2007 new format.
// Read your Excel workbook
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());
}
But this error is showing...
Fatal error: Class 'PHPExcel' not found in F:\xampp\htdocs\preme\PHPExcel\Reader\Excel2007.php on line 351
I autoload the "phpoffice/phpexcel" with composer. Make sure you get the same to your directory. If you do not use composer then point to the right directory. Just for demonstration I pointed to the folder down below
<?php
//require_once 'vendor/autoload.php';
require_once 'vendor\phpoffice\phpexcel\Classes\PHPExcel\IOFactory.php';
$inputFileName = 'uploads/aaa.xlsx';
// Read your Excel workbook
try {
$excelReader = PHPExcel_IOFactory::createReaderForFile($inputFileName);
$excelReader->setReadDataOnly();
$excelReader->setLoadAllSheets();
$excelObj = $excelReader->load($inputFileName);
//var_dump($excelObj);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}

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 ?

.xlsx to .csv with phpexcel creates readonly .csv file

I am converting a .xlsx file url-list-with-change-in-page-size.xlsx to url-list-with-change-in-page-size.csv file using phpexcel library.
My code looks :
<?
/** SCRIPT SETTINGS */
error_reporting(E_ALL);
/** PHPExcel */
require_once 'PHPExcel.php'; // (this should include the autoloader)
require_once 'PHPExcel/IOFactory.php';
$excel_readers = array(
'Excel5' ,
'Excel2003XML' ,
'Excel2007'
);
$reader = PHPExcel_IOFactory::createReader('Excel2007');
$reader->setReadDataOnly(true);
$path = '/opt/lampp/htdocs/Wdrive/url-list-with-change-in-page-size.xlsx';
$excel = $reader->load($path);
$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->save('/opt/lampp/htdocs/Wdrive/url-list-with-change-in-page-size.csv');
?>
this code successfully makes .csv file. but it is making read only file .
is there any way i can change or modify this code to make writable file programatically?
Thankyou

Categories