2012-06-07 256 views
28

我正在使用PHPExcel導出數據以供下載。打開下載的文件時,如果單元格數量很大,則會顯示「#######」而不是數值。我試過setAutoSize()每列,然後調用$sheet->calculateColumnWidths()但它仍然沒有改變。我看到calculateColumnWidths()at here,@Mark Ba​​ker說「calculateColumnWidths()增加了大概5%的值,以確保整列適合」。如果細胞數長度超過5%,似乎DOEN的解決了這個問題如何PHPExcel設置自動列寬度

UPDATE 這是我的功能,自動調整大小列:

function autoFitColumnWidthToContent($sheet, $fromCol, $toCol) { 
     if (empty($toCol)) {//not defined the last column, set it the max one 
      $toCol = $sheet->getColumnDimension($sheet->getHighestColumn())->getColumnIndex(); 
     } 
     for($i = $fromCol; $i <= $toCol; $i++) { 
      $sheet->getColumnDimension($i)->setAutoSize(true); 
     } 
     $sheet->calculateColumnWidths(); 
    } 
+0

您應用哪種自動調整方法計算?大約還是精確?你在使用任何特定的字體嗎?或大膽/斜體樣式?這個數字有多遠?也許增加10%會好於5%。 –

+0

@MarkBaker我已經更新了我的問題的示例代碼。我使用ColumnDimension類的setAutoSize()。 – Davuz

回答

28

一是潛在的問題可能是你正在使用列字母。 PHP的增量操作可以使用列字母,所以如果$ i是'A',那麼$ i ++會給'B',如果$ i是'Z'而不是$ i ++會給'AA';但不能使用< =作爲比較器,因爲'AA'爲< ='Z'時作爲直接比較執行。

而不是

for($i = $fromCol; $i <= $toCol; $i++) { 

使用

$toCol++; 
for($i = $fromCol; $i !== $toCol; $i++) { 

要調用$片狀> calculateColumnWidths後添加5%的保證金()做的事:

for($i = $fromCol; $i !== $toCol; $i++) { 
    $calculatedWidth = $sheet->getColumnDimension($i)->getWidth(); 
    $sheet->getColumnDimension($i)->setWidth((int) $calculatedWidth * 1.05); 
} 
+0

謝謝!我使用'setWidth((int)$ calculatedWidth * 1.4);'來適應16位數字。它工作良好,但我仍然希望有一個函數自動完成計算並設置列寬^^。 – Davuz

+1

@Panique它與我合作。你可以試試這個'setWidth((int)$ calculatedWidth * 1.4);',將1.4改爲一個更大的值,1.6或1.8 ... – Davuz

+0

@我刪除了我的評論,因爲問題出在我身上。我真的很抱歉,給你一個+1。力量可能與你同在! – Sliq

0

的建議無工作對我來說,我做了一個手動計算(相當容易和快速)(示例代碼如下),並完美工作(注意字體/ st yles是默認的,但它很容易調整爲其他字體或樣式)

foreach((array)$data as $sheet_data) 
{ 
    $maxwidth = array(); 
    $objPHPExcel->setActiveSheetIndex($i++); 
    $sheet = $objPHPExcel->getActiveSheet(); 

    if (!empty($sheet_data['title'])) 
     $sheet->setTitle($sheet_data['title']); 

    if (!empty($sheet_data['rows'])) 
    { 
     foreach((array)$sheet_data['rows'] as $row=>$cols) 
     { 
      foreach((array)$cols as $col=>$val) 
      { 
       $p = strpos($col,':'); 
       if (false !== $p) 
       { 
        // range 
        $range = $col; $xy = substr($col, 0, $p); 
        $col = substr($xy,0,-1); 

        // estimate maximum column width by number of characters 
        $w = mb_strlen($val); 
        if (!isset($maxwidth[$col])) $maxwidth[$col] = $w; 
        elseif ($w > $maxwidth[$col]) $maxwidth[$col] = $w; 

        $sheet->mergeCells($range); 
        $sheet->setCellValue($xy, $val); 
        $sheet->getStyle($range) 
          ->getAlignment() 
          ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER) 
          ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER) 
        ; 
       } 
       else 
       { 
        $xy = $col.$row; 

        // estimate maximum column width by number of characters 
        $w = mb_strlen($val); 
        if (!isset($maxwidth[$col])) $maxwidth[$col] = $w; 
        elseif ($w > $maxwidth[$col]) $maxwidth[$col] = $w; 

        $sheet->setCellValue($xy, $val); 
        $sheet->getStyle($xy) 
          ->getAlignment() 
          ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER) 
          ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER) 
        ; 
       } 
      } 
     } 
    } 
    // autosize columns based on calculation + some padding 
    foreach($maxwidth as $col=>$width) 
    { 
     $sheet->getColumnDimension($col)->setAutoSize(false); 
     $sheet->getColumnDimension($col)->setWidth((int)($width * 1.2)); // add padding as well 
    } 
}