如何为 Xlsx 文件生成 pCoordinates

我想为此生成坐标


 $sheet->setCellValue($pCoordinate, $pValue);

$pCoodrinates 是一个字母,然后是一个数字,如 A1、B1、C1,例如第一行,然后下一行是 A2、B2、C2,下一行是 3


这是我现在拥有的代码


class SpreadSheetHelper

{

    private static $alphabet = 'ABCDEFGHIJKLMNOPQRSTUYVWXYZ';


    public static function createSpreadSheet($data = []) {

        $spreadsheet = new Spreadsheet();

        $sheet       = $spreadsheet->getActiveSheet();

        foreach ($data as $rowIndex => $row) {

            foreach ($row as $columnIndex => $columnValue) {

                $pCoordinate = self::getAlphabetCoordinate($rowIndex, $columnIndex);

                $pValue = $columnValue;

                $sheet->setCellValue($pCoordinate, $pValue);

            }

        }


        return $spreadsheet;

    }



    private static function getAlphabetCoordinate($rowIndex, $columnIndex) {

        $letter = strtoupper(substr(self::$alphabet, $columnIndex, 1));

        $number = $rowIndex + 1;

        return "$letter$number";

    }

}

如您所见, $alphabet 是硬编码的并且是有限的,它到达最后一个字母,它应该以 AA, AB, AC,AD,AE,AF 开头,这就是我想要生成的。知道怎么做吗?


慕神8447489
浏览 61回答 1
1回答

牛魔王的故事

您可以利用 ASCII 表:<?php//Number of rows and columns$rows = 3;$cols = 40;$pcoords = array();for($current_row=1;$current_row<$rows+1;$current_row++) {&nbsp; &nbsp; $alpha_index = 65;&nbsp; &nbsp; $alpha_pref_index = 65;&nbsp; &nbsp; $alpha_count = 0;&nbsp; &nbsp; $pref_letter = '';&nbsp; &nbsp; for($current_col=0;$current_col<$cols;$current_col++) {&nbsp; &nbsp; &nbsp; &nbsp; $col_letter = chr($alpha_index);&nbsp; &nbsp; &nbsp; &nbsp; $pcoords[] = $pref_letter . $col_letter. $current_row;&nbsp; &nbsp; &nbsp; &nbsp; $alpha_count++;&nbsp; &nbsp; &nbsp; &nbsp; if ($alpha_count == 26) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $alpha_count = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $alpha_index = 65;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $pref_letter = chr($alpha_pref_index);&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $alpha_pref_index++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $alpha_index++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}echo '<pre>';print_r($pcoords);echo '</pre>';
打开App,查看更多内容
随时随地看视频慕课网APP