Jump to content


PHP and Excel

  • Please log in to reply
4 replies to this topic

#1 firedrop84

  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 27 March 2006 - 08:02 AM

I have an existing excel spreadsheet that has all my calculations and formulas on it for report generation. Was wondering if it was possible to export the database query results directly into the spreadsheet (and into the cells required) so that my calculations can be done for my report generation. And then enable this file to be downloaded by the user.

Any help is much appreciated as I’m only just getting started.

Have a good day.

#2 Toni_montana

  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 27 March 2006 - 11:51 AM

there are many scripts around that are able to export mysql to excel. You might wanna search google for it. It's easyer than writing it yourself.

good luck


#3 firedrop84

  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 28 March 2006 - 12:52 PM

thank you for your replay I have done already some search on Google and I laready found scripts and some software that converts. I already know how to convert to Excel sheet. but the problem that I am facing is that I already have an excel sheet that has forumlas. The problem that i have how can i enter some data into an existing excel sheet.

I really approciate your help


#4 ober

  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 28 March 2006 - 01:51 PM

You're going to have to use the COM object: [a href=\"http://us2.php.net/manual/en/class.com.php\" target=\"_blank\"]http://us2.php.net/manual/en/class.com.php[/a]

And keep in mind that any Excel file you edit MUST be on the server, not on the client machine!

Code from that page:
$filename = "c:/spreadhseet/test.xls";
$sheet1 = 1;
$sheet2 = "sheet2";
$excel_app = new COM("Excel.application") or Die ("Did not connect");
print "Application name: {$excel_app->Application->value}\n"; 
print "Loaded version: {$excel_app->Application->version}\n";
$Workbook = $excel_app->Workbooks->Open("$filename") or Die("Did not open $filename $Workbook");
$Worksheet = $Workbook->Worksheets($sheet1);
$excel_cell = $Worksheet->Range("C4");
$excel_result = $excel_cell->value;
print "$excel_result\n";
$Worksheet = $Workbook->Worksheets($sheet2);
$excel_cell = $Worksheet->Range("C4");
$excel_result = $excel_cell->value;
print "$excel_result\n";
#To close all instances of excel:

And here's another function that I have, but have never used:

   function WriteExcel($strPath,$astrSheetName,$astrSQL){
/* This function takes a file save path, and array of sheet names and a corresponding array */
/* of SQL queries for each sheet and created a multi-worksheet excel spreadsheet*/
       $dbs=new clsDB;
       $exapp = new COM("Excel.Application") or Die ("Did not connect");
           $exapp->Application->Visible = 1; 
       for ($int=0;$int<$intSheetCount;$int++){
               $strOut="";//initialize the output string
               for ($int2=0;$int2<$intCols;$int2++){//we start at 1 because don't want to output the table's index
                         $cell->value = $strOut; 
                   }//end of colcount for loop
           }//end while loop
       }//end sheetcount for loop
       if (file_exists($strPath)) {unlink($strPath);}
   }//function WriteExcel 

Info: PHP Manual

#5 firedrop84

  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 09 April 2006 - 10:06 AM

Hello ..

I have been trying to use the code that you have adviced me to you use I mean ober code. It shows me some errors. the error is

Application name: Microsoft Excel Loaded version: 11.0
Fatal error: Uncaught exception 'com_exception' with message 'Source: Microsoft Office Excel
Description: 'test.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct. If you are trying to open the file from your list of most recently used files on the File menu, make sure that the file has not been renamed, moved, or deleted.' in C:\Documents and Settings\12966233\Desktop\xampplite\htdocs\test1.php:16 Stack trace: #0 C:\Documents and Settings\12966233\Desktop\xampplite\htdocs\test1.php(16): variant->Open('test.xls') #1 {main} thrown in C:\Documents and Settings\12966233\Desktop\xampplite\htdocs\test1.php on line 16

and line 16 is $Workbook = $excel_app->Workbooks->Open("$filename") or Die("Did not open $filename $Workbook");

so can anyone help me with this error as I really dont know why it is not working.


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users