Search code examples
phpphpexcelphpoffice

PHPOffice/PHPExcel PHPExcel_Style_NumberFormat FORMAT_CURRENCY_USD_SIMPLE correct way to use this.


I am trying to use FORMAT_CURRENCY_USD_SIMPLE .

I am able to use dates, integer and general succefully. But i am having problem with this.

My Cell content can be like this.

enter image description here

Here you can see, I am using "$75.00" is my cell content. I am putting $ as my cell content as I get this data from my query.

Is CONTENT of CELL value we put in setCellValueByColumnAndRow should be without "$" or with "$". I have tried it with "$".

DOCUMENTS

I have not tried without "$".

So what will be the correct CONTENT and what will be the correct format code.

I have used "FORMAT_CURRENCY_USD_SIMPLE" , "FORMAT_CURRENCY_USD" , '"$"#,##0.00_-' ( directly ) , '$#,##0_-' ( right now ).

My all currencny number will be like $1,356.25. If you follow this structure.

Which format code should i use for content value like this.

My code , It works with date and numbers.

$areaOfExcel = $column_alphabet.$row_start_data.":".$column_alphabet.$excel_current_row_index ;
$this->excel_active_sheet->getStyle( $areaOfExcel )
                ->getNumberFormat()
                ->setFormatCode( $dataTypeFromAdoDb );

My main concern is what should be correct format code or what should be correct content. to use $ sign and format them properly.


Solution

  • The content value in the cell should be a simple floating point number, with no currency code, no thousands separator, etc..... exactly as it should be in MS Excel itself if you want to use a currency format number mask.

    $value = 1234.56; // float value
    
    $objPHPExcel->getActiveSheet()
        ->setCellValue('A1', $value);
    $objPHPExcel->getActiveSheet()
        ->getStyle('A1')
        ->getNumberFormat()
        ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);