如何使用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] : "";

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

$objPHPExcel->getActiveSheet()->getStyle('A1')->getNumberFormat()->setFormatCode('0000');

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'
    );