PHPExcel - set cell type before writing a value in it

后端 未结 6 1294
南方客
南方客 2020-12-05 10:56

Do you know how can I set the cell type before writing a value in it? I would like to be able to set types like \"General\", \"Text\" and \"Number\".

Thank you.

相关标签:
6条回答
  • 2020-12-05 11:05

    When the text is a number with leading zeros, then do: (Cuando el texto es un número que empieza por ceros, hacer)

    $objPHPExcel->getActiveSheet()->setCellValueExplicit('A1', $val,PHPExcel_Cell_DataType::TYPE_STRING);
    
    0 讨论(0)
  • 2020-12-05 11:11

    try this

    $currencyFormat = '_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)';
    $textFormat='@';//'General','0.00','@'
    $excel->getActiveSheet()->getStyle('B1')->getNumberFormat()->setFormatCode($currencyFormat);
    $excel->getActiveSheet()->getStyle('C1')->getNumberFormat()->setFormatCode($textFormat);`
    
    0 讨论(0)
  • 2020-12-05 11:12

    For Numbers with leading zeroes and comma separated:

    You can put 'A' to affect the entire column'.

    $objPHPExcel->getActiveSheet()->getStyle('A1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
    

    Then you can write to the cell as you normally would.

    0 讨论(0)
  • 2020-12-05 11:18

    I wanted the Number same as I get from database for example.

    1) 00100.220000

    2) 00123

    3) 0000.0000100

    So I modified the code as below

    $objPHPExcel->getActiveSheet()
        ->setCellValue('A3', '00100.220000');
    $objPHPExcel->getActiveSheet()
        ->getStyle('A3')
        ->getNumberFormat()
        ->setFormatCode('00000.000000');
    
    
    $objPHPExcel->getActiveSheet()
        ->setCellValue('A4', '00123');
    $objPHPExcel->getActiveSheet()
      ->getStyle('A4')
     ->getNumberFormat()
    ->setFormatCode('00000');
    
    
    $objPHPExcel->getActiveSheet()
        ->setCellValue('A5', '0000.0000100');
    $objPHPExcel->getActiveSheet()
      ->getStyle('A5')
     ->getNumberFormat()
    ->setFormatCode('0000.0000000');
    
    0 讨论(0)
  • 2020-12-05 11:19

    Followed Mark's advise and did this to set the default number formatting to text in the whole workbook:

    $objPHPExcel = new PHPExcel(); 
    $objPHPExcel->getDefaultStyle()
        ->getNumberFormat()
        ->setFormatCode(
            PHPExcel_Style_NumberFormat::FORMAT_TEXT
        );
    

    And it works flawlessly. Thank you, Mark Baker.

    0 讨论(0)
  • 2020-12-05 11:20

    The same way as you'd set the type (number format mask) after writing a value to it:

    $objPHPExcel->getActiveSheet()
        ->getStyle('A1')
        ->getNumberFormat()
        ->setFormatCode(
            PHPExcel_Style_NumberFormat::FORMAT_GENERAL
        );
    

    or

    $objPHPExcel->getActiveSheet()
        ->getStyle('A1')
        ->getNumberFormat()
        ->setFormatCode(
            PHPExcel_Style_NumberFormat::FORMAT_TEXT
        );
    

    Though "Number" isn't a valid format mask.

    You can find a list of pre-defined format masks in Classes/PHPExcel/Style/NumberFormat.php or set the value to any valid Excel number format masking string.

    0 讨论(0)
提交回复
热议问题