472,122 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

Can php read .xls files directly?


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

Jul 17 '05 #1
5 20325

James Johnson wrote:
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

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

Jul 17 '05 #2
On 21 Dec 2004 02:07:18 -0800, "Steve" <go********@nastysoft.com> wrote:

James Johnson wrote:
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

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


Exactly what I was looking for. I'll try it later today.
Jul 17 '05 #3
On Tue, 21 Dec 2004 06:12:32 -0500, James Johnson <jj@yaaho.com> wrote:
On 21 Dec 2004 02:07:18 -0800, "Steve" <go********@nastysoft.com> wrote:

James Johnson wrote:
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

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


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


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

Jul 17 '05 #4

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

Jul 17 '05 #5
An noise sounding like James Johnson said:
On Tue, 21 Dec 2004 06:12:32 -0500, James Johnson <jj@yaaho.com> wrote:
On 21 Dec 2004 02:07:18 -0800, "Steve" <go********@nastysoft.com> wrote:

James Johnson wrote:
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
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


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


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?

Check out VBA for excel. That'll tell you what you need to know. Do the stuff
up in excel and you'll see how to loop through row and columns, just open the
vb editor under tools and play around, then port your code to php.

db
--

/(bb|[^b]{2})/
Trees with square roots don't have very natural logs.

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Batista, Facundo | last post: by
2 posts views Thread by Jim Richards | last post: by
8 posts views Thread by sophie_newbie | last post: by
14 posts views Thread by IVETH | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.