I am using phpexcel class to read .xls, .ods and .xlsx files, I need to ignore blank cells, even, if these cells have format.
I have used setReadDataOnly method to get it, and it's working fine with .xls and .ods files but it's not working with .xlxs files.
What exactly do you mean by "not working".
The setReadDataOnly(TRUE) option on read doesn't prevent blank cells from being read if the cell actually exists in the spreadsheet file, it simply means that it doesn't read the formatting for any of the cells... a blank cell is still a blank cell, and unless you create a read filter to prevent certain cells from being read, then they will still be loaded.
This behaviour is identical for xls, ods and for xlsx.
The only facility provided in PHPExcel to "skip" blank cells is within the Cell Iterator.
Related
Ive been trying to wrap my head around using PHPexcel and would like ot know if there is any other simpler solution.
Using PHPexcel i am able to load a standard .xsl file and pick the exact cell i need to import.
$code = $data->val(2,2,0);
echo $code
Now this works perfectly, it will echo the second cell in the second row without any issues. problem is the spreadsheets i need to parse are all in .xlsx format which PHPexcel does not support.
Is there an alternative to PHPexcel which will allow me to simply extract a single cell in a .xlsx file?
Is there any way to format the cells of a csv file with php ? For instance , i have a csv file that i need to format its cell's size before i export it.And another thing is that for numbers with many digits,the excel shows it in another format before i dobule click it and i want to get rid of this too.
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=export.csv');
I know that i have to show what i have already tried,but i couldn't find any information about this,so i have nowhere to begin...
EDIT: The file is csv but i use it as an excel file,and the excel cells are the ones that need to be formated
CSV format consists of nothing more than raw values; there is no way of storing formatting or data type information. Which is why most, if not all, spreadsheet apps let the user manually set column types when importing a CSV file.
If you want to spare your users the hassle of doing that, your best bet would be generating an xls/xlsx rather than CSV. PHPExcel is a popular PHP library for generating Excel files, you might want to give it a try.
Since the accepted answer was written in 2014, the PHPExcel library has been abandoned. It's last version, 1.8.1, was released in 2015. It was officially deprecated in 2017 and permanently archived in 2019. It's direct successor is PhpSpreadsheet.
PHP can parse a CSV string into an array by using str_getcsv (http://php.net/str_getcsv). From there it's simply a matter of editing the values of the array.
As for more advanced features, the CSV format in itself does not support them since it's a pure text format and neither PHP nor the CSV file can decide how Excel should parse the file.
I've had success with exporting Excel XML files (the old, pure text XML files; not the new, zipped ones) and if data presentation is important for you, this might be the way to go.
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.
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.
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.