PHPExcel输出数字错误

时间:2013-04-13 20:20:39

标签: php phpexcel

我有这段代码:

$query = "SELECT ciudadh,corporacionh,r1,proxima,ultima FROM usuariox"; 
$headings = array('#', 'Estas son sus fichas hasta la fecha','fecha de ingreso'); 
if ($result = mysql_query($query) or die(mysql_error())) { 
    // Create a new PHPExcel object 
    $objPHPExcel = new PHPExcel(); 
    $objPHPExcel->getActiveSheet()->setTitle('Asuntos Pendientes'); 

    $rowNumber = 2; 
     $rowNumber2 = 2; 
    $col = 'A'; 
    foreach($headings as $heading) { 
       $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading); 
       $col++; 
         } 
    // Loop through the result set 
    $rowNumber = 3; 
    $newId = 1;
    while ($row = mysql_fetch_row($result)) { 
       $col = 'B'; 
        $objPHPExcel->getActiveSheet()->setCellValue("A$rowNumber", $newId++);
       foreach($row as $cell) { 
          $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell); 
          $col++; 
       } 
       $rowNumber++; 
         $rowNumber2++; 
    } 

$styleArray = array(
       'borders' => array(
             'allborders' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN,
                    'color' => array('argb' => 'FFB0c4dE'),
             ),

       ),
);


$styleArray2 = array(
             'fill' => array(
                    'type' => PHPExcel_Style_Fill::FILL_SOLID,
                    'color' => array('argb' => 'FFB0c4dE'),
             ),



);

$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('A1:F1')->applyFromArray($styleArray2);
$objPHPExcel->getActiveSheet()->getSheetView()->setZoomScale(80);
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath('./images/headerexcel2.png');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objDrawing->setCoordinates('A1');
$objDrawing->setOffsetX(30);

    // Freeze pane so that the heading line won't scroll 

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(50);
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(30);
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(50);
$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(15);
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&C&16');
$objPHPExcel->getDefaultStyle()->getFont()->setName('Arial');
$objPHPExcel->getDefaultStyle()->getFont()->setSize(14); 
$objPHPExcel->getActiveSheet()->freezePane('A2'); 

    // Save as an Excel BIFF (xls) file 



   $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 

   header('Content-Type: application/vnd.ms-excel'); 
   header('Content-Disposition: attachment;filename="asuntos-pendientes.xls"'); 
   header('Cache-Control: max-age=0'); 
    $objWriter->save('php://output'); 


   exit(); 


} 
echo 'a problem has occurred... no data retrieved from the database'; 

代码有效,但“r1”字段(mysql)的数字如下:

05001400300120110010000

但如果查看excel文件而不是数字Im得到这个:

enter image description here

2 个答案:

答案 0 :(得分:3)

使用05001400300120110010000setCellValueExplicit()之类的值,默认情况下将其视为字符串值,因此将值存储为前导零(因为它远大于Excel或它不会被转换为浮动并以科学格式显示

答案 1 :(得分:0)

如果单元格类型设置为“常规”,则Excel无法表示此长数,这不是PHPExcel的问题。您必须将单元格类型设置为Number。 你可以尝试:

$PHPExcelObject->getActiveSheet()->getStyle($cell)->getNumberFormat()->setFormatCode('0');