尊龙凯时人生就是博

ÔõÑùʹÓÃThinkPHP6¾ÙÐÐExcelµ¼ÈëºÍµ¼³ö£¿

Ëæ×Å»¥ÁªÍøµÄÉú³¤£¬Êý¾Ýµ¼ÈëºÍµ¼³öµÄÐèÇóÔ½À´Ô½¶à£¬ÓÈÆäÊÇÔÚÆóÒµÖС£excel×÷ΪһÖÖºÜÊÇÊ¢Ðеİ칫Èí¼þ£¬Ò²±»ÆÕ±éÓ¦ÓÃÓÚÊý¾ÝµÄ´æ´¢ºÍ´¦Àí¡£Òò´Ë£¬ÔõÑùʹÓÃthinkphp6¾ÙÐÐexcelµÄµ¼ÈëºÍµ¼³öÒѾ­³ÉΪÁËÒ»¸öºÜÖ÷ÒªµÄÎÊÌâ¡£±¾ÎĽ«ÏÈÈÝʹÓÃthinkphp6¾ÙÐÐexcelµ¼ÈëºÍµ¼³öµÄ°ì·¨¡£

Ò»¡¢Excelµ¼³ö

ThinkPHP6ÌṩÁËÒ»¸öºÜÊÇÀû±ãµÄExcelµ¼³ö¹¤¾ßÀࡪ¡ªPHPExcel£¬Ê¹ÓÃPHPExcel¿ÉÒÔ½«Êý¾Ýµ¼³öΪExcelÎļþ£¬Ïêϸ°ì·¨ÈçÏ£º

1.×°ÖÃPHPExcelÀà¿â

ÔÚcomposer.jsonÎļþÖÐÌí¼ÓPHPExcelÀà¿â£º

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡± £»

"require": {
    "phpoffice/phpexcel": "^1.8"
},

µÇ¼ºó¸´ÖÆ

Ö´ÐÐÏÂÁî×°ÖÃPHPExcelÀà¿â£º

composer install

µÇ¼ºó¸´ÖÆ

2.½¨ÉèExcelµ¼³öController

½¨ÉèÒ»¸ö¿ØÖÆÆ÷ÀàÀ´´¦ÀíExcelµ¼³öµÄÇëÇó£º

namespace appdmincontroller;

use PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetWriterXlsx as Writer;

class Excel extends Base
{
    public function export()
    {
        // TODO: µ¼³öExcel
    }
}

µÇ¼ºó¸´ÖÆ

3.½á¹¹Êý¾ÝÔ´

ÔÚµ¼³öExcel֮ǰ£¬ÐèÒªÏÈ×¼±¸ºÃÒªµ¼³öµÄÊý¾ÝÔ´¡£Í¨³£ÓÐÁ½ÖÖ·½·¨À´»ñÈ¡Êý¾ÝÔ´£º

(1) ´ÓÊý¾Ý¿âÖлñÈ¡Êý¾Ý

namespace appdmincontroller;

use appdminmodelUser as UserModel;
use PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetWriterXlsx as Writer;

class Excel extends Base
{
    public function export()
    {
        $users = UserModel::select()->toArray();

        $spreadsheet = new Spreadsheet();

        $sheet = $spreadsheet->getActiveSheet();
        $sheet->setCellValue('A1', 'ID');
        $sheet->setCellValue('B1', 'ÐÕÃû');
        $sheet->setCellValue('C1', 'ÐÔ±ð');
        $sheet->setCellValue('D1', 'ÄêËê');

        $row = 2;
        foreach ($users as $user) {
            $sheet->setCellValue('A' . $row, $user['id']);
            $sheet->setCellValue('B' . $row, $user['name']);
            $sheet->setCellValue('C' . $row, $user['gender']);
            $sheet->setCellValue('D' . $row, $user['age']);
            $row++;
        }

        $writer = new Writer($spreadsheet);
        $writer->save('users.xlsx');
    }
}

µÇ¼ºó¸´ÖÆ

(2) ´ÓÆäËûÊý¾ÝÔ´»ñÈ¡Êý¾Ý

ÈôÊÇÎÒÃÇÒª½«Ò»Ð©Êý¾Ýµ¼³öΪExcelÎļþ£¬µ«ÕâЩÊý¾Ý²¢²»´æ´¢ÔÚÊý¾Ý¿âÖУ¬ºÃ±ÈÎÒÃÇÒªÒÔExcelµÄÐÎʽµ¼³öһЩ¶©µ¥ÐÅÏ¢µÈ¡£Õâʱ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÆäËû·½·¨À´»ñÈ¡ÕâЩÊý¾Ý£¬ÀýÈç´ÓÍøÂçAPI½Ó¿ÚÖлñÈ¡¡£

namespace appdmincontroller;

use GuzzleHttpClient;
use PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetWriterXlsx as Writer;

