discuzX 3.4:修改PhpSpreadsheet中的rangeToArray函数,使第一行作为字段名

未修改前,excel活动sheet转为json数据,运行

$sheetData = $sheet->toArray(null, true, true, true);
echo json_encode($sheetData); 

返回的数据格式是:

{"1":{"A":"id","B":"c_ddid","C":"c_khid","D":"c_gyid","E":"c_dgrq","F":"c_dhrq","G":"c_fhrq","H":"c_yhs","I":"c_yhf","J":"c_hzmc","K":"c_hzdz","L":"c_hzcs","M":"c_hzdq","N":"\u8d27\u4e3b\u90ae\u653f\u7f16\u7801","O":"c_hzgj"},"2":{"A":9,"B":10256,"C":"WELLI","D":3,"E":"1996\/7\/15","F":"1996\/8\/12","G":"1996\/7\/17","H":2,"I":13.97,"J":"\u4f55","K":"\u5c71\u5927\u5317\u8def237\u53f7","L":"\u6d4e\u5357","M":"\u534e\u4e1c","N":873763,"O":"\u4e2d\u56fd"},"3":{"A":10,"B":10257,"C":"HILAA","D":4,"E":"1996\/7\/16","F":"1996\/8\/13","G":"1996\/7\/22","H":3,"I":81.91,"J":"\u738b","K":"\u6e05\u534e\u8def78\u53f7","L":"\u4e0a\u6d77","M":"\u534e\u4e1c","N":502234,"O":"\u4e2d\u56fd"},"4":{"A":11,"B":10258,"C":"ERNSH","D":1,"E":"1996\/7\/17","F":"1996\/8\/14","G":"1996\/7\/23","H":1,"I":140.51,"J":"\u738b\u5148\u751f","K":"\u7ecf\u4e09\u7eac\u56db\u8def48\u53f7","L":"\u6d4e\u5357","M":"\u534e\u4e1c","N":801009,"O":"\u4e2d\u56fd"},"5":{"A":12,"B":10259,"C":"CENTC","D":4,"E":"1996\/7\/18","F":"1996\/8\/15","G":"1996\/7\/25","H":3,"I":3.25,"J":"\u6797\u5c0f\u59d0","K":"\u9752\u5e74\u897f\u8def\u7532245\u53f7","L":"\u4e0a\u6d77","M":"\u534e\u4e1c","N":705022,"O":"\u4e2d\u56fd"}

我需要的是第一行作为字段名。修改后,运行

// 注意取数从第二行开始

$sheetData = $sheet->rangeToArrayCbq('A2:'. $highestCol . $highestRow, null, true, true, true);
echo json_encode($sheetData); 

返回的数据格式:

{"2":{"id":9,"c_ddid":10256,"c_khid":"WELLI","c_gyid":3,"c_dgrq":"1996\/7\/15","c_dhrq":"1996\/8\/12","c_fhrq":"1996\/7\/17","c_yhs":2,"c_yhf":13.97,"c_hzmc":"\u4f55","c_hzdz":"\u5c71\u5927\u5317\u8def237\u53f7","c_hzcs":"\u6d4e\u5357","c_hzdq":"\u534e\u4e1c","\u8d27\u4e3b\u90ae\u653f\u7f16\u7801":873763,"c_hzgj":"\u4e2d\u56fd"},"3":{"id":10,"c_ddid":10257,"c_khid":"HILAA","c_gyid":4,"c_dgrq":"1996\/7\/16","c_dhrq":"1996\/8\/13","c_fhrq":"1996\/7\/22","c_yhs":3,"c_yhf":81.91,"c_hzmc":"\u738b","c_hzdz":"\u6e05\u534e\u8def78\u53f7","c_hzcs":"\u4e0a\u6d77","c_hzdq":"\u534e\u4e1c","\u8d27\u4e3b\u90ae\u653f\u7f16\u7801":502234,"c_hzgj":"\u4e2d\u56fd"},"4":{"id":11,"c_ddid":10258,"c_khid":"ERNSH","c_gyid":1,"c_dgrq":"1996\/7\/17","c_dhrq":"1996\/8\/14","c_fhrq":"1996\/7\/23","c_yhs":1,"c_yhf":140.51,"c_hzmc":"\u738b\u5148\u751f","c_hzdz":"\u7ecf\u4e09\u7eac\u56db\u8def48\u53f7","c_hzcs":"\u6d4e\u5357","c_hzdq":"\u534e\u4e1c","\u8d27\u4e3b\u90ae\u653f\u7f16\u7801":801009,"c_hzgj":"\u4e2d\u56fd"},"5":{"id":12,"c_ddid":10259,"c_khid":"CENTC","c_gyid":4,"c_dgrq":"1996\/7\/18","c_dhrq":"1996\/8\/15","c_fhrq":"1996\/7\/25","c_yhs":3,"c_yhf":3.25,"c_hzmc":"\u6797\u5c0f\u59d0","c_hzdz":"\u9752\u5e74\u897f\u8def\u7532245\u53f7","c_hzcs":"\u4e0a\u6d77","c_hzdq":"\u534e\u4e1c","\u8d27\u4e3b\u90ae\u653f\u7f16\u7801":705022,"c_hzgj":"\u4e2d\u56fd"}

下面是修改后的函数内容。

    /**
     * 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 bool $calculateFormulas Should formulas be calculated?
     * @param bool $formatData Should formatting be applied to cell values?
     * @param bool $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
     */
	 // 第一行作为字段名(by cbq 20210203)
    public function rangeToArrayCbq($pRange, $nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false)
    {
        // Returnvalue
        $returnValue = [];
        //    Identify the range that we need to extract from the worksheet
        [$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($pRange);
        $minCol = Coordinate::stringFromColumnIndex($rangeStart[0]);
        $minRow = $rangeStart[1];
        $maxCol = Coordinate::stringFromColumnIndex($rangeEnd[0]);
        $maxRow = $rangeEnd[1];

        ++$maxCol;
        // Loop through rows
        $r = -1;
        for ($row = $minRow; $row <= $maxRow; ++$row) {
            $rRef = $returnCellRef ? $row : ++$r;
            $c = -1;
            // Loop through columns in the current row
            for ($col = $minCol; $col != $maxCol; ++$col) {
				//
				$row1Title = $this->cellCollection->get($col . "1")->getValue(); // cbq

                $cRef = $returnCellRef ? $col : ++$c;
				//$cRef = $returnCellRef ? $row1Title : ++$c;

                //    Using getCell() will create a new cell if it doesn't already exist. We don't want that to happen
                //        so we test and retrieve directly against cellCollection
                if ($this->cellCollection->has($col . $row)) {
                    // Cell exists
                    $cell = $this->cellCollection->get($col . $row);
                    if ($cell->getValue() !== null) {
                        if ($cell->getValue() instanceof RichText) {
                            $returnValue[$rRef][$row1Title] = $cell->getValue()->getPlainText();
                        } else {
                            if ($calculateFormulas) {
                                $returnValue[$rRef][$row1Title] = $cell->getCalculatedValue();
                            } else {
                                $returnValue[$rRef][$row1Title] = $cell->getValue();
                            }
                        }

                        if ($formatData) {
                            $style = $this->parent->getCellXfByIndex($cell->getXfIndex());
                            $returnValue[$rRef][$row1Title] = NumberFormat::toFormattedString(
                                $returnValue[$rRef][$row1Title],
                                ($style && $style->getNumberFormat()) ? $style->getNumberFormat()->getFormatCode() : NumberFormat::FORMAT_GENERAL
                            );
                        }
                    } else {
                        // Cell holds a NULL
                        $returnValue[$rRef][$row1Title] = $nullValue;
                    }
                } else {
                    // Cell doesn't exist
                    $returnValue[$rRef][$row1Title] = $nullValue;
                }
            }
        }

        // Return
        return $returnValue;
    }

猜你喜欢

转载自blog.csdn.net/bq_cui/article/details/113595098
今日推荐