I have some cell on excel file with General format.
The value of this cell is
1,99
10,88
Using phpexcel, When I read the cell i got:
1.99
10.88
How can I get the original value 1,99 ?
I did try
$sheet->getCell("C2")->getValue();
$sheet->getCell("C2")->getFormattedValue()
But both return the
1.99
10.88
PHPExcel cannot possibly know the locale setting that are configured for any users version of MS Excel, and handling of locale-specific formattings (such as decimal commas rather than points) is specific to an instance of MS Excel itself. If I opened that same file in my copy of MS Excel, then I would see a decimal point... i.e. local settings aren't defined in the file, they're purely in the MS Excel GUI.
If you need to format specific to a users locale, then retrieve the raw data using getValue(), and use PHP's Intl NumberFormatter to localise the format.
Related
I have to code a PHP platform for importing an Excel file (quite heavy) to fill an SQL server database.
To do so I used the library PHP-excel-reader (https://code.google.com/archive/p/php-excel-reader/downloads) to be able recover the value of specific cells (eg $ water = $ data-> val (13, 'A');) everything works fine except that PHP-excel-reader is only compatible with .xls not .xlsx, if I go directly convert my files in excel no problem.
But so that the user does not have to do this operation every time I want automated converting .xls .xlsx has.
I tried to use PHPExcel (http://phpexcel.codeplex.com/) but nothing works all the file converted by the libraries do not work on PHP-excel-reader. Would you have a solution?
If there is no solution do you have knowledge of another library that allows like PHP to store the value of an Excel cell in a variable?
I am desperate. Thank you in advance for your help. Sorry for my English I'm french x)
For those interested, I found a solution. It is used the SimpleXLSX library (http://www.spyrozone.net/hacking/2011/05/php-parse-and-retrieve-data-from-xlsx-files.jsp), it will return from to an .xlsx an array of values where it will be to recover each cell value. if you have a problem with date conversion check this ( https://www.ablebits.com/office-addins-blog/2015/07/01/excel-convert-time-decimal/)
which contains contact numbers in "9.77985E+12" format. I have to save the actual contact numbers in mysql database in this format "9779849561769". Excel automatically converts the number into "9.77985E+12" format. I need to convert "9.77985E+12" format into "9779849561769" format using PHP. How can I do this?
I used a third party library but it saves in "9.77985E+12" format.
The data 9.77985E+12 is a float. It's a not usually a good idea to convert from a floated value to a full value when you need the exact value as you can lose precision from the initial.
You should be able to set the formatting of the cells in excel to 'text' instead of 'integer', which will fix the problem in excel.
Otherwise try this in your PHP to convert it
echo number_format($float,10,'.','');
if this problems comes for a javascript user when exporting a csv in
excel format, can use below method. that will solve the problem.
let mobile = +941231234123
let mobileForCsv = mobile + '\t';
console.log(mobileForCsv) // +941231234123
The problem occured when i saved numbers in CSV format from microsoft excel, it loses some degree of precision. This happened while saving the file from my computer only. I tried saving the file from another computer, it worked. I don't know what happened to my excel software.
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 have an excel spreadsheet that contains some formulas to calculate ROI for an upcoming product of a client. Is it possible to convert this into a php script whereby a user would type into some boxes and then submit it working out the same. The excel spreadsheet is here: http://www.solidcamxpress.co.uk/roi.xls
Sure, PHP can do math just like Excel's formulas. However, there is no magic method of converting Excel to a Web page, if that's what you're asking.
#Cameron
There is a software in the market which can convert the spreadsheet to active html/php with ease. You can get the software at the following url:
http://www.spreadsheetconverter.com
Even you can also convert the spreadsheet by yourself as it is simple to convert with scripts. but you can also this software for the conversion =)
You can use PHPExcel to read an existing spreadsheet and extract its data/formulas, but converting those formulas from Excel syntax to PHP is rather non-trivial.
Unless you're doing a generalized Excel->PHP converter, the time/effort required to write such a converter will be far greater than it would take you do analyze the one spreadsheet by hand and code up some PHP stuff to recreate it.
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.)