Excel Data is not reading from amazon s3 server - php

I am uploading the excel file in amazon s3 server and its getting uploaded successfully, but while reading that same excel file in php getting the below data.
����E� �ɂ^R,5��� I
�ҧ���{ߙw�Ig�FS���셄�0b����[�Jb���k�>+��s��Q�z�� ,E�� º
����u��� ����O�DC�pn��Wi�_��p1F�h��axu���\tWW�N�b��~�:� >����*���p�1�ڧ 掓�f��Q����PK)��e�PK�;:K
xl/styles.xml�Y[o�0}߯���R�e
�Z�L���j�4i�&�8�Uǎ�B�| ! ��jT>���%7�l�Q��EA8��A�B���(�xA!�������rF�m�����J�r�"Lq���c�jb.2$UQ$N���B7ʨ�v:GN����&Y���|¤�QA��F
<�A`��<RR�\^]ޜ���9%��Ŝ-x���W<�DIW���rD2� 蘟�ʰ
xl/sharedStrings.xmlPK�;:K)��e��xl/workbook.xmlPK�;:K�,q�^�
Gxl/styles.xmlPK�;:K�!49���docProps/app.xmlPK�;:K�ma�m�docProps/core.xmlPK�;:K�������docProps/custom.xmlPK�;:K�#��C��[Content_Types].xmlPKh
using the below php code to print the data
$s3Client->registerStreamWrapper();
$stream = file_get_contents(utf8_encode($this->stock_catalog_xls_path), 'r');
echo $stream;
so please can you give me any idea how to read this excel data in php.

You are reading Excel as a raw file.
You need to pass the contents to an Excel library, so you can read them with cells, sheets format.
Below is the library that can help you in converting the raw file to excel readable format.
https://github.com/PHPOffice/PHPExcel
Sample implementation on how to read Excel file.
https://github.com/PHPOffice/PHPExcel/blob/1.8/Examples/07reader.php
Hope it helps.

Related

PHPExcel - File is not recognised as an OLE file

Trying to import XLS file to my mysql table.
But returns error below.
excelimport/storage/framework/laravel-excel/laravel-excel-BBA94ICWI0E7wrAuLevn89J6OElWa1qR.xls is not recognised as an OLE file
Here's my code.
I know that, the file got wrong format, but when I save as an excel (without changing anything), It works well.
But problem is; my users will upload files like that.
And I need to solve it without saying "please export your xls file as excel again".
$file = $request->file('file')->store('import');
Excel::import(new UserImportModel, $file);

Save xml api as and excel file in php

I have this code which save xml api as an xlsx. But When I save it, i can not open on computer. But txt doc and other formats support it. How can I save it as an xlsx file successfully.
<?php
$xlsx = file_get_contents("http://cbu.uz/uzc/arkhiv-kursov-valyut/xml/");
file_put_contents("currency/kurs.xlsx", $xlsx);
?>

Change excel file extension from xls to xlsx while uploading with php

Is it possible and if is how can i change excel file extension while uploading or before saving file on server? I am using php and mysql.
Thankyou
You can do something like this.
move_uploaded_file($_FILES['file']['tmp_name'], upload_PATH.'/'.$_FILES['file']['name'].'x');
But that will only change the file name with the xlsx extension. It will not actually convert the file to xlsx format.
As previously mentioned in a different reply, changing the extension won't actually change the format, and it's not a good idea to serve a .xls file as .xlsx, since this will only confuse anyone trying to read it.
What you could do (disregarding potential problems with converting and verification of the file) is read the uploaded file into a library like PHPExcel (http://phpexcel.codeplex.com) and then use the builtin functions to export it as an .xlsx file. Sample below:
// Create a reader to read .xls format
$reader = PHPExcel_IOFactory::createReader('Excel5');
// Read the .xls file from upload storage
$workbook = $reader->load($_FILES['file']['tmp_name']);
// Create a writer to output in .xlsx format
$writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
// Save file to destination .xlsx path
$writer->save($destination_path);
Keep in mind that although this might work perfectly well, the conversion might mess with the contents of the file. This might not be desirable, as the conversion can cause data loss, formatting changes and all sorts of weirdness.

xls file php download is in a different format than specified by the file extension

I'm struggling with my XML to XLS code in PHP.
I have PHP code that exports MySql data to XML and I have code that writes XML to XLS.
This all works fine, except that when I open the xls file, I get an error:
"The file you are trying to open is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"
my code:
<?php
$file = 'filename';
$url = "$file.xml"; // from xml file
header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=$file.xls" ); // to xls file
if (file_exists($url)) {
$xml = simplexml_load_file($url);
echo 'Voornaam'."\t" . 'Achternaam'."\t" . 'Geslacht'."\t" . 'Instrument'."\t\n";
foreach($xml->lid as $lid)
{
echo $lid->voornaam."\t" . $lid->achternaam."\t" . $lid->geslacht."\t" . $lid->instrument."\n";
}
}
?>
I found that it has something to do with the MIME_Types https://filext.com/faq/office_mime_types.php
The header("Content-Type: application/vnd.ms-excel"); is for the older versions of excel. But when I use the xlsx type, the file won't open at all.
Like I said, this code runs fine, downloads the Excel file in xls format to my computer. But how can I open it without the error?
I would also like to upgrade the file to xlsx format if someonw knows how to open the file in that format.
Well, your header and filename are both saying "this is an XLS file" - but it isn't, it's actually a TSV file (Tab-Separated Values). So yes, the message is accurate: the file extension is misleading.
To write an actual, valid XLS (or XLSX), it is easiest to use a library which does this for you - both these formats are complex and tricky. I've had best results using PhpExcel (example code).
When Excel opens an .xls file, it expects it to be in normal old binary Excel format. You seem to be outputting tab separated text (.tsv format), which Excel understands but doesn't want to open without issuing a warning. I think this warning was introduced with Excel 2007, and that earlier versions opened happily without warnings. Unfortunately I have no sources handy for this info, only personal experience.

Read data from xlsx file in php and print as php array

I need a php script which can read the data from an uploaded xlsx file. Also I need to print those data in an array format. Please help.
Save as CSV first and after that - parse it with http://gist.github.com/385876

Categories