ms excel file doesn't load in PHPExcel - php

ms excel file doesn't load in PHPExcel but when i create excelsheets using libreoffice in linux it's load and upload in database.
$temp=$_FILES['file']['tmp_name'];
$destination = "/var/www/html/recovery";
move_uploaded_file($temp,$destination."/"."recovery"."."."xlsx");
chmod("/var/www/html/recovery/recovery.xlsx", 0777);
$inputFileName ='recovery.xlsx';
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());
}
$allDataInSheet = $objPHPExcel->getActiveSheet()- >toArray(null,true,true,true);
$arrayCount = count($allDataInSheet);
what i do for upload all type of excel file in database using PHPExcel.

Related

PHP: reading xls with phpexcel

I am having trouble reading the .xls file. Values ​​are read as null and in unknown characters. The file is downloaded from an ftp server and it comes protected
$path = 'doc/20180719-ASK FIDC_EVOLUCAO_COTA.xls';
$inputFileType = PHPExcel_IOFactory::identify($path);
$reader = PHPExcel_IOFactory::createReader($inputFileType);
$reader->setReadDataOnly(true);
$reader->setInputEncoding('ISO-8859-1');
//$reader->setDelimiter("\t");
$excel = $reader->load($path);
var_dump($excel);
echo $inputFileType;
$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->setUseBOM(true);
$writer->save('data.csv');
echo 'File saved to csv format';
The converted file looks like the image:

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 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

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);

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