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.
Related
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.
is there a way that I can format data from a .csv file created with the php function fputcsv into Excel-like tables? Meaning, I don't want my data to be separated by commas, I want them to appear in tables like an Excel spreadsheet or something.
I know I can do that in Excel itself, but is there a way to do that directly without tweaking the file in Excel (like with a php function or something)
fputcsv does exactly what the manual says:
It formats a line as CSV and writes it to a file pointer.
The output can then be imported into Excel or other csv-capable software.
You can generate Excel files directly via php, too, but therefore you have to delve very deep into the Excel file format. If you want to do this, I recommend PHPExcel. This project provides...
...a set of classes for the PHP programming language, which allow you to
write to and read from different file formats, like Excel 2007, PDF,
HTML, ... This project is built around Microsoft's OpenXML standard
and PHP.
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
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 have tabulated data in an Excel spreadsheet (file size will likely never be larger than 1 mb). I want to use PHP to parse the data and insert in to a MySQL database.
Is there any advantage to keeping the file as an .xls/.xlsx and parsing it using a PHP Excel Parsing Library? If so, what are some good libraries to use?
Obviuously, I can save the .xls/.xlsx as a CSV and handle the file that way.
Thanks!
If you are just after the values, I would save it as a CSV. This is much easier to parse programatically, especially if you are trying to do this on a non-windows box.
That being said, there will be information lost in the export to CSV. It will only save the values of the cells - not their formatting information, formulas, etc. If you need to use that information, you're better off doing this straight from Excel.
Here is a PHP Excel Reading library. If you decide to read Excel files directly, this may help get you started.
If your excel files contain strictly data and contain no formulas, scripts, macros and etc., I would say parsing through Excel will only add development overhead, and will potentially slow down processing. It would probably be best to convert the files to CSV in this case.
Also consider that MySQL's 'LOAD DATA INFILE' command can be used to import entire CSV files into a table, this can potentially further uncomplicate matters for you.
when you provide a way for customers to upload excel/csv files, you should consider that
CSV files will only export one sheet
Having multiline cells will make the CSV parsing complicated
You cannot easily detect corrupted/incomplete CSV files
CSV files do not include formatting
Besides from that, importing CSV is a lot easier than importing XLS.
Remember that if you're importing the csv file directly into Mysql, that you may have problems with the date format (as Mysql uses a different date format to Excel). I find it easier to change the date fields in Excel first (to format yyyy-mm-dd) prior to saving as a csv file.
Edit: Although I've not used it myself, others have recommended Navicat as a very good tool for converting Excel spreadsheets or Access data into Mysql databases. May be worth a look.
With Office 2003 there's an XML format called SpreadsheetML which is a bit in-between XML and Excel. I've considered using this format to import/export data to a web site but the format turns out to be a bit complex. Internally, this format turns all references into relative references. (Relative from the current location.) Worse, some cells have an index, thus you might see a row with only two cells, but the second cell might be 6 columns away from the first cell. (In which case Index=5.) Basically, if you want to use the Excel format, you will need to have a good way to calculate the position of each cell and know how to translate the references in the cells in a proper way.
If you're onlyinterested in the data, CSV would be much, much easier to implement. As an in-between solution, you could define an XML schema and add an XML mapping to your spreadsheet to export the data to an XML file. It's more complex than CSV i9mport/export, but also a bit more robust. But the Excel or Excel XML formats themselves are horrible to implement. (Or just a nice challenge, if you're a real XML expert.)