Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Excel sheet connection from php code ....

    Can I get a php code that reads a selected row from an excel sheet and print it in the page that is using this code ... (The excel sheet is in the server)

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    unless the server is a windows server no ( unless you can find some *NIX excel reader and exec() it )

    on win32 look at COM

    alternately save the excel document in csv format (or XML with latest versions) either of which php can get at with fgetcsv or xml respectively
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Apr 2004
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if you ignore the server can you give me a solution?

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    you can't ignore the server .... a COM answer ? (for win32) , probably not , too many headaches though a quick google should bring up some references.

    *NIX , yes if we know the format the file is stored in and an example of such , e.g. a few sample CSV lines or an XML node.

    as for an .xls reader for *NIX I assume such exist though I don;t know of any myself.

    note that the csv/xml answer would be x-platform
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    New Coder
    Join Date
    Nov 2002
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is easy.
    Just make an ODBC connection to the .xls file and run sql to it.

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can you do that? becuase an excel sheet is not a database. Im not ruling out the possibility but I very much doubt it will be possible like that.

  • #7
    New Coder
    Join Date
    Nov 2002
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by missing-score
    can you do that? becuase an excel sheet is not a database. Im not ruling out the possibility but I very much doubt it will be possible like that.
    You sure can.
    The trick is to set whats called a 'Named range' in the xls file first. This becomes the name of the table in the sql statement. You then make the ODBC connection using the xls driver and connect it to the workbook.

    I do this every day at work, but I connect to the odbc data with coldfusion not php so I would need to look up the php functions for that part.

  • #8
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm.. sounds pretty cool... that would be much easier than using COM, just a question though... what type of server are you running at work?

  • #9
    New Coder
    Join Date
    Nov 2002
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    *thinks oh great I better get ready for this*
    I run IIS on Microsoft systems.
    *ducks the flying pies*

  • #10
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I wondered... Im not saying this wouldnt work on Apache/*NIX, but i wonder whether it is this easy becuase you are on a windows server.

  • #11
    New Coder
    Join Date
    Apr 2004
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    This is what I found

    Code:
    <?PHP
    $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);
    $Worksheet->activate;
    $excel_cell = $Worksheet->Range("C4");
    $excel_cell->activate;
    $excel_result = $excel_cell->value;
    print "$excel_result\n";
    $Worksheet = $Workbook->Worksheets($sheet2);
    $Worksheet->activate;
    $excel_cell = $Worksheet->Range("C4");
    $excel_cell->activate;
    $excel_result = $excel_cell->value;
    print "$excel_result\n";
    #To close all instances of excel:
    $Workbook->Close;
    unset($Worksheet);
    unset($Workbook);
    $excel_app->Workbooks->Close();
    $excel_app->Quit();
    unset($excel_app);
    ?>

    I didn't test it yet...
    but I want to ask what is the COM class? does it need a special library or something to use it?

  • #12
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I believe the COM class is built into PHP, as I installed my php distribution and was able to use COM without installing anything further.

  • #13
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    COM functions are bundled with PHP win32 build , e.g built in , but not available in any form on *NIX , in saying that perhaps with a *NIX ODBC driver you can do as Darknight suggests, a good point worth looking at anyway !
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #14
    New Coder
    Join Date
    Apr 2004
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    I tried the code and I ggot the following error:

    Fatal error: Maximum execution time of 30 seconds exceeded in C:\sokkit\site\read_excel.php on line 5

  • #15
    New Coder
    Join Date
    Apr 2004
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    it works now... thankyou all...
    ammmm... but can somebody tell me how can I modify the code so that it can check if the excel sheet is empty...
    Last edited by romram; 04-21-2004 at 09:51 AM.


  •  
    Page 1 of 2 12 LastLast

    LinkBacks (?)

    1. 04-20-2014, 07:44 PM

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •