宅男在线永久免费观看网直播,亚洲欧洲日产国码无码久久99,野花社区在线观看视频,亚洲人交乣女bbw,一本一本久久a久久精品综合不卡

全部
常見問題
產(chǎn)品動(dòng)態(tài)
精選推薦

知識(shí)付費(fèi)的導(dǎo)入導(dǎo)出功能

管理 管理 編輯 刪除

知識(shí)付費(fèi)的導(dǎo)入導(dǎo)出功能使用了PhpSpreadsheet所以我們首先要了解它。

一、PhpSpreadsheet 介紹

1、PhpSpreadsheet 是什么

PhpSpreadsheet是一個(gè)用純PHP編寫的庫(kù),提供了一組類,使您可以讀取和寫入不同的電子表格文件格式
PhpSpreadsheet提供了豐富的API接口,可以設(shè)置諸多單元格以及文檔屬性,包括樣式、圖片、日期、函數(shù)等等諸多應(yīng)用,總之你想要什么樣的Excel表格,PhpSpreadsheet都能做到

· 使用 PhpSpreadsheet 開發(fā)的PHP要求 7.1或更高版本

· PhpSpreadsheet 支持鏈?zhǔn)讲僮?/span>

2、PhpSpreadsheet 支持的文件格式

c2297202502201014595930.png

3、PhpSpreadsheet 官方網(wǎng)址

· https://phpspreadsheet.readthedocs.io

4、PhpSpreadsheet 安裝

· composer require phpoffice/phpspreadsheet

二、使用PhpSpreadsheet 完成導(dǎo)出功能

項(xiàng)目中extend/service/PhpSpreadsheetService文件outdata方法為導(dǎo)出方法

/**
     * 通用導(dǎo)出方法。傳入?yún)?shù)即可
     * @param unknown $filename 導(dǎo)出的excel文件名稱,不包括后綴
     * @param unknown $rows 要導(dǎo)出的數(shù)據(jù),數(shù)組
     * @param unknown $head 要導(dǎo)出數(shù)據(jù)的表頭,數(shù)組
     * @param unknown $keys 要導(dǎo)出數(shù)據(jù)的鍵值對(duì)對(duì)應(yīng)
     */
    public static function outdata($filename = '', $rows = [], $head = [])
    {
        $count = count($head);  //計(jì)算表頭數(shù)量

        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        //設(shè)置樣式,設(shè)置劇中,加邊框,設(shè)置行高
        $styleArray = [
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
            ],
            'borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                    'color' => ['argb' => '6184542'],
                ],
            ],
        ];
        $rows_count = count($rows);
        $sheet->getDefaultRowDimension()->setRowHeight(18);//設(shè)置默認(rèn)行高。
        $sheet->getStyle('A1:' . strtoupper(chr($count + 65 - 1)) . strval($rows_count + 1))->applyFromArray($styleArray);
        $sheet->getStyle('A4:' . strtoupper(chr($count + 65 - 1)) . '1')->getFont()->setBold(true)->setName('Arial')->setSize(10)->applyFromArray($styleArray);
        //設(shè)置樣式結(jié)束

        //寫入表頭信息
        for ($i = 65; $i < $count + 65; $i++) {
            //數(shù)字轉(zhuǎn)字母從65開始,循環(huán)設(shè)置表頭:
            $sheet->setCellValue(strtoupper(chr($i)) . '1', $head[$i - 65]);
        }

        //寫入數(shù)據(jù)信息
        foreach ($rows as $key => $item) {
            //循環(huán)設(shè)置單元格:
            //$key+2,因?yàn)榈谝恍惺潜眍^,所以寫到表格時(shí)   從第二行開始寫
            for ($i = 65; $i < $count + 65; $i++) {
                //數(shù)字轉(zhuǎn)字母從65開始:
                $sheet->setCellValue(strtoupper(chr($i)) . ($key + 2), $item[$i - 65]);
                $spreadsheet->getActiveSheet()->getColumnDimension(strtoupper(chr($i)))->setWidth(30); //固定列寬
                // 支持換行
//                $sheet->getStyle(strtoupper(chr($i)))->getAlignment()->setWrapText(true);
            }

        }
        //header('Content-Type: application/vnd.ms-excel');xls
        header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//xlsx
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $writer = new Xlsx($spreadsheet);
        $writer->save('php://output');

        //刪除清空:
        $spreadsheet->disconnectWorksheets();
        unset($spreadsheet);
        exit;
    }

