2017-05-06 59 views
1

我正在使用phpexcel從mysql數據庫生成可下載的excel文件。在那裏,有一個有18位數字的字段,在mysql中它被定義爲Bigint。該編號在代碼中定義爲超鏈接。現在,出現以下問題 -不希望的生成的PHPexcel中的數字舍入

數字超鏈接的最後4位數字顯示爲0000,但點擊數字超鏈接時,它正確打開。例如860814069447613475顯示爲在Excel中產生

860814069447610000這裏是我的代碼 -

$objPHPExcel->getActiveSheet() 
    ->getStyle('A'.(string)$n) 
    ->getNumberFormat() 
    ->setFormatCode(
     PHPExcel_Style_NumberFormat::FORMAT_NUMBER 
    ); 

$n = 2; 

while ($row = mysqli_fetch_array($result)){ 
    $objPHPExcel->getActiveSheet()->setCellValue('A'.(string)$n, 
$row['t_id']); 
    $objPHPExcel->getActiveSheet()->setCellValue('B'.(string)$n, 
$row['t_text']); 
    $objPHPExcel->getActiveSheet()->setCellValue('C'.(string)$n, 
$row['user_name']); 
    $objPHPExcel->getActiveSheet()->setCellValue('D'.(string)$n, 
$row['description']); 
    $objPHPExcel->getActiveSheet()->setCellValue('E'.(string)$n 
,$row['time']); 
    $objPHPExcel->getActiveSheet()->setCellValue('F'.(string)$n, 
$row['place']); 
    $objPHPExcel->getActiveSheet()->getStyle("A$n:F$n")->getAlignment()- 
>setWrapText(true); 


$objPHPExcel->getActiveSheet() 
    ->getCell('A'.(string)$n) 
    ->getHyperlink() 
    ->setUrl('http://t.com/'.$row['user_name'].'/status/' . $row['t_id']); 

    // Config 
$link_style_array = [ 
    'font' => [ 
    'color' => ['rgb' => '0000FF'], 
    'underline' => 'single' 
    ] 
]; 

// Set it! 
$objPHPExcel->getActiveSheet()->getStyle('A'.(string)$n)->applyFromArray($link_style_array); 
    $n++; 
} 
+1

Excel的精度爲15位。如果您需要以更高精度顯示數字,則需要將它們作爲文本存儲在Excel中(並且在輸入數字字符串之前,單元格必須格式化爲文本) –

回答

2

找到的代碼爲我工作 -

setCellValueExplicit('A'.(string)$n, $row['t_id'], 
PHPExcel_Cell_DataType::TYPE_STRING);