首页/Home PHP Common Programming 从Excel中读取内容(Read Excel contents)

从Excel中读取内容(Read Excel contents)

PrintE-mail
Monday, 21 January 2008 17:00  

本文用来从Excel中读取或修改一个单元格的内容.
您可以根据您的需要进行修改.

This code is used to read or change a cell's content.
You can make it fit to your application as you need.

<?php
$fileName
= 'example.xls';
$sheet1 = 'Sheet1';

// If there is more than one sheet:
//$sheet2 = 'Sheet2';

$excelApp = new COM("Excel.application")
or Die (
'Can not connect');
$workBook = $excelApp->Workbooks->Open("$fileName")
or Die(
"Could not open $fileName $workBook");
$workSheet = $workBook->Worksheets($sheet1);
$workSheet->activate;
$excelCell = $workSheet->Cells(4,4);
$excelCell->activate;
$excelResult = $excelCell->value;

echo
'result:', $excelResult, ' <br />';


//$workSheet = $workBook->Worksheets($sheet2);
//$workSheet->activate;
//$excelCell = $workSheet->Range("C4");
//$excelCell->activate;
//$excelCell->value = 'cccccc';
//$excelResult = $excelCell->value;
//echo $excelResult, '<br />';

// To close all instances of excel:
$workBook->SaveAs($fileName);
$workBook->Close;
$excelApp->Workbooks->Close();
$excelApp->Quit();
$excelApp = null;

unset(
$workSheet);
unset(
$workBook);
?>

*注: 因为.xls文件是Microsoft的产品, 所以此代码仅限于在Windows服务器上使用.
在Linux服务器上, 您就可以考虑用.csv文件了, 也用不着com组件.

 

回复

留个脚印儿吧.


回复