doulu8341
2014-02-18 16:40
浏览 16
已采纳

如何使用PHPExcel将0001放入excel报告中

I'm having some problems with my code in PHPexcel on generating report. I've got numbers like 0001,0002,0087 in a textbox and when I transfer it to Excel, to make a report the output is 1,2,87. Why is that?

my var to pass value to generate excel report using PHPExcel

$aic = isset($_POST['aic'.$n]) ? $_POST['aic'.$n] : "";

图片转代码服务由CSDN问答提供 功能建议

我在PHPexcel中生成报告时遇到了一些问题。 我在文本框中有 0001 0002 0087 等数字,当我将它传输到Excel时,要生成一个报告,输出是 <代码> 1 ,<代码> 2 ,<代码> 87 。 为什么会这样?

我的var传递值以使用PHPExcel生成excel报告

  $ aic = isset($ _ POST ['aic  '。$ n])?  $ _POST ['aic'。$ n]:“”; 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongwo5110 2014-02-18 16:55
    已采纳

    I think setting your cell format to 4 digits number should do the trick. Try this:

    $objPHPExcel->getActiveSheet()->getStyle('A1')->getNumberFormat()->setFormatCode('0000');
    
    打赏 评论
  • duandie0921 2014-02-18 17:08

    The answer to your problem is given in the documentation ;)

    https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/07-Accessing-Cells.md#setting-a-number-with-leading-zeroes

    There are 2 ways of achieving that:

    Set the data type as string

    // Set cell A8 with a numeric value, but tell PHPExcel it should be treated as a string
    $objPHPExcel->getActiveSheet()->setCellValueExplicit(
        'A8', 
        "01513789642",
        PHPExcel_Cell_DataType::TYPE_STRING
    );
    

    or set a specific number format:

    // Set cell A9 with a numeric value
    $objPHPExcel->getActiveSheet()->setCellValue('A9', 1513789642);
    // Set a number format mask to display the value as 11 digits with leading zeroes
    $objPHPExcel->getActiveSheet()->getStyle('A9')
        ->getNumberFormat()
        ->setFormatCode(
            '00000000000'
        );
    
    打赏 评论

相关推荐 更多相似问题