php excel读写,用PHP读取Excel文件

// Here is the simple code using COM object in PHP

class Excel_ReadWrite{

private $XLSHandle;

private $WrkBksHandle;

private $xlBook;

function __construct() {

$this->XLSHandle = new COM("excel.application") or die("ERROR: Unable to instantaniate COM!\r\n");

}

function __destruct(){

//if already existing file is opened

if($this->WrkBksHandle != null)

{

$this->WrkBksHandle->Close(True);

unset($this->WrkBksHandle);

$this->XLSHandle->Workbooks->Close();

}

//if created new xls file

if($this->xlBook != null)

{

$this->xlBook->Close(True);

unset($this->xlBook);

}

//Quit Excel Application

$this->XLSHandle->Quit();

unset($this->XLSHandle);

}

public function OpenFile($FilePath)

{

$this->WrkBksHandle = $this->XLSHandle->Workbooks->Open($FilePath);

}

public function ReadData($RowNo, $ClmNo)

{

$Value = $this->XLSHandle->ActiveSheet->Cells($RowNo, $ClmNo)->Value;

return $Value;

}

public function SaveOpenedFile()

{

$this->WrkBksHandle->Save();

}

/***********************************************************************************

* Function Name:- WriteToXlsFile() will write data based on row and column numbers

* @Param:- $CellData- cell data

* @Param:- $RowNumber- xlsx file row number

* @Param:- $ColumnNumber- xlsx file column numbers

************************************************************************************/

function WriteToXlsFile($CellData, $RowNumber, $ColumnNumber)

{

try{

$this->XLSHandle->ActiveSheet->Cells($RowNumber,$ColumnNumber)->Value = $CellData;

}

catch(Exception $e){

throw new Exception("Error:- Unable to write data to xlsx sheet");

}

}

/****************************************************************************************

* Function Name:- CreateXlsFileWithClmName() will initialize xls file with column Names

* @Param:- $XlsColumnNames- Array of columns data

* @Param:- $XlsColumnWidth- Array of columns width

*******************************************************************************************/

function CreateXlsFileWithClmNameAndWidth($WorkSheetName = "Raman", $XlsColumnNames = null, $XlsColumnWidth = null)

{

//Hide MS Excel application window

$this->XLSHandle->Visible = 0;

//Create new document

$this->xlBook = $this->XLSHandle->Workbooks->Add();

//Create Sheet 1

$this->xlBook->Worksheets(1)->Name = $WorkSheetName;

$this->xlBook->Worksheets(1)->Select;

if($XlsColumnWidth != null)

{

//$XlsColumnWidth = array("A1"=>15,"B1"=>20);

foreach($XlsColumnWidth as $Clm=>$Width)

{

//Set Columns Width

$this->XLSHandle->ActiveSheet->Range($Clm.":".$Clm)->ColumnWidth = $Width;

}

}

if($XlsColumnNames != null)

{

//$XlsColumnNames = array("FirstColumnName"=>1, "SecondColumnName"=>2);

foreach($XlsColumnNames as $ClmName=>$ClmNumber)

{

// Cells(Row,Column)

$this->XLSHandle->ActiveSheet->Cells(1,$ClmNumber)->Value = $ClmName;

$this->XLSHandle->ActiveSheet->Cells(1,$ClmNumber)->Font->Bold = True;

$this->XLSHandle->ActiveSheet->Cells(1,$ClmNumber)->Interior->ColorIndex = "15";

}

}

}

//56 is for xls 8

public function SaveCreatedFile($FileName, $FileFormat = 56)

{

$this->xlBook->SaveAs($FileName, $FileFormat);

}

public function MakeFileVisible()

{

//Hide MS Excel application window`enter code here`

$this->XLSHandle->Visible = 1;

}

}//end of EXCEL class

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值