【问题标题】:What are the purpose of three bool value in PHPExcel reader?PHPExcel阅读器中三个布尔值的目的是什么?
【发布时间】:2017-03-07 10:23:09
【问题描述】:

我在 Yii2 应用程序中使用 PHPExcel 阅读器从 Exce 文件中读取数据。 这是我使用的代码:

$objPHPExcel = new \PHPExcel();
        $fileName = Yii::getAlias('@webroot/trash/trash_vatout/') . $name;
        $inputFiles = fopen(Yii::getAlias('@webroot/trash/trash_vatout/') . $name, "r");
        try {
            $inputFileType = \PHPExcel_IOFactory::identify($fileName);
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($fileName);
        } catch (Exception $ex) {
            die('Error');
        }
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestDataRow();
        $highestColumn = $sheet->getHighestDataColumn();
        $colNumber = PHPExcel_Cell::columnIndexFromString($highestColumn);
        $col = $colNumber - 1;
        $arrayData = [];

   $bool1 = NULL;            //first bool value
   $bool2 = NULL;            //second bool value
   $bool3 = NULL;            //third bool value
   for ($row = 1; $row <= $highestRow; ++$row) {
     $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, $bool1, $bool2, $bool3);
     if (!is_null($rowData[0][$col])) {
        $arrayData[] = array_map(function($values) {
           $tempArrayKey = [];
           foreach ($values as $key => $value) {
               $newKey = $key + 1;
               $tempArrayKey[] = $newKey . '_' . $value;
           }
           return $tempArrayKey;
     }, $rowData);
   }
  }

我在一些来源的教程之后使用它。 在行代码$rowData = $sheet-&gt;rangeToArray('A' . $row . ':' . $highestColumn . $row, $bool1, $bool2, $bool3); 中,设置了三个布尔值。就我而言,我将它们全部设置为 NULL

有人知道布尔值的实际用途是什么吗?

我已经多次尝试读取文件,如果我没记错的话,第二个布尔值设置为读取Excel公式 .

但是其他人呢?

谢谢。

【问题讨论】:

  • 您好,我看到您刚刚开始进入 PHP 语言的美好旅程。有时请不要忘记阅读文档,如下所示:hitautodestruct.github.io/PHPExcelAPIDocs/classes/… 其中所有内容都一一解释。
  • 哦,我忘了一件事。您必须单击“从一系列单元格创建数组”标题以展开规范。玩得开心的朋友!

标签: excel yii2 phpexcel phpexcelreader


【解决方案1】:

rangeToArray() method 的签名是

/**
 * 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
 */

所以

  • $bool1 - 混合$nullValue 如果单元格不存在,则在数组条目中返回值(可以是任何数据类型/值)
  • $bool2 - 布尔值$calculateFormulas 应该计算公式吗?
  • $bool3 - 布尔值$formatData 是否应将格式应用于单元格值?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-17
    • 2011-01-29
    • 1970-01-01
    • 2022-11-08
    • 2016-04-03
    • 1970-01-01
    • 2019-05-22
    • 2016-03-29
    相关资源
    最近更新 更多