By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Can php read .xls files directly?

P: n/a

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
Share this Question
Share on Google+
5 Replies


P: n/a

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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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.