简体   繁体   中英

Export From PHP to CSV: Amounts with dollar sign being taken as text

I'm trying to export a CSV with php/mysql, when an amount is in dollars the amount is taken as text.

Is there a work around to have these amounts taken as numbers without additional excel formatting from my php program?

Edit:

The following code is where the amounts are given their respective amounts.

    switch($currency)
    {
                            case 'Eur':
                                $symbol=iconv("UTF-8", "cp1252", "€");
                                break;
                            case 'USD':
                                //$symbol=chr(36);//"$";                            
                                $symbol=iconv("UTF-8", "cp1252", "$");
                                break;
                            case 'GBP':
                                //$symbol="£";//chr(163)//;
                                $symbol=iconv("UTF-8", "cp1252", "£");
                                break;
                            case 'EUR':
                                $symbol=iconv("UTF-8", "cp1252", "€");
                                break;
  }        
  return  $symbol."".$number;

The return is fed to the csv.

Obviously Euro and Pounds are working correctly but Dollar isn't I suspect becouse of the absolute reference function it has.

It's excel who converts the value to text. This all depends on the regional settings of windows. There's no way around it. (As far as I know)

For Example: When I set Standard and formats to English (United States), the cell is formatted as currency (cell value = 1 with $ as currency) and when set to Dutch (Netherlands), the cell is formatted as general (cell value = $1 as text).

I wouldn't really advise storing formatting in CSV files. Although it can cause more work it also leads to more flexibility if you store your data raw. You can always send raw data and convert afterwards.

Keep in mind that CSV is not an Excel format. By forcing your CSV to be Excel compliant, you could even be causing issues for someone who is, say, trying to import CSV onto the web, or something along those lines. Formatting that works for one project (or country, as dn Fer has shown in his answer) may not work for another in the same way.

I think a more important question than "how can I do this?" is, "why should I do this?" If you are going to provide an "Excel-friendly" pre-formatted program then you should offer it in addition to an un-formatted raw CSV file. If you are only going to choose one, let the end user handle the formatting themselves. The alternative will inevitably cause you more issues than it is worth.

My suggestion is to let the column headers do the explaining for you. Put the name of the currency and even the symbol at the top of each column head if you want, and put the totals underneath in raw number form. Your end user should have no trouble formatting it themselves in whatever program they choose to open it with, and it will have far less chance of not converting correctly.

Conversely, you can consider creating an XML file. While it will be much more high-powered if you want it to work in multiple file formats, it will allow you to format as far as your imagination/documentation/experimentation/testing gets you. If you choose to go this route here are some resources I have found:

xml - Foreign currency to Excel xslt (SO) Features and limitations of Excel spreadsheet format

In the end, I would strongly recommend going the CSV route first. If you want to make something more robust, you can begin developing something different. XML is probably a better format as modern spreadsheets support some version of XML standard. The trade off is that there is time. But in the end, nothing is impossible - just expensive!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM