2012-11-12 67 views
0

我試圖複製整個列或將值複製到另一個column.my腳本可以確定需要複製的列然後更高的值。有什麼建議麼。使用phpexcel複製整個列

+0

閱讀,放在一個數組,並寫下來......?你遇到了什麼困難? –

+0

包括你的代碼以顯示你到目前爲止? – Starx

回答

0

我遇到了同樣的問題,經過幾天的搜索,到目前爲止我所擁有的就是這個主題[copy style and data in PHPExcel]。代碼完美清晰地理解,但正如你一樣,我需要從列到列複製,而不是逐行復制。然後,我想出了複製一列基本上就是「自行將一個單元格複製到另一個單元格索引」。所以這裏是代碼,經過測試,它對我來說很有用。希望這可以幫助。

/** 
* Copy excel column to column 
* @param $sheet:current active sheet 
* @param $srcRow: source row 
* @param $dstRow: destination row 
* @param $height: rows number want to copy 
* @param $width: column number want to copy 
**/ 
private function selfCopyRow(\PHPExcel_Worksheet $sheet, $srcRow, $dstRow, $height, $width) 
{ 
    for ($row = 0; $row < $height; $row++) { 
     for ($col = 0; $col < $width; $col++) { 
      $cell = $sheet->getCellByColumnAndRow($col, $srcRow + $row); 
      $style = $sheet->getStyleByColumnAndRow($col, $srcRow + $row); 
      $dstCell = \PHPExcel_Cell::stringFromColumnIndex(($width + $col)) . (string)($dstRow + $row); 
      $sheet->setCellValue($dstCell, $cell->getValue()); 
      $sheet->duplicateStyle($style, $dstCell); 
     } 

     $h = $sheet->getRowDimension($srcRow + $row)->getRowHeight(); 
     $sheet->getRowDimension($dstRow + $row)->setRowHeight($h); 
    } 

    // EN : Copy format 
    foreach ($sheet->getMergeCells() as $mergeCell) { 
     $mc = explode(":", $mergeCell); 
     $col_s = preg_replace("/[0-9]*/", "", $mc[0]); 
     $col_e = preg_replace("/[0-9]*/", "", $mc[1]); 
     $row_s = ((int)preg_replace("/[A-Z]*/", "", $mc[0])) - $srcRow; 
     $row_e = ((int)preg_replace("/[A-Z]*/", "", $mc[1])) - $srcRow; 

     if (0 <= $row_s && $row_s < $height) { 
      $merge = $col_s . (string)($dstRow + $row_s) . ":" . $col_e . (string)($dstRow + $row_e); 
      $sheet->mergeCells($merge); 
     } 
    } 

}