PHPでExcel †
書式をコピー †
$excel->duplicateStyle
一括で書式をコピー(行) †
/** * 一括で書式をコピー(行) * コピー元行の範囲を指定し、コピー先の範囲へ書式をコピーする * <code> * $excel->duplicate_style_row("A2:E2", "A2:E50"); * </code> * @param string $from_cells コピー元セル範囲 * @param string $to_cells コピー先セル範囲 */ public function duplicate_style_row($from_cells, $to_cells) { // ---------------------------------------- // コピー元のセルの範囲を数値で取得 // ---------------------------------------- $from_area = PHPExcel_Cell::rangeBoundaries($from_cells); // ex) array( array(2, "2"),array(53, "3")) $from_start_col = (int)$from_area[0][0]; // 開始列数値 ex) B --- 2 $from_start_row = (int)$from_area[0][1]; // 開始行数値 $from_end_col = (int)$from_area[1][0]; // 終了列数値 ex) BA --- 53 $from_end_row = (int)$from_area[1][1]; // 終了行数値 // ---------------------------------------- // 範囲の行数、列数を取得 // ---------------------------------------- $area_dmsn = PHPExcel_Cell::rangeDimension($from_cells); // ex) array( 53, 1 ) $area_cols = $area_dmsn[0]; $area_rows = $area_dmsn[1]; $to = PHPExcel_Cell::rangeBoundaries($to_cells); // ex) array( array(2, "2"),array(53, "3")) $to_start_col = (int)$to[0][0]; // 開始列数値 ex) B --- 2 $to_start_row = (int)$to[0][1]; // 開始行数値 $to_end_col = (int)$to[1][0]; // 終了列数値 ex) BA --- 53 $to_end_row = (int)$to[1][1]; // 終了行数値 //スタイル取得 $from_styles = array(); for( $j = 0; $j <= $to_end_col - $to_start_col; $j++ ) { $from_col = PHPExcel_Cell::stringFromColumnIndex($from_start_col + $j - 1) . $from_start_row; $from_styles[] = $this->_active_sheet->getStyle($from_col); } for( $i = 0; $i <= $to_end_col - $to_start_col; $i++ ) { $to_start_col_str = PHPExcel_Cell::stringFromColumnIndex($to_start_col + $i - 1) . $to_start_row; $to_end_col_str = PHPExcel_Cell::stringFromColumnIndex($to_start_col + $i - 1) . $to_end_row ; $targets[] = $to_start_col_str.":".$to_end_col_str; } foreach($from_styles as $key=>$from_style) { $this->_active_sheet->duplicateStyle($from_style, $targets[$key]); } }