write the data to saved excel without affecting the format - php

I have saved excel sheet on my server,it has an format i trying to write data to the saved excel but when i trying write its removing the excel format (i.e it removing header color everything) it write start data in the first cell,i want to write the data to saved excel without changeling the excel format start from cell 10, below is the image how my excel looks like before performing the function.
and after how its,looks like.
before:
after
mycode
<?php
$File = "Book1.xls";
$Handle = fopen($File, 'w');
$Data = "Abdul Rahim\n";
fwrite($Handle, $Data);
$Data = "Raja\n";
fwrite($Handle, $Data);
print "Data Written";
fclose($Handle);
?>
can anyone help me how to do this .

You cannot modify the XLS just with fwrite. It has a strict format.
You could switch to CSV or use a third-party library like PhpExcel.

Related

Read vbaProject.bin for macrosCode in PHP

I am trying to create a .xlsm excel file while reading the macrosCode from the vbaProject.bin file.
I implemented the following snippet:
$filename = 'private://zips/xl/vbaProject.bin';
$handle = fopen($filename, "rb");
$contents = stream_get_contents($handle);
fclose($handle);
$spreadsheet->setMacrosCode($contents);
This is a workaround because the PhpSpreadsheet library does not support generating excel file with macros. But somehow, when reading the vbaProject.bin file, the format seems incorrect, might be because of the encoding. https://i.stack.imgur.com/7SGbK.png
My question is: could I read the vbaProject.bin somehow with the proper format?

Display issue when reading a file in php

i have an simple xls file and it's print me that :
PK!b�h^�[Content_Types].xml �(����N�0E�H�C�-Jܲ#5��Q>�ēƪc[�ii����B�j7���{2��h�nm���ƻR����U^7/���%��rZY�#1__�f��q��R4D�AJ�h>����V�ƹ�Z�9����NV�8ʩ����ji){^��-I�"{�v^�P!XS)bR�r��K�s(�3�c�0��������7M4�����ZƐk+�|\|z�(���P��6h_-[�#�!���Pk���2n�}�?�L��� ��%���d����dN"m,�ǞDO97*�~��ɸ8�O�c|n���E������B��!$}�����;{���[����2���PK!�U0#�L_rels/.rels �(���MO�0��H�����ݐBKwAH�!T~�I����$ݿ'T�G�~����<���!��4��;#�w����qu*&r�Fq���v�����GJy(v��*����K��#F��D��.W ��=��Z�MY�b���BS�����7��ϛז�� ?�9L�ҙ�sbgٮ|�l!��USh9i�b�r:"y_dl��D���|-N��R"4�2�G�%��Z�4�˝y�7 ë��ɂ�����PK!��3_I�xl/workbook.xml�Umo�8�~���w�M0oj� o�J{����~�tr�+�9c�T���CH�����^���c?3����ӡm�&.���ϐi��%������ LcP�+i#:�6�~�����^�ݝ;�am�J��mE�Z:���u`��l������^2Z5c�ml!�n)��!���U���blY�f����P�~X���=p-���� ��q��'P�h�����5��� ���#��&0�������#�3�7�cdc���1x�kK��uO���AV� �{���0Hk�J�� 9qs��y�v3Kנ}�mu��h蠲�+V�M�b�^mȱ�Gހ�qV1��IΗ�(YE�F]��x��� �m���b�������jns�I-#���{�Aa���Wiѻᒪ�e�6�����߂��MٰS��}�K����2i�c�8�����d���RI�/�ϐ�?���^�����Eq� ��J�6��4O��x؊]B�e> �7�BzQ!��c�5��t�L���b�(y�|
my code is : i tried :
$file = file_get_contents('...\TestExcel.xlsx');
echo $file;
and also :
$file = file('......\TestExcel.xlsx')
foreach($file as $files){
echo $files;
}
Whats wrong please ?
Looks like a binary file. An XLSX file is a Microsoft Excel Open XML Format Spreadsheet file. It's a ZIP-compressed, XML-based spreadsheet, which you can't open the way you tried.
I think it is possible to unzip it and get to the actual content of the spreadsheet, which is basically just xml.

Save PDF as a blob in a database using PHP?

I have a PDF file on my server that I want to select and transform into a blob for insertion into my database (using an INSERT INTO command). My first problem is getting hold of the PDF using PHP. I know it is done with the file_get_contents() function, but I do not understand what parameters it needs.
$fp = fopen($fileLocation, 'r');
$content = fread($fp, filesize($fileLocation));
$content = addslashes($content);
fclose($fp);
You can save the $content to blob field in mysql

PHP Export Excel to specific Path?

Is it possible to export a PHP MySQL Excel sheet to a specified path such as USB Flash Drive.
Its because I'm using php as Point of Sale and all What i want now is once you click on a Button- it will collects records from MySQL database and exports it as excel or csv file to a USB Flash Drive.
I've tried to Google it out, but I can't seem to find anything.
Thank You.
You need to write the file. To write to usb drive you just need to specify correct file path. If you're on *nix it would be like /media/USB/ on linux or /Volumes/USB/ on Mac. For Windows it would be like F:/.
so rough example is like
$a = array('1','2','3'); //This is imaginable row from MySQL
$f = fopen('F:/mycsv.csv', 'w');
fputcsv($f, $a);
fclose($f);
Manual for fputcsv is here.
If needed, you can ask user for a file path in any way supported by your application.
Not a simple process to save it as excel, but relatively straightforward to write a csv.
$temp = tempnam(sys_get_temp_dir(),'tmp');
$handle = fopen($temp,'w');
$query = $pdo->prepare("select * from table");
$query->execute();
$row=$statement->fetch();
$keys=array_keys($row);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=export_'.date('dmY').'.csv');
fputcsv($handle,$keys);
while($row){
fputcsv($handle,$row);
$row=$statement->fetch();}
echo file_get_contents($temp);
fclose($handle);
This creates a temporary file in your temp folder, you write the csv to that and then give it csv headers and echo it out which gives the user a save file box.
I call $row once before the loop to get the table columns with array_keys and output those first and then loop through with a while calling $row=$statement->fetch() at the end of the loop.

to fetch doc file content

$myFile = "pdf/document_file.doc";
$fh = fopen($myFile, 'r');
$theData = file_get_contents($myFile);
fclose($fh);
echo "<pre>";
echo $theData;
echo "</pre>";
My above script fetches the data from document file but it display some of binary numbers.
I need to display document file content in to html content
For Example
If my document file is a resume. i need to fetch the content and display document data same as selected file in html page
It IS binary data. To view it as HTML you need to convert it first.
Convert .doc to html in php
You're reading binary data. Hence the "numbers". The raw data needs to go through some "filter/render implementation" first.
Plus, when you use file_get_contents() there's no need to fopen() and fclose().

Categories