PHPExcel エクセル表示

PHPExcel エクセル表示 Function PHP ファイル
excel_disp_f.php


63Pear Villageに戻る

<?php
/*
 読込xls名前
   $xlsnm = 'test-2.xlsx';
 シ-トNo $shtno
   $shtno = 2;
 行中抜き数
   $toba = 0;
 最大行設定
   $maxrow = 0;
 最大列設定
   $max_column = 0;
 数値小数点以下表示数
   $shousu = 2; 
 excel_disp('test-2.xlsx',2,0,0,2);
*/
function excel_disp
 
($xlsnm$shtno$toba$maxrow,
  
$max_column,$shousu)
{
/* ライブラリのインクルード 
 require_once
 'phpoffice/phpexcel/Classes/
  PHPExcel.php';
*/    
require_once 'autoload.php';
// Excelの読み込み
$excel PHPExcel_IOFactory::load($xlsnm);
// 読み込みたいシートを設定する
$sheet $excel ->
 
setActiveSheetIndex($shtno);
$sheetData = array();
// 行の最大値を取得
if($maxrow == 0){
  
$maxrow $sheet -> getHighestRow();
  }
for( 
$i 1$i <= $maxrow$i++){ //for文①
  
$tmp = array();
//列の最大値を取得
  
if($max_column == 0){
    
$max_column =
     
PHPExcel_Cell::columnIndexFromString
     
($sheet -> getHighestColumn());
  }
//A、B、Cの文字列形式で帰ってくるので数字に変換する    
  
for( $j 0$j $max_column$j++){
//for文②
//列のインデックスと、行のインデックスからセルの値を取得
    
$jlist $sheet->
     
getStyleByColumnAndRow($j,$i);
         
    
$cellst[] = $jlist ->
     
getFill()->getStartColor()->getargb();

    
$font_cl[] = $jlist ->
     
getFont() ->getColor()->getargb();
         
    
$font_bold[] = $jlist ->
     
getFont() ->getBold();//太文字

    
$tmp0 $sheet->
     
getCellByColumnAndRow($j,$i)->
//   getValue(); 文字列 or 計算式
     
getCalculatedValue();

    if(
is_numeric($tmp0)){
      
$tmp0 $tmp0 1.00;
      
$tmp0 number_format($tmp0,$shousu);
    }
    
$tmp[] = $tmp0;
    }
    if(
$i == 1){
      
$i $i $toba;
  } 
//for文②end
  
$sheetData[] = $tmp;
}
//for文①end

$data '<table class="samp_tb">';
$c 0;
foreach(
$sheetData as $table){//foreach①
  
$data .= '<tr>';
  foreach(
$table as $td){//foreach②
//substr-文字列 $cellst[$c] の 頭の2文字 FF を削除。
       
$tdcolor substr($cellst[$c], 2);
    if(
$tdcolor == '000000'){
      
$tdcolor 'FFFFFF';
    }
//substr-文字列 $font_cl[$c] の 頭の2文字 FF を削除。        
    
$ftcolor substr($font_cl[$c], 2);
//太字処理
    
$font_b ';">';
    if(
$font_bold[$c] == 1){
      
$font_b ';font-weight:bold;">';    
    }
    
$data .=
     
'<td class="samp_td" style="color:#'
      
.$ftcolor
      
.';background-color:#'
      
.$tdcolor
      
.$font_b.$td
            
.'</td>';
    
$c++;
  }
//foreach②end
/*
 <td style =
 "color:#FF0000           //$ftcolor
 ;background-color:#DEEAF6//$tdcolor
 ;font-weight:bold;">     //$font_b-太
 ;">                      //$font_b-細
 [0][0] A1 青赤細         //$td
 </td>
*/
  
$data .= '</tr>';
}
//foreach①end
$data .= '</table>';
echo 
$data;
}
//function end
?>