Ich habe das nur aus Langeweile geschrieben. Vergessen Sie nicht, mir eine Kopie zu schicken.
PS: Der Test ist nicht effizient, haha~~~
[email protected] Inhalte in die Zwischenablage kopieren
PHP-Code:
<?php
/**
*COM-basierte Excel-Operationsklasse (PHP5.x)
*PHPer:TTR
*Datum:[24.05.2007]
*Version:1.0.0
*Blog:[url]http://www.Gx3.cn[/url] [url]http://Gx3.cn[/url]
*QQ:252319874
*/
Klasse Excel
{
static $instance=null;
privat $excel=null;
private $workbook=null;
privat $workbookadd=null;
privat $worksheet=null;
privat $worksheetadd=null;
privat $sheetnum=1;
private $cells=array();
private $fields=array();
private $maxrows;
private $maxcols;
private $filename;
//Konstruktor
private Funktion Excel()
{
$this->excel = new COM("Excel.Application") or die("Did Not Connect");
}
//Klasseneintrag
öffentliche statische Funktion getInstance()
{
if(null == self::$instance)
{
self::$instance = new Excel();
}
return self::$instance;
}
//Dateiadresse festlegen
öffentliche Funktion setFile($filename)
{
return $this->filename=$filename;
}
//Datei öffnen
öffentliche Funktion Open()
{
$this->workbook=$this->excel->WorkBooks->Open($this->filename);
}
//Blatt festlegen
öffentliche Funktion setSheet($num=1)
{
if($num>0)
{
$this->sheetnum=$num;
$this->worksheet=$this->excel->WorkSheets[$this->sheetnum];
$this->maxcols=$this->maxCols();
$this->maxrows=$this->maxRows();
$this->getCells();
}
}
//Alle Werte aus der Tabelle abrufen und in das Array schreiben
private Funktion 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;
}
}
return $this->cells;
}
// Tabelleninhaltsarray zurückgeben
öffentliche Funktion getAllData()
{
return $this->cells;
}
//Den angegebenen Zelleninhalt zurückgeben
öffentliche Funktion Cell($row,$col)
{
return $this->worksheet->Cells($row,$col)->Value;
}
//Das Array der Tabellenfeldnamen abrufen
öffentliche Funktion getFields()
{
for($i=1;$i<$this->maxcols;$i++)
{
$this->fields[]=$this->worksheet->Cells(1,$i)->value;
}
return $this->fields;
}
//Ändere den Inhalt der angegebenen Zelle
öffentliche Funktion editCell($row,$col,$value)
{
if($this->workbook==null || $this->worksheet==null)
{
echo „Fehler: Keine Verbindung hergestellt!“;
}anders{
$this->worksheet->Cells($row,$col)->Value=$value;
$this->workbook->Save();
}
}
//Eine Datenzeile ändern
öffentliche Funktion editOneRow($row,$arr)
{
if($this->workbook==null || $this->worksheet==null || $row>=2)
{
echo „Fehler: Keine Verbindung hergestellt!“;
}anders{
if(count($arr)==$this->maxcols-1)
{
$i=1;
foreach($arr als $val)
{
$this->worksheet->Cells($row,$i)->Value=$val;
$i++;
}
$this->workbook->Save();
}
}
}
//Erhalte die Gesamtzahl der Spalten
private Funktion maxCols()
{
$i=1;
while(true)
{
if(0==$this->worksheet->Cells(1,$i))
{
return $i;
brechen;
}
$i++;
}
}
//Erhalte die Gesamtzahl der Zeilen
private Funktion maxRows()
{
$i=1;
while(true)
{
if(0==$this->worksheet->Cells($i,1))
{
return $i;
brechen;
}
$i++;
}
}
//Lesen Sie die angegebenen Zeilendaten
öffentliche Funktion getOneRow($row=2)
{
if($row>=2)
{
for($i=1;$i<$this->maxcols;$i++)
{
$arr[]=$this->worksheet->Cells($row,$i)->Value;
}
return $arr;
}
}
//Schließe das Objekt
öffentliche Funktion Close()
{
$this->excel->WorkBooks->Close();
$this->excel=null;
$this->workbook=null;
$this->worksheet=null;
self::$instance=null;
}
};
/*
$excel = new 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->Close();
?>