Jump to content

add up values in a column in excel?


muppet77

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.