<?php
class XWExcel
{
const _sheetbegin = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\n".'<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:B2"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"/></sheetViews><sheetFormatPr defaultRowHeight="13.5"/><sheetData>';
const _sheetend = '</sheetData><phoneticPr fontId="1" type="noConversion"/><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/><pageSetup paperSize="9" orientation="portrait" horizontalDpi="200" verticalDpi="200" r:id="rId1"/></worksheet>';
const _appxml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\n".'<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Application>Microsoft Excel</Application><DocSecurity>0</DocSecurity><ScaleCrop>false</ScaleCrop><HeadingPairs><vt:vector size="2" baseType="variant"><vt:variant><vt:lpstr>工作表</vt:lpstr></vt:variant><vt:variant><vt:i4>1</vt:i4></vt:variant></vt:vector></HeadingPairs><TitlesOfParts><vt:vector size="1" baseType="lpstr"><vt:lpstr>Sheetx</vt:lpstr></vt:vector></TitlesOfParts><Company></Company><LinksUpToDate>false</LinksUpToDate><SharedDoc>false</SharedDoc><HyperlinksChanged>false</HyperlinksChanged><AppVersion>12.0000</AppVersion></Properties>';
const _shareStringbegin = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
const _shareStringend = '</sst>';
const _workbookxml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4505"/><workbookPr filterPrivacy="1" defaultThemeVersion="124226"/><bookViews><workbookView xWindow="0" yWindow="90" windowWidth="19200" windowHeight="11640"/></bookViews><sheets><sheet name="Sheetx" sheetId="1" r:id="rId1"/></sheets><calcPr calcId="124519"/></workbook>';
const _search = array('&','<','>',"\'","\"");
const _target = array('&','<','>',''','"');
private $sheetname;
private $filepath;
private $filename;
private $filetype;
private $curRow;
private $curCol;
private $fp;
private $fpss;
private $indexCache;
private $cellcount;
private $writebuf;
private $writebufss;
public $errMsg;
function __construct()
{
$this->indexCache = array('A');
$this->curRow = 0;
$this->curCol = 0;
$this->cellcount =0;
}
function __destruct()
{
$this->free();
}
#free resource;
private function free()
{
if ( is_resource($this->fp) ){
fclose($this->fp);
}
if ( is_resource($this->fpss) ){
fclose($this->fpss);
}
if ( file_exists($this->filepath.$this->filename) )
{
system("rm -rf ".$this->filepath.$this->filename);
}
unset($indexCache);
unset($writebuf);
unset($writebufss);
}
#create a excel temp object
public function create($filepathfull)
{
$idx = strrpos($filepathfull,"/");
$idx1 = strrpos($filepathfull,".xlsx");
if ($idx===false)
{
$idx = -1;
}
if ($idx1===false)
{
$this->errMsg = "not a valid filetype ,file must be .xlsx";
return false;
}
$this->filepath = substr($filepathfull,0,$idx+1);
$this->filename = substr($filepathfull,$idx+1,$idx1-$idx-1);
return $this->createTempDIR($this->filepath,$this->filename);
}
#create a excel temp object
public function createTempDIR($path,$name)
{
if ( file_exists($path.$name) )
{
$this->errMsg = "file exists.";
return false;
}
$dirname = $path.$name;
system("mkdir -p $dirname;");
system("cp -r ".str_replace('xwexcel.php', 'template', __FILE__)."/* ".$dirname."/");
return true;
}
public function setSheetName($name)
{
$appxml = str_replace("Sheetx",$name,self::_appxml);
$workbookxml = str_replace("Sheetx",$name,self::_workbookxml);
try {
$fp1 = fopen($this->filepath.$this->filename."/docProps/app.xml","w");
if (fp1==NULL)
{
$this->errMsg = "define $name error.";
return false;
}
fprintf(fp1,"%s\n",$appxml);
fclose(fp1);
$fp3 = fopen($this->filepath.$this->filename."/xl/workbook.xml","w");
if (fp3==NULL)
{
$this->errMsg = "define $name error.";
return false;
}
fprintf(fp3,"%s\n",$workbookxml);
fclose(fp3);
}
catch(Exception $e) {
$this->errMsg =$e->getFile().":".$e->getMessage;
}
}
public function save()
{
if (is_resource($this->fp)){
fprintf($this->fp,"%s\n",$this->writebuf);
fprintf($this->fp,"%s\n",self::_sheetend);
fclose($this->fp);
$this->fp = $this->fp = fopen($this->filepath.$this->filename."/xl/worksheets/sheet1.xml" ,"w");
$maxcolname = $this->stringFromColumnIndex($this->curCol).$this->curRow;
fprintf($this->fp,"%s\n",str_replace("A1:B2","A1:".$maxcolname,self::_sheetbegin));
fclose($this->fp);
system("cat ".$this->filepath.$this->filename."/xl/worksheets/sheet1.xml.mid >>".$this->filepath.$this->filename."/xl/worksheets/sheet1.xml");
system("rm -f ".$this->filepath.$this->filename."/xl/worksheets/sheet1.xml.mid ");
}
if (is_resource($this->fpss)){
fprintf($this->fpss,"%s\n",$this->writebufss);
fprintf($this->fpss,"%s\n",self::_shareStringend);
fclose($this->fpss);
$this->fpss = fopen($this->filepath.$this->filename."/xl/sharedStrings.xml" ,"w");
fprintf($this->fpss,"%s\n",self::_shareStringbegin);
$headcount = '<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="'.$this->cellcount.'" uniqueCount="'.$this->cellcount.'">';
fprintf($this->fpss,"%s",$headcount);
fclose($this->fpss);
system("cat ".$this->filepath.$this->filename."/xl/sharedStrings.xml.mid >>".$this->filepath.$this->filename."/xl/sharedStrings.xml");
system("rm -f ".$this->filepath.$this->filename."/xl/sharedStrings.xml.mid ");
}
system("cd ".$this->filepath.$this->filename.";/usr/bin/zip -r xxx.zip * >/dev/null");
system("mv ".$this->filepath.$this->filename."/xxx.zip ".$this->filepath.$this->filename.".xlsx ");
system("rm -rf ".$this->filepath.$this->filename);
}
public function addRow($inarray)
{
if (is_array($inarray))
{
$this->writebuf .= '<row r="'.($this->curRow+1).'" spans="1:2">';
$i = 0;
foreach ($inarray as $value)
{
$colname = $this->stringFromColumnIndex($i).($this->curRow+1);
$i++;
$this->writebuf .= '<c r="'.$colname.'" t="s"><v>'.$this->cellcount.'</v></c>';
$this->writebufss .= '<si><t>'.$this->formatstr($value).'</t><phoneticPr fontId="1" type="noConversion"/></si>';
$this->cellcount++;
}
$this->writebuf .= '</row>';
if ($i>$this->curCol)
{
$this->curCol = $i;
}
}
$this->curRow++;
$this->writeRow();
}
评论0
最新资源