class Excel extends Base
{
    public function export()
    {
        $client = new Client();

        $response = $client->get('https://api.example.com/orders');

        $orders = json_decode($response->getBody()->getContents(), true);

        $spreadsheet = new Spreadsheet();

        $sheet = $spreadsheet->getActiveSheet();
        $sheet->setCellValue('A1', '¶©µ¥±àºÅ');
        $sheet->setCellValue('B1', 'ϵ¥ÈË');
        $sheet->setCellValue('C1', '¶©µ¥½ð¶î');

        $row = 2;
        foreach ($orders as $order) {
            $sheet->setCellValue('A' . $row, $order['id']);
            $sheet->setCellValue('B' . $row, $order['user']);
            $sheet->setCellValue('C' . $row, $order['amount']);
            $row++;
        }

        $writer = new Writer($spreadsheet);
        $writer->save('orders.xlsx');
    }
}

µÇ¼ºó¸´ÖÆ

¶þ¡¢Excelµ¼Èë

ʹÓÃThinkPHP6´¦ÀíExcelµ¼ÈëÒ²ºÜÊÇÀû±ã£¬Í¬ÑùʹÓÃPHPExcelÀà¿âʵÏÖ£¬Ïêϸ°ì·¨ÈçÏ£º

1.×°ÖÃPHPExcelÀà¿â

ͬExcelµ¼³öµÄ°ì·¨Ò»Ñù£¬ÐèÒªÏÈ×°ÖÃPHPExcelÀà¿â¡£

2.½¨ÉèExcelµ¼ÈëController

½¨ÉèÒ»¸ö¿ØÖÆÆ÷ÀàÀ´´¦ÀíExcelµ¼ÈëµÄÇëÇó£º

namespace appdmincontroller;

use PhpOfficePhpSpreadsheetIOFactory;

class Excel extends Base
{
    public function import()
    {
        $file = request()->file('file');

        $info = $file->validate(['ext' => 'xlsx'])->move('uploads');

        if ($info) {
            $filename = 'uploads/' . $info->getSaveName();

            $reader = IOFactory::createReader('Xlsx');
            $spreadsheet = $reader->load($filename);

            $sheet = $spreadsheet->getActiveSheet();
            $highestRow = $sheet->getHighestRow();
            $highestColumn = $sheet->getHighestColumn();

            $data = [];

            for ($row = 2; $row <= $highestRow; $row++) {
                $rowData = [];
                for ($column = 'A'; $column <= $highestColumn; $column++) {
                    $cellValue = $sheet->getCell($column . $row)->getValue();
                    $rowData[] = $cellValue;
                }
                $data[] = $rowData;
            }

            unlink($filename);

            dump($data);
        } else {
            echo $file->getError();
        }
    }
}

µÇ¼ºó¸´ÖÆ

3.ÉÏ´«ExcelÎļþ

ÎÒÃÇÐèÒªÔÚÊÓͼÖÐÌí¼ÓÒ»¸öÉÏ´«±íµ¥£¬À´ÈÃÓû§ÉÏ´«Òªµ¼ÈëµÄExcelÎļþ¡£

<form method="post" action="admin/excel/import"
      enctype="multipart/form-data">
      <input type="file" name="file">
      <input type="submit" value="ÉÏ´«">
</form>

µÇ¼ºó¸´ÖÆ

4.´¦Àíµ¼ÈëÊý¾Ý

ÔÚµ¼ÈëExcelÖ®ºó£¬ÎÒÃÇ¿ÉÒÔͨ¹ýPHPExcelÌṩµÄAPI»ñÈ¡µ½µ¼ÈëµÄÊý¾Ý¡£ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËÒÔÏ´úÂëÀ´»ñÈ¡Êý¾Ý£º

$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();

$data = [];

for ($row = 2; $row <= $highestRow; $row++) {
    $rowData = [];
    for ($column = 'A'; $column <= $highestColumn; $column++) {
        $cellValue = $sheet->getCell($column . $row)->getValue();
        $rowData[] = $cellValue;
    }
    $data[] = $rowData;
}

µÇ¼ºó¸´ÖÆ

µ¼ÈëµÄÊý¾Ý¼´´æ´¢ÔÚ$data±äÁ¿ÖУ¬ÎÒÃÇ¿ÉÒÔ¾ÙÐкóÐøµÄÊý¾Ý´¦Àí²Ù×÷£¬ºÃ±È½«Êý¾Ý²åÈëµ½Êý¾Ý¿âÖС£

×ÛÉÏËùÊö£¬Ê¹ÓÃThinkPHP6¾ÙÐÐExcelµ¼ÈëºÍµ¼³öÏà¶ÔÀ´Ëµ½ÏÁ¿¼òÆÓ£¬Í¨¹ýʹÓÃPHPExcelÀà¿â£¬ÎÒÃÇ¿ÉÒÔºÜÀû±ãµØʵÏÖExcelÎļþµÄ¶ÁÈ¡ºÍµ¼³ö¡£

ÒÔÉϾÍÊÇÔõÑùʹÓÃThinkPHP6¾ÙÐÐExcelµ¼ÈëºÍµ¼³ö£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是博ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是博ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