項(xiàng)目中使用,如會(huì)員記錄的導(dǎo)出;方法傳入表格名稱、要導(dǎo)出的數(shù)據(jù)(數(shù)組)、要導(dǎo)出數(shù)據(jù)的表頭(數(shù)組)

    public static function getPurchaseRecordList($where){
    $model = new self();
    if (isset($where['excel']) && $where['excel'] == 1) {$list = $model->select();}
    if (isset($where['excel']) && $where['excel'] == 1) {self::SaveExcel($list);}
}
    /**
     * 保存并下載excel
     * $list array
     * return
     */
    public static function SaveExcel($list)
    {
        $export = [];
        foreach ($list as $index => $item) {
            $export[] = [
                $item['id'],
                $item['uid'],
                $item['title'],
                $item['source'],
                $item['validity'],
                $item['price'],
                $item['code']
            ];
        }
        $filename = '會(huì)員記錄導(dǎo)出' . time() . '.xlsx';
        $head = ['編號(hào)', '昵稱/UID', '類別', '來源', '有效期/天', '優(yōu)惠價(jià)', '卡號(hào)'];
        PhpSpreadsheetService::outdata($filename, $export, $head);
    }


三、使用PhpSpreadsheet 完成導(dǎo)入功能

引入use \PhpOffice\PhpSpreadsheet\IOFactory;

方法傳入兩個(gè)參數(shù)$filename 文件名稱  $startLine  從哪一行開始讀取

$widt(數(shù)組)  數(shù)據(jù)讀取后的數(shù)組格式

   /**文件導(dǎo)入
     * @param string $filename
     * @param int $startLine
     * @param array $width
     * @return array
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     */
    public static function GetExcelData($filename = '1.xlsx', $startLine = 4)
    {
        $width = [
            'question_type' => 'A',
            'pid' => 'B',
            'stem' => 'C',
            'image' => 'D',
            'is_img' => 'E',
            'a' => 'F',
            'b' => 'G',
            'c' => 'H',
            'd' => 'I',
            'e' => 'J',
            'f' => 'K',
            'answer' => 'L',
            'difficulty' => 'M',
            'analysis' => 'N',
            'sort' => 'O'
        ];
        $filename = ROOT_PATH . 'public' . $filename;
        $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
        switch ($extension) {
            case 'xlsx':
                $reader = IOFactory::createReader('Xlsx');
                $spreadsheet = $reader->load($filename);
                break;
            case 'xls':
                $reader = IOFactory::createReader('Xls');
                $spreadsheet = $reader->load($filename);
                break;
            case 'csv':
                $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
                $reader->setInputEncoding('GBK');
                $reader->setDelimiter(',');
                $reader->setEnclosure('');
                $reader->setSheetIndex(0);
                $spreadsheet = $reader->load($filename);
                break;
        }
        $highestRow = $spreadsheet->getSheet(0)->getHighestRow(); // 取得總行數(shù)
        $getvalue = $spreadsheet->getActiveSheet();
        $data = [];
        for ($j = $startLine; $j <= (int)$highestRow; $j++) {
            $value = [];
            foreach ($width as $key => $val) {
                if ($v = $getvalue->getCell($val . $j)->getValue()) $value[$key] = $v;
                else $value[$key] = '';
            }
            if ($value) $data[] = $value;
        }
        return $data;
    }


根據(jù)導(dǎo)出數(shù)組處理導(dǎo)入數(shù)據(jù)

