PHP: reading xls with phpexcel - php

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:

Related

Save File in PHPExcel

Is it posibble to save existing file with out creating the writer object in PHPExcel?
$filename = 'test.xlsx';
$phpExcel = PHPExcel_IOFactory::load($filename);
$phpExcel->save($filename);

How to convert a xlsx file into pdf using php

I am in searching to find a solution to convert the .xlsx(Excel) file into pdf files.
require_once('phpExcel/PHPExcel/IOFactory.php');
require_once('phpexcel/PHPExcel.php');
$fileType = 'Excel5';
$fileName = 'testing.xlsx';
$outputFileType = 'PDF';
$outputFileName = 'test.pdf';
$objPHPExcel = PHPExcel_IOFactory::load($fileName);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $outputFileType);
$objWriter->writeAllSheets();
$objWriter->save($outputFileName);
I used this above code,but is not working. If any can an Idea, please help me

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

PHPExcel - read 4 CSV files into a single workbook with 4 spreedsheets

I need to aggregate 4 CSV files into a single, Excel workbook using PHPExcel.
Working on a single CSV file and a mono-spreadsheet workbook all works fine.
Using more than one CSV, I'm unable to get each CSV file into a seperate sheet.
How can I achieve this using PHPExcel?
There is an example of this in the Documentation/Examples/Readers directory in the SVN repository for PHPEXcel: It's Example #13
include 'PHPExcel/IOFactory.php';
$inputFileType = 'CSV';
$inputFileNames = array('./example1.csv','./example2.csv','./example3.csv','./example4.csv');
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$inputFileName = array_shift($inputFileNames);
$objPHPExcel = $objReader->load($inputFileName);
$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
foreach($inputFileNames as $sheet => $inputFileName) {
$objReader->setSheetIndex($sheet+1);
$objReader->loadIntoExisting($inputFileName,$objPHPExcel);
$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
}
$loadedSheetNames = $objPHPExcel->getSheetNames();
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$objPHPExcel->setActiveSheetIndexByName($loadedSheetName);
echo $loadedSheetName,PHP_EOL;
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);
echo PHP_EOL;
}
You didn't specify exactly where the problem is...
For multiple worksheet excel file proceed as following:
Load CSV file
Create new worksheet
Write CSV to new worksheet
Go to 1
From docs:
If you need to create more worksheets in the workbook, here is how:
$objWorksheet1 = $objPHPExcel->createSheet();
$objWorksheet1->setTitle('Another sheet');
To set active worksheet:
$objWorksheet1->setActiveSheetIndex($index);

Categories