Jump to content

add up values in a column in excel?


muppet77

Recommended Posts

I am trying to find a loop type script that starts at a particular row in a particular column in an excel file and keeps adding values in each row until the last value is reached.

 

I have searched and am pretty new to loops. can anyone point me in the right direction please?

Link to comment
Share on other sites

ok, i borrowed and adapted some code

 

<?php
    // include class file
require_once 'excel_reader2.php';
    
    // This could be post or get data from a form if you need to make it look for something other than id 65.
    $search_var = 65;
    // initialize reader object
    
    // Use the construct to read the file FALSE means php will not get the cell format data such as font color etc..
    $excel = new Spreadsheet_Excel_Reader('testdata.xlsx', FALSE);
    
    // get total number of rows in spreadsheet
    $tot = $excel -> rowcount($sheet_index = 0);
    
    // don't really need this bit but it makes it easier to follow in big scripts
    $cells = $excel -> sheets[0]['cells'];
    // Start the loop on the first row 
    for ($row = 1; $row <= $tot; $row++)
    {
        // the number 1 in $cells[$row][1] relates to the colummn as this is a multi dimentional array.
        // You could make this a variable so you have more adaptable search options
        
          $total = $total + $cells[$row][1];
          
        }
        
    echo $total;
    ?>    

 

but it returns

 

The filename testdata.xlsx is not readable

 

it's a basic file with A1:A10 filled with 1 and B1:B10 filled with 2

 

Excel_reader2.php is

 

/**

* A class for reading Microsoft Excel (97/2003) Spreadsheets.

*

* Version 2.21

*

* Enhanced and maintained by Matt Kruse < http://mattkruse.com >

* Maintained at http://code.google.com/p/php-excel-reader/

*

* Format parsing and MUCH more contributed by:

*    Matt Roxburgh < http://www.roxburgh.me.uk >

*

* DOCUMENTATION

* =============

http://code.google.com/p/php-excel-reader/wiki/Documentation

*

* CHANGE LOG

* ==========

http://code.google.com/p/php-excel-reader/wiki/ChangeHistory

*

* DISCUSSION/SUPPORT

* ==================

http://groups.google.com/group/php-excel-reader-discuss/topics

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.