/**批量導(dǎo)入試題
     * @param array $data
     */
    public static function importQuestions($data = [])
    {
        foreach ($data as $key => $value) {
            $dat = [];
            switch ($value['question_type']) {
                case 1:
                    if ($value['a']) $dat['A'] = $value['a'];
                    if ($value['b']) $dat['B'] = $value['b'];
                    if ($value['c']) $dat['C'] = $value['c'];
                    if ($value['d']) $dat['D'] = $value['d'];
                case 2:
                    if ($value['a']) $dat['A'] = $value['a'];
                    if ($value['b']) $dat['B'] = $value['b'];
                    if ($value['c']) $dat['C'] = $value['c'];
                    if ($value['d']) $dat['D'] = $value['d'];
                    if ($value['e']) $dat['E'] = $value['e'];
                    if ($value['f']) $dat['F'] = $value['f'];
                    break;
                case 3:
                    if ($value['a']) $dat['A'] = $value['a'];
                    if ($value['b']) $dat['B'] = $value['b'];
                    break;
            }
            $array['question_type'] = $value['question_type'];
            $array['pid'] = $value['pid'];
            $array['stem'] = $value['stem'];
            $array['image'] = $value['image'];
            $array['is_img'] = $value['is_img'];
            $array['answer'] = trim($value['answer'], " ");
            $array['difficulty'] = $value['difficulty'];
            $array['analysis'] = $value['analysis'];
            $array['sort'] = (int)$value['sort'];
            $array['option'] = json_encode($dat);
            $array['add_time'] = time();
            if (self::be(['stem' => $value['stem'], 'question_type' => $value['question_type'], 'pid' => $value['pid'], 'is_del' => 0, 'mer_id' => 0])) continue;
            self::set($array);
        }
        return true;
    }

如此使用PhpSpreadsheet完成導(dǎo)入導(dǎo)出功能完成

請(qǐng)登錄后查看

全 最后編輯于2025-02-20 10:23:50

快捷回復(fù)
回復(fù)
回復(fù)
回復(fù)({{post_count}}) {{!is_user ? '我的回復(fù)' :'全部回復(fù)'}}
排序 默認(rèn)正序 回復(fù)倒序 點(diǎn)贊倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level }}

作者 管理員 企業(yè)

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
{{item.is_suggest == 1? '取消推薦': '推薦'}}
沙發(fā) 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暫無簡(jiǎn)介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
打賞
已打賞¥{{item.reward_price}}
{{item.like_count}}
{{item.showReply ? '取消回復(fù)' : '回復(fù)'}}
刪除
回復(fù)
回復(fù)

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回復(fù) {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
打賞
已打賞¥{{itemc.reward_price}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回復(fù)' : '回復(fù)'}}
刪除
回復(fù)
回復(fù)
查看更多
打賞
已打賞¥{{reward_price}}
820
{{like_count}}
{{collect_count}}
添加回復(fù) ({{post_count}})

相關(guān)推薦

快速安全登錄

使用微信掃碼登錄
{{item.label}} 加精
{{item.label}} {{item.label}} 板塊推薦 常見問題 產(chǎn)品動(dòng)態(tài) 精選推薦 首頁頭條 首頁動(dòng)態(tài) 首頁推薦
取 消 確 定
回復(fù)
回復(fù)
問題:
問題自動(dòng)獲取的帖子內(nèi)容,不準(zhǔn)確時(shí)需要手動(dòng)修改. [獲取答案]
答案:
提交
bug 需求 取 消 確 定
打賞金額
當(dāng)前余額:¥{{rewardUserInfo.reward_price}}
{{item.price}}元
請(qǐng)輸入 0.1-{{reward_max_price}} 范圍內(nèi)的數(shù)值
打賞成功
¥{{price}}
完成 確認(rèn)打賞

微信登錄/注冊(cè)

切換手機(jī)號(hào)登錄

{{ bind_phone ? '綁定手機(jī)' : '手機(jī)登錄'}}

{{codeText}}
切換微信登錄/注冊(cè)
暫不綁定
CRMEB客服

CRMEB咨詢熱線 咨詢熱線

400-8888-794

微信掃碼咨詢

CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
返回頂部 返回頂部
CRMEB客服