PHPExcel エクセル表示

エクセル表示テスト PHP ファイル
excel_disp_t.php


63Pear Villageに戻る

<?php
//読込xls名前
$xlsnm 'test-2.xlsx';
//シ-トNo $shtno
$shtno 0;
//行中抜き数
$toba 0;
//最大行設定
$maxrow 0;
//最大列設定
$max_column 0;
//数値小数点以下表示数
$shousu 2;
//エクセル内容保存配列
$sheetData = array();
// ライブラリのインクルード 
require_once
'phpoffice/phpexcel/Classes/PHPExcel.php';
// Excelの読み込み
$excel PHPExcel_IOFactory::load($xlsnm);
// 読み込みたいシートを設定する
$sheet $excel -> setActiveSheetIndex($shtno);
// 行の最大値を取得
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;
 
/*    
 getFont()->getName(); //フォント名
 getFont()->getBold();          //太文字
 getFont()->getItalic();        //斜字
 getFont()->getUnderline();     //下線
 getFont()->getStrikethrough(); //打消線
 getFont()->getColor()->getARGB;//フォントカラ-
 getFont()->getSize(); //フォントサイズ

 ここから以下はテスト用①
*/ 
  
echo "背景色 $ cellst=<pre>";
  
print_r($cellst);
  echo 
"</pre>";
    
  echo 
"フォント色 $ font_cl=<pre>";
  
print_r($font_cl);    
    
  echo 
"</pre>太文字 $ font_bold=<pre>";
  
print_r($font_bold);
    
  echo 
"</pre>表示内容 $ tmp=<pre>";
  
print_r($tmp);
  echo 
"</pre>----------------------<br>";
// ここまで①
    
}//for文①end
//ここから以下はテスト用②
  
echo "総合表示内容 $ sheetData=<pre>";
  
print_r($sheetData);
  echo 
"</pre>";
//ここまで②

$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 class="samp_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;
?>