2

我是 PHP 新手,我正在从 excel 电子表格中读取数据,我想将每一行保存为一个数组。在我拥有该数组之后,我想将该数组传递给另一个函数以“设置”具有特定标题的每个数组值。我无法弄清楚这一点,并希望有人能提供帮助。

这是第一个脚本:

    <?php 
include './Classes/PHPExcel.php';
include 'testCase.php';

class ExcelReader{

function getExcelFile(){
/*Get file from form*/
    echo 'Hello world' . "\n";
    $FileName = $_FILES["fileName"]["name"];

    /*Move file to server, if file already exists, don't move*/
    if(file_exists("uploadedFiles/".$_FILES["fileName"]["name"])){
        echo $_FILES["fileName"]["name"]." already exists";
    }
    else{
        move_uploaded_file($_FILES["fileName"]["tmp_name"],"uploadedFiles/".$_FILES["fileName"]["name"]);
        echo $_FILES["fileName"]["name"]." has been moved";



    }
    return $FileName;
}

function readExcelFile($FileName){


    /*Create reader object for file and read object in*/
    $PHPExcelReader= PHPExcel_IOFactory::createReaderForFile($FileName);
    $PHPExcelReader->setReadDataOnly(true);
    $PHPExcelObj=$PHPExcelReader->load($FileName);






    $PHPobjWorksheet = $PHPExcelObj->getActiveSheet();

    $topRow = $PHPobjWorksheet->getHighestRow();
    $topCol = $PHPobjWorksheet->getHighestColumn();

    $highestColIndex=  PHPExcel_Cell::columnIndexFromString($topCol);
    for($row=1;$row<=$topRow; $row++){

        $newTestCase = new testCase();
        $testIndex = $newTestCase->getTestCase($row, $highestColIndex, $PHPobjWorksheet);
        echo $testIndex."<- Test Index <br/>";
        $newTestCase->setTestCase($testIndex);
    }
 }
}

$newExcelReader = new ExcelReader;
$newFileName = $newExcelReader->getExcelFile();
$newExcelReader->readExcelFile($newFileName);

?>

这是我用来获取数据的第二个类,我正在尝试设置:

    <?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Class for creating test cases to be sent 
 * into TestRail
 * @author Banderson
 */
class testCase {

    /*Function to get the data for each individual test case*/
    function getTestCase($row, $highestColIndex, $PHPobjWorksheet){

            echo "<br />";
            for($col=0;$col<=$highestColIndex;++$col){
                $ii=0;
                $cellValue=array($PHPobjWorksheet->getCellByColumnAndRow($col,$row)->getValue());
                echo $cellValue[$ii]. " | ";
                $ii++;


        }

    return $cellValue;

    }


    /*Function to set the data for each individual test case*/
    function setTestCase($cellValue){
                $title = $cellValue[0];
                echo $title. "<- Title"."<br/>";
                $type = $cellValue[1];
                echo $type. "<- Type"."<br/>";
                $priority = $cellValue[2];
                echo $priority. "<- Priority"."<br/>";
                $estimate = $cellValue[3];
                echo $estimate. "<- Estimate"."<br/>";
                $milestone = $cellValue[4];
                echo $milestone. "<- MileStone"."<br/>";
                $references = $cellValue[5];
                echo $references. "<- 5"."<br/>";
                $preconditions = $cellValue[6];
                echo $preconditions. "<- 6"."<br/>";
                $steps = $cellValue[7];
                echo $steps. "<- 7"."<br/>";
                $expectedResults = $cellValue[8];
                echo $expectedResults. "<- 8"."<br/>";
                $testSuit = $cellValue[9];
                echo $testSuit. "<- 9"."<br/>";


    }



}

?>

最后这是我收到的错误消息:

    Hello world testrailtestinputV2.xlsx already exists
Title | Type | Priority | Estimate | Milestone | Reference | Preconditions | Steps | Expected Result | Section | Test Suite | | Array<- Test Index<- Title

( ! ) Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 37
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- Type

( ! ) Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 39
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- Priority

( ! ) Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 41
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- Estimate

( ! ) Notice: Undefined offset: 4 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 43
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- MileStone

( ! ) Notice: Undefined offset: 5 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 45
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- 5

( ! ) Notice: Undefined offset: 6 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 47
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- 6

( ! ) Notice: Undefined offset: 7 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 49
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- 7

( ! ) Notice: Undefined offset: 8 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 51
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- 8

( ! ) Notice: Undefined offset: 9 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 53
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.3133  5664536 testCase->setTestCase( )    ../testRailScripting.php:50
<- 9

Title 1 | Functionalit | 1 | | | | | 1. Turn phone on 2. Go to main menu 3. Push button | The screen should look like this | | | | Array<- Test Index<- Title

( ! ) Notice: Undefined offset: 1 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 37
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- Type

( ! ) Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 39
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- Priority

( ! ) Notice: Undefined offset: 3 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 41
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- Estimate

( ! ) Notice: Undefined offset: 4 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 43
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- MileStone

( ! ) Notice: Undefined offset: 5 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 45
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- 5

( ! ) Notice: Undefined offset: 6 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 47
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- 6

( ! ) Notice: Undefined offset: 7 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 49
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- 7

( ! ) Notice: Undefined offset: 8 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 51
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- 8

( ! ) Notice: Undefined offset: 9 in /Applications/MAMP/htdocs/TestRailIntegration/testCase.php on line 53
Call Stack
#   Time    Memory  Function    Location
1   0.0000  635280  {main}( )   ../testRailScripting.php:0
2   0.0034  749112  ExcelReader->readExcelFile( )   ../testRailScripting.php:57
3   0.5097  5675280 testCase->setTestCase( )    ../testRailScripting.php:50
<- 9

Title 2 | Functionalit | 1 | | | | | 1. Turn phone on 2. Go to main menu 3. Push button | The screen should look like this | | | | Array<- Test Index<- Title

任何帮助都会很棒!先感谢您!

4

2 回答 2

3

您的函数使用一个元素创建新数组,并在每次迭代时getTestCase将其放入变量中。$cellValue所以,这个函数返回的数组只包含一个元素。您应该以这种方式修复它:

function getTestCase($row, $highestColIndex, $PHPobjWorksheet) {
  $cellValue = array();
  for($col = 0; $col <= $highestColIndex; ++$col) {
    $v = $PHPobjWorksheet->getCellByColumnAndRow($col,$row)->getValue();
    $cellValue[] = $v; //append element to the array
  }
  return $cellValue;
}
于 2012-05-13T07:56:16.843 回答
0

与其单独读取行中的每个单元格并自己根据这些值构建一个数组,不如让 PHPExcel 使用工作表的 toArray() 方法为您构建数组

/**
 * Create array from a range of cells
 *
 * @param  string   $pRange             Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
 * @param  mixed    $nullValue           Value returned in the array entry if a cell doesn't exist
 * @param  boolean  $calculateFormulas  Should formulas be calculated?
 * @param  boolean  $formatData         Should formatting be applied to cell values?
 * @param  boolean  $returnCellRef      False - Return a simple array of rows and columns indexed by number counting from zero
 *                                      True - Return rows and columns indexed by their actual row and column IDs
 * @return array
 */
public function rangeToArray($pRange = 'A1', $nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false)

只需使用标准 Excel 范围格式符号(例如 A1:Z1)指定范围

于 2012-05-13T16:52:13.240 回答