I am working on a project in which i need to import data form a excel(.xlsx) fileinto a table in database. I already write code for a controller upload.php
$this->load->library('excel');
ini_set('memory_limit', '-1');
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$path = $_SERVER['DOCUMENT_ROOT'] . '/bitcoinreports/uploads/data.xlsx';
$objPHPExcel=$objReader->load($path);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
//loop from first data until last data
for($i=2; $i<=77; $i++){
$name = $objWorksheet->getCellByColumnAndRow(0,$i)->getValue();
$email = $objWorksheet->getCellByColumnAndRow(1,$i)->getValue();
$phone = $objWorksheet->getCellByColumnAndRow(2,$i)->getValue();
$details = $objWorksheet->getCellByColumnAndRow(3,$i)->getValue();
$location = $objWorksheet->getCellByColumnAndRow(4,$i)->getValue();
$data_user = array(
"name" => $name,
"email" => $email ,
"phone" => $phone ,
"details" => $details ,
"location" => $location ,
);
$this->dashboard_model->add_data($data_user);
}
Whenever i call this controller i got the following error
Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open C:/xampp/htdocs/bitcoinreports/uploads/data.xlsx for reading! File does not exist.' in C:\xampp\htdocs\bitcoinreports\application\third_party\PHPExcel\Reader\Excel2007.php:347
Stack trace:
#0 C:\xampp\htdocs\bitcoinreports\application\controllers\admin\upload.php(40): PHPExcel_Reader_Excel2007->load('C:/xampp/htdocs...')
#1 [internal function]: Upload->index()
#2 C:\xampp\htdocs\bitcoinreports\system\core\CodeIgniter.php(359): call_user_func_array(Array, Array)
#3 C:\xampp\htdocs\bitcoinreports\index.php(202): require_once('C:\\xampp\\htdocs...')
#4 {main}
thrown in C:\xampp\htdocs\bitcoinreports\application\third_party\PHPExcel\Reader\Excel2007.php on line 347
How to solve this problem ?
Before opening the file, check if it exists, AND is readable for the process that runs PHP (xampp in this case. Not sure how the user is named in xampp, maybe apache).
http://php.net/manual/en/function.is-readable.php
is_readable — Tells whether a file exists and is readable
So replace this:
$objPHPExcel=$objReader->load($path);
with
if (is_readable($path)){
$objPHPExcel=$objReader->load($path);
} else {
// handle problem, for example
echo "cannot read $path";
exit;
}
Related
I get error:
Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Exception: Column
string index can not be longer than 3 characters in
/var/www/html/domain.com/phpspreadsheet/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Coordinate.php:305
Stack trace: #0
/var/www/html/domain.com/phpspreadsheet/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Worksheet/Worksheet.php(1430):
PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString() #1
/var/www/html/domain.com/exceltests.php(393):
PhpOffice\PhpSpreadsheet\Worksheet\Worksheet->getColumnDimension() #2
{main} thrown in
/var/www/html/domain.com/phpspreadsheet/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Coordinate.php
on line 305
And this is the line #393 in my file:
$spreadsheet->getActiveSheet()->getColumnDimension("B$i")->setWidth(90);
EDIT More code:
$blob = returnItemImageOnly($pdo, $row["item"]);
if($blob){
$spreadsheet->getActiveSheet()->getColumnDimension("B$i")->setWidth(90);
$spreadsheet->getActiveSheet()->getRowDimension("$i")->setRowHeight(80);
$type = $blob['type'];
$image = base64_encode($blob['image']);
$imgdata = base64_decode($image);
$mimetype = getImageMimeType($imgdata);
$fname = "tmp/".rand(). ".".$mimetype;
$forUnlink[] = $fname;
file_put_contents($fname, $imgdata);
$drawing = new PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setPath($fname);
$drawing->setCoordinates("B$i");
$drawing->setResizeProportional(false);
$drawing->setWidth(75);
$drawing->setHeight(75);
$drawing->setOffsetX(34);
$drawing->setOffsetY(15);
$drawing->setWorksheet($sheet);
}
$spreadsheet->getActiveSheet()->mergeCells("B$i:C$i");
I searched for possible issues but no luck!
Maybe someone had the same issue?
Thanks in advance!
<?php
require_once 'PHPExcel/PHPExcel.php';
$downloadedFileName = "sheet.xls";
set_time_limit(0);
if (file_exists($downloadedFileName)) {
unlink($downloadedFileName);
}
$spreadsheet_url="Google Sheet with 4 tabs URL goes here";
file_put_contents($downloadedFileName,file_get_contents($spreadsheet_url));
if (file_exists($downloadedFileName)) {
$inputFileType = PHPExcel_IOFactory::identify($downloadedFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcelReader = $objReader->load($downloadedFileName);
$loadedSheetNames = $objPHPExcelReader->getSheetNames();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$objWriter->setSheetIndex($sheetIndex);
$objWriter->save($loadedSheetName.'.csv');
}
}
else{
echo "File not found";
}
?>
This gives an error :
Fatal error: Uncaught exception 'PHPExcel_Calculation_Exception' with
message 'ArticulosQueNoSonKARE!A1 -> internal error' in
D:\xampp\htdocs\xlstocsv\PHPExcel\PHPExcel\Cell.php:291 Stack trace: #0
D:\xampp\htdocs\xlstocsv\PHPExcel\PHPExcel\Worksheet.php(2492): PHPExcel_Cell-
>getCalculatedValue() #1
D:\xampp\htdocs\xlstocsv\PHPExcel\PHPExcel\Writer\CSV.php(142):
PHPExcel_Worksheet->rangeToArray('A1:M1', '', true) #2
D:\xampp\htdocs\xlstocsv\index.php(30): PHPExcel_Writer_CSV-
>save('ArticulosQueNoS...') #3 {main} thrown in
D:\xampp\htdocs\xlstocsv\PHPExcel\PHPExcel\Cell.php on line 291
I can figureout that its because of some cells containing formulas..But I need to take that value from the cell.
I have no edit permission to modify and edit those cells. I need this script running as a chrone job.. Ihave done with the other part.
Please help.
Thank you
I am editing .xlsm file. Which has macro and multiple tabs.
EDIT
I tried with macro enable .xlsx file but same issues.
I always get below error when save file after editing.
Fatal error: Uncaught exception 'PHPExcel_Exception' with message
'Invalid cell coordinate 'I|Gen'' in
D:\xampp\htdocs\work\PHPExcel-1.8\Classes\PHPExcel\Cell.php:594 Stack
trace: #0
D:\xampp\htdocs\work\PHPExcel-1.8\Classes\PHPExcel\Cell.php(651):
PHPExcel_Cell::coordinateFromString(''I|Gen'') #1
D:\xampp\htdocs\work\PHPExcel-1.8\Classes\PHPExcel\Cell.php(624):
PHPExcel_Cell::absoluteCoordinate(''I|Gen'') #2
D:\xampp\htdocs\work\PHPExcel-1.8\Classes\PHPExcel\Writer\Excel2007\Workbook.php(323):
PHPExcel_Cell::absoluteReference(''I|Gen'') #3
D:\xampp\htdocs\work\PHPExcel-1.8\Classes\PHPExcel\Writer\Excel2007\Workbook.php(300):
PHPExcel_Writer_Excel2007_Workbook->writeDefinedNameForNamedRange(Object(PHPExcel_Shared_XMLWriter),
Object(PHPExcel_NamedRange)) #4
D:\xampp\htdocs\work\PHPExcel-1.8\Classes\PHPExcel\Writer\Excel2007\Workbook.php(269):
PHPExcel_Writer_Excel2007_Workbook->writeNamedRanges(Object(PHPExcel_Shared_XMLWriter),
Object(PHPExcel)) #5
D:\xampp\htdocs\work\PHPExcel-1.8\Classes\PHPExcel\Writer\Excel2007\Workbo
in D:\xampp\htdocs\work\PHPExcel-1.8\Classes\PHPExcel\Cell.php on line
594
Below is my code
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcelR = $objReader->load("20170407Simpledevelopmentv0.2.xlsm");
$objPHPExcelR->setActiveSheetIndex(11);
$worksheet = $objPHPExcelR->getActiveSheet();
$worksheet->setCellValueByColumnAndRow('4','28','22,000');
//echo "kapil". $worksheet->getCellByColumnAndRow('4','28')->getValue(); die;
$objPHPExcel = new PHPExcel();
$objPHPExcel = $objPHPExcelR;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//$objWriter->setPreCalculateFormulas(true);
$objWriter->save('20170407Simpledevelopmentv0.2.xlsm');
exit;
I get error when save the file. If I remove comment echo "kapil". $worksheet->getCellByColumnAndRow('4','28')->getValue(); die; then it show data. I tried with the $objWriter->setPreCalculateFormulas(false); but same issue.
I am creating a form for users that will allow them to upload .csv files and xls/xlsx files. Currently, the program does allow them to upload .csv files, which are used to update the Oracle 11g database I am working off of. However, I cannot seem to figure out how to open, then right the xsl/xlsx files to csv's. I keep getting this error:
Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open uploaded.xls for reading! File does not exist, or it is not readable.' in /opt/apache/servers/planninganddesign/htdocs/LG/SLCCA/Classes/PHPExcel/Shared/OLERead.php:80 Stack trace: #0 /opt/apache/servers/planninganddesign/htdocs/LG/SLCCA/Classes/PHPExcel/Reader/Excel5.php(1164): PHPExcel_Shared_OLERead->read('uploaded.xls') #1 <DIRECTORY> (612): PHPExcel_Reader_Excel5->_loadOLE('uploaded.xls') #2 /opt/apache/servers/planninganddesign/htdocs/LG/SLCCA/update_handler2.php(12): PHPExcel_Reader_Excel5->load('uploaded.xls') #3 {main} thrown in /opt/apache/servers/planninganddesign/htdocs/LG/SLCCA/Classes/PHPExcel/Shared/OLERead.php on line 80
It's a very long error, but I think just the first line is my main issue at the moment.
update2.php (form) [I doubt you need this]:
<?php require('reader.php'); ?>
<form name="file" enctype="multipart/form-data" action="update_handler2.php" method="post" >
<u>GF:</u> <input type="file" name="uploaded"><br>
<u>GF:</u> <input type="number" name="numbers"><br>
<input type="submit" value="Submit">
</form>
update_handler2.php (handler):
<?php require_once'Classes/PHPExcel/IOFactory.php'; ?>
<?php
$file = $_FILES['uploaded']['tmp_name'];
$inputFileType = 'Excel5';
$inputFileName = '$file';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcelReader = $objReader->load($inputFileName);
$loadedSheetNames = $objPHPExcelReader->getSheetNames();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$objWriter->setSheetIndex($sheetIndex);
$objWriter->save($loadedSheetName.'.csv');
}
?>
Does anyone know how to fix to this issue?
$inputFileName = '$file';
Is your file really called $file (you're quoting it as a string literal).... or is the filename in the variable $file
$inputFileName = $file;
I am trying to create Excel File and appending data to it from a webpage. Excel file is created corrected and data is also saved in it, but when I try to load the file again and append data to it. it gives a Fatal error
Uncaught exception 'PHPExcel_Writer_Exception' with message 'File
zip:///home/timespk/public_html/htmlParser/index.xlsx#xl/media/b1b39dadf76812b4c58e06ea6ddf57841.png
does not exist' in
/home/timespk/public_html/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php:216
Stack trace: #0
/home/timespk/public_html/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php(164):
PHPExcel_Writer_Excel2007_ContentTypes->_getImageMimeType('zip:///home/tim...')
1 /home/timespk/public_html/Classes/PHPExcel/Writer/Excel2007.php(224):
PHPExcel_Writer_Excel2007_ContentTypes->writeContentTypes(Object(PHPExcel),
false) #2 /home/timespk/public_html/htmlParser/index.php(216):
PHPExcel_Writer_Excel2007->save('/home/timespk/p...') #3 {main} thrown
in
/home/timespk/public_html/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php
on line 216
Can anyone tell what I am doing wrong here?
This is the code that I am using to save Image to Excel file
$objPHPExcel2->getActiveSheet()->getColumnDimension('A')->setWidth(22);
$image = file_get_contents($imgSrc);
file_put_contents('image' . $highestRow . '.jpg', $image);
$gdImage = imagecreatefromjpeg('image' . $highestRow . '.jpg');
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setImageResource($gdImage);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(150);
$objDrawing->setCoordinates('A'. $highestRow);
$objDrawing->setWorksheet($objPHPExcel2->getActiveSheet());
I believe that I've found proper solution to this problem which works with PHPExcel_Worksheet_Drawing:
https://stackoverflow.com/a/23951597/925196