Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can php read .xls files directly?

30 views
Skip to first unread message

James Johnson

unread,
Dec 21, 2004, 4:29:48 AM12/21/04
to

I'd like to be able to read a microsoft spreadsheet with php. I know that php can read mysql, so I was thinking that maybe it could read excel spreadsheets as well.

Does anyone know if that can be done?

Jim

Steve

unread,
Dec 21, 2004, 5:07:18 AM12/21/04
to


You could do this if you have MS Excel installed on the target machine,
using PHP support for COM.

<?php

$strSheetName = 'Sheet1'
$strCellName = 'A1';

$objXLApp = new COM( "excel.application" ) or die( "unable to start
MSExcel" );
$objXLApp->Workbooks->Open( "c:\\temp\\test.xls" );
$objXLSheet = $objXLApp->ActiveWorkBook->WorkSheets( $strSheetName );
$objXLCell = $objXLSheet->Range( $strCellName );

print "Cell $strCellName in $strSheetName: \"" . $objXLCell->Value() .
"\"\n";

// must do all of these to release resources correctly...

unset( $objXLCell );
unset( $objXLSheet );

$objXLApp->ActiveWorkBook->Close();
$objXLApp->Quit();

unset( $objXLApp );

?>

---
Steve

James Johnson

unread,
Dec 21, 2004, 6:12:32 AM12/21/04
to

Exactly what I was looking for. I'll try it later today.


James Johnson

unread,
Dec 21, 2004, 7:29:59 AM12/21/04
to

I tried it and it works great. Can you point me to any examples that get the number of rows & columns and loops through these?
Or, where can I find documentation on these (php related) functions?

Jim

Steve

unread,
Dec 21, 2004, 7:54:08 AM12/21/04
to

James Johnson wrote:
> Can you point me to any examples that get the number of rows &
columns and loops
> through these? Or, where can I find documentation on these (php
related) functions?

In this example PHP provides only the creation of the object $objXLApp
using the COM support class. See
http://www.php.net/manual/en/class.com.php

Everything after that is passed through to the COM object, so they are
native MS Excel methods and properties. Slightly off-topic: you'll find
documentation for these either from within MS Excel (Tools/Macros/VB
Editor: Help/MS VB Help) or via the usual MS support sites.

---
Steve

Message has been deleted

Tim Roberts

unread,
Dec 23, 2004, 2:09:38 AM12/23/04
to

You have received some good answers to this, but I would just like to point
out a terminology problem with your question.

PHP does not "read" MySQL files at all. No applications do. What PHP
knows how to do is make requests of a MySQL server. It is the MySQL
database server that reads and writes the MySQL files.

The Excel solutions you have been given are the same. PHP, in that case,
is NOT reading .xls files directly, as your subject line asked. Instead,
it is launching a copy of Excel itself, and making requests of it as if it
were an "Excel server".

It seems nitpicky, but it is an important difference.
--
- Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

0 new messages