退屈でこれを書いただけです。拡張機能が気に入ったら、忘れずにコピーを送ってください。笑、ありがとう。
テストは効率的ではありません。guudj
@yahoo.com.cnをコピーしてください。コンテンツをクリップボードにコピーする
PHP コード:
<?php
/**
※COMベースのExcel操作クラス(PHP5.x)
*PHPer:TTR
*日付:[2007-05-24]
※Ver:1.0.0
*ブログ:[url]http://www.Gx3.cn[/url] [url]http://Gx3.cn[/url]
*QQ:252319874
*/
クラスエクセル
{
静的 $instance=null;
プライベート $excel=null;
プライベート $workbook=null;
プライベート $workbookadd=null;
プライベート $worksheet=null;
プライベート $worksheetadd=null;
プライベート $sheetnum=1;
プライベート $cells=array();
プライベート $fields=array();
プライベート $maxrows;
プライベート $maxcols;
private $filename
//コンストラクター
プライベート関数 Excel()
{
$this->excel = new COM("Excel.Application") または die("Did Not Connect");
}
//クラスエントリー
パブリック静的関数 getInstance()
{
if(null == self::$instance)
{
self::$instance = 新しい Excel();
}
self::$instance を返します。
}
//ファイルアドレスを設定
パブリック関数 setFile($filename)
{
$this->filename=$filename を返します。
}
//ファイルを開く
パブリック関数 Open()
{
$this->workbook=$this->excel->WorkBooks->Open($this->ファイル名);
}
//シートを設定
パブリック関数 setSheet($num=1)
{
if($num>0)
{
$this->sheetnum=$num;
$this->worksheet=$this->excel->ワークシート[$this->sheetnum];
$this->maxcols=$this->maxCols();
$this->maxrows=$this->maxRows();
$this->getCells();
}
}
//テーブルからすべての値を取得し、配列に書き込みます
プライベート関数 getCells()
{
for($i=1;$i<$this->maxcols;$i++)
{
for($j=2;$j<$this->maxrows;$j++)
{
$this->cells[$this->worksheet->Cells(1,$i)->value][]=(string)$this->worksheet->Cells($j,$i)->value;
}
}
$this->cells を返します。
}
//テーブルコンテンツの配列を返す
パブリック関数 getAllData()
{
$this->cells を返します。
}
//指定されたセルの内容を返します
パブリック関数 Cell($row,$col)
{
$this->worksheet->Cells($row,$col)->Valueを返します。
}
//テーブルのフィールド名の配列を取得します
パブリック関数 getFields()
{
for($i=1;$i<$this->maxcols;$i++)
{
$this->fields[]=$this->ワークシート->Cells(1,$i)->値;
}
$this->fields を返します。
}
//指定されたセルの内容を変更します
パブリック関数 editCell($row,$col,$value)
{
if($this->workbook==null || $this->worksheet==null)
{
echo "エラー: 接続できませんでした!";
}それ以外{
$this->ワークシート->セル($row,$col)->値=$value;
$this->workbook->Save();
}
}
//データ行を変更します
パブリック関数 editOneRow($row,$arr)
{
if($this->workbook==null || $this->worksheet==null || $row>=2)
{
echo "エラー: 接続できませんでした!";
}それ以外{
if(count($arr)==$this->maxcols-1)
{
$i=1;
foreach($arr として $val)
{
$this->ワークシート->セル($row,$i)->値=$val;
$i++;
}
$this->workbook->Save();
}
}
}
//列の総数を取得する
プライベート関数 maxCols()
{
$i=1;
その間(真)
{
if(0==$this->ワークシート->セル(1,$i))
{
$i を返します。
壊す;
}
$i++;
}
}
//総行数を取得
プライベート関数 maxRows()
{
$i=1;
その間(真)
{
if(0==$this->ワークシート->セル($i,1))
{
$i を返します。
壊す;
}
$i++;
}
}
//指定された行データを読み込みます
パブリック関数 getOneRow($row=2)
{
if($行>=2)
{
for($i=1;$i<$this->maxcols;$i++)
{
$arr[]=$this->ワークシート->セル($row,$i)->値;
}
$arr を返します。
}
}
//オブジェクトを閉じる
パブリック関数Close()
{
$this->excel->WorkBooks->Close();
$this->excel=null;
$this->workbook=null;
$this->worksheet=null;
self::$instance=null;
}
};
/*
$excel = 新しい COM("Excel.Application");
$workbook = $excel->WorkBooks->Open('D:\Apache2\htdocs\wwwroot\MyExcel.xls');
$worksheet = $excel->WorkSheets(1);
echo $worksheet->Cells(2,6)->Value;
$excel->WorkBooks->Close();
*/
$excel=Excel::getInstance();
$excel->setFile("D:\Apache2\htdocs\wwwroot\MyExcel.xls");
$excel->Open();
$excel->setSheet();
for($i=1;$i<16;$i++ )
{
$arr[]=$i;
}
//$excel->editOneRow(2,$arr);
print_r($excel->getAllData());
$excel->閉じる()
?>