PHP: How to embed image in an excel column generated from PHP? - php

I am echoing TSV data and turning it into an excel file via header()
This works fine, but 1 of the columns needs to be an image. How can I embed the image as one of the columns?
Thanks!

Well, put simply, you can't. A TSV is a plain text (text/plain) file, not an 'Excel file'. An excel file is a binary representation of a spreadsheet, and although Excel can read a TSV and present it as tabular data, it's not this advanced binary form which can contain images.
You would have to create a 'proper' Excel xls file using one of the available libraries. I've used PHPExcel in the past and I think it supports adding images.

I don't believe there is anyway to use TSV data, and get Excel to display an image.
However, I have had success in using Spreadsheet_Excel_Writer from PEAR, and it supports inserting images (bitmaps). Also supports nice formatting, etc, etc.

Related

How to export PHP to stylized CSV or XLS without using PEAR and Spreadsheet_Excel_Writer?

I need to export the output of php script to xls or csv file. Output file must be formatted (font size, font weight, borders etc).
I have two options:
content variable as string with html tags (formatted text)
an array with data ready to be formatted
The easiest way I think is to export variable containing html code strict to xls file which I just did. The only problem is when I download xls file and open it, Excel alerts me that opened file format is different that file extension.
Headers in my php script:
header("Content-Type: application/xmls.ms-excel");
header("Content-Disposition: attachment;filename=test.xls");
header("Content-Transfer-Encoding: binary");
Charset is set to UTF-8.
Also I'm able to use array variable with data that I need to put in cells, but:
I don't know how to format cells (fonts, borders etc)
I don't know how to put data in exact cell
I'm not able to use PEAR and Spreadsheet_Excel_Writer
Any further help will be appreciated.
Just dumping HTML out isn't going to work, unfortunately. If you really need the spreadsheet to be formatted, CSV isn't an option since it doesn't store formatting information.
That only leaves XLS as a file format, which is a complicated binary format. Trying to export to it directly is impractical, which is why a few PHP libraries have been written to take care of this for you. I was recently bonked over the head with PHPExcel, which is supposedly not all that difficult to use. My experience with Excel exporters is that they definitely do have a learning curve to them however.
You don't say why you can't use PEAR, but if you're unable to use any external libraries at all, then you're probably out of luck.

Export to CSV with customizing the font color using PHP

In my application i am able to export data to CSV file successfully, But i need some contents of the CSV file to be in "bold" and "color".
So is it possible way to do it? If so please help me out.
If it is a valid option, you can export to Excel format using PHPExcel. This supports font color, weight etc..
The basic CSV file format doesn't have any way to represent such attributes. If the file is to be read by a specific application, maybe it supports a richer format that you can output in?
As others have said, you can not do it with CSV. But if your target program to read the CSV is Excel, you can create a XLS spreadsheet.
But I have had very good success using Spreadsheet_Excel_Writer: http://pear.php.net/manual/en/package.fileformats.spreadsheet-excel-writer.php
Give it a shot.
CSV file is just comma separated values file so it can't be formatted. this is a simple text file with .csv extension. If you want to format then exporting into excel.

How can i take an excel file, parse it contents and store the column's data of excel sheet in my database column?

There is one excel sheet which contains student name, rollno etc. How can i parse the contents of excel file and store that in my sql database?
You are looking for PHPExcel library i believe.
http://phpexcel.codeplex.com/
use this class to read the excel files
http://sourceforge.net/projects/phpexcelreader
If you want to do it directly from the Excel file, rather than create an intermediate CSV using Excel's "Save As" options, then look at libraries such as PHPExcel that can read the xls binary format and make the data accessible to your PHP script.
I'd recommend to you that you save it as csv, it's much more easier and less hacking to upload it to the database
here is simple a sample
http://www.bradino.com/php/csv-upload-to-database/
PHPExcel is one of the best PHP classes i have ever worked with. In addition to parsing and reading files of all Excel variations (including csv) the output to Excel 2007 + is excellent.
Be sure to look at the documentation and examples...they will get you up to speed very quickly

Convert .xls file to xml using php

I need to implement a functionality where a user can upload an Excel file, that is, .xls file and i need to write a function that will read that file and then save the values in a table, compare them to values in another table and then print some kind of billing quotation.
From what I have read, it seems that the best way to do that is to first convert that file to an xml file and then write a function to parse that xml file.
I want to know, is it possible to write a function in php that will convert an xls file to xml. Assuming that the users will save and upload the file as xls.
Thanks.
i often let admin users upload a csv file they exported from an xcel file, its much simpler to parse through using http://php.net/manual/en/function.fgetcsv.php
A quick Google search for "PHP excel parser" turns up PHP Excel Reader, for an open source library to use for reading data from .xls files. There may be others that suit your needs.
I have been successfully using the 'ABC Excel Parser Pro' from Zakkis.
This allows converting from a XLS file directly into a MySQL table.
A few notes on limitations:
column count must match exactly to database columns
Excel 2007 format (xlsx) are not supported

CSV files and multi line text cells

I am generating a simple csv file using php. The file contains some user's personal data.
When I open the generated file in office, the addresses are not displayed in full height. I have to double click on the cell for the address to be shown fully (in full width and height) otherwise I can only see the first word/number of the address.
Also, I have date of births displayed as ######, I have to expand the whole column to see them fully.
This doesn't happen in open office.
Is there any way to force MS Office to show all fields in full? Because otherwise it'll be to confusing for the people who will use (Hey where are all the details!:)
Thanks :)
I don't think you can "format" your sheets with CSV. You will have to produce some other file format that Excel understands. I would suggest XML which is really easy to generate.
Just make a sample sheet with the data you want, save it as XML and you'll see how your file should be generated.
Or you could use some ready-made PHP solution for writing excel files if you can't be bothered with analysing the XML file.
you could try the auto-size columns feature.
This is a UI issue with how Excel works, you can't force Excel or anything else how they handle it.
The quickest work around is to perhaps create an XLS file that runs a macro to retrieve the CVS file and format the cells as needed, but there's nothing you can do inside the CSV to affect what Excel is displaying.

Categories