{"id":529,"date":"2020-12-21T14:31:17","date_gmt":"2020-12-21T06:31:17","guid":{"rendered":"https:\/\/www.djlog.cn\/?p=529"},"modified":"2021-10-27T09:25:33","modified_gmt":"2021-10-27T01:25:33","slug":"thinkphp6-%e4%bd%bf%e7%94%a8-phpoffice-phpspreadsheet-%e5%af%bc%e5%87%ba%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/www.djlog.cn\/?p=529","title":{"rendered":"Thinkphp6 \u4f7f\u7528 phpoffice\/phpspreadsheet \u5bfc\u51fa\u6587\u4ef6"},"content":{"rendered":"<pre class=\"language-php\"><code>  \/**\r\n     * @param array $data \u6570\u636e\r\n     * @param array $title \u8868\u5934\r\n     * @return bool\r\n     * @throws \\PhpOffice\\PhpSpreadsheet\\Writer\\Exception \u4e0b\u8f7d\u6587\u4ef6\r\n     *\/\r\n    public function _export($data,$title){\r\n        \/\/\u6570\u636e\r\n        \/*$data = [\r\n            ['title1' =&gt; '111', 'title2' =&gt; '222'],\r\n            ['title1' =&gt; '111', 'title2' =&gt; '222']\r\n        ];\r\n        \/\/\u8868\u5934\r\n        $title = ['\u7b2c\u4e00\u884c\u6807\u9898', '\u7b2c\u4e8c\u884c\u6807\u9898'];*\/\r\n\r\n        $spreadsheet = new Spreadsheet();\r\n        $sheet = $spreadsheet-&gt;getActiveSheet();\r\n\r\n        \/\/\u8868\u5934\r\n        \/\/\u8bbe\u7f6e\u5355\u5143\u683c\u5185\u5bb9\r\n        $titCol = 'A';\r\n        foreach ($title as $key =&gt; $value) {\r\n            \/\/ \u5355\u5143\u683c\u5185\u5bb9\u5199\u5165\r\n            $sheet-&gt;setCellValue($titCol . '1', $value);\r\n            $titCol++;\r\n        }\r\n        $row = 2; \/\/ \u4ece\u7b2c\u4e8c\u884c\u5f00\u59cb\r\n        foreach ($data as $item) {\r\n            $dataCol = 'A';\r\n            foreach ($item as $value) {\r\n                \/\/ \u5355\u5143\u683c\u5185\u5bb9\u5199\u5165\r\n                $sheet-&gt;setCellValue($dataCol . $row, $value);\r\n                $dataCol++;\r\n            }\r\n            $row++;\r\n        }\r\n\r\n        ob_end_clean();\/\/\u6e05\u9664\u7f13\u51b2\u533a,\u907f\u514d\u4e71\u7801\r\n        ob_start();\r\n        \/\/ \u5c06\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u5ba2\u6237\u7aef\u7684\u7f51\u7edc\u6d4f\u89c8\u5668\uff08Xlsx\uff09\r\n        header('Content-Type: application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet');\r\n        header('Cache-Control: max-age=0');\r\n        \/\/ \u5982\u679c\u60a8\u8981\u4f7f\u7528IE 9\uff0c\u5219\u53ef\u80fd\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\r\n        header('Cache-Control: max-age=1');\r\n\r\n        \/\/ \u5982\u679c\u60a8\u901a\u8fc7SSL\u5411IE\u63d0\u4f9b\u670d\u52a1\uff0c\u5219\u53ef\u80fd\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\r\n        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); \/\/ Date in the past\r\n        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); \/\/ always modified\r\n        header('Cache-Control: cache, must-revalidate'); \/\/ HTTP\/1.1\r\n        header('Pragma: public'); \/\/ HTTP\/1.0\r\n\r\n        header('Content-type:application\/vnd.ms-excel;charset=utf-8;name=\"'.date('YmdHis').'.xlsx\"');\r\n        header(\"Content-Disposition:attachment;filename=\".date('YmdHis').\".xlsx\");\/\/attachment\u65b0\u7a97\u53e3\u6253\u5370 inline\u672c\u7a97\u53e3\u6253\u5370\r\n\r\n        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');\r\n        $writer-&gt;save('php:\/\/output');\r\n\r\n        \/* \u91ca\u653e\u5185\u5b58 *\/\r\n        $spreadsheet-&gt;disconnectWorksheets();\r\n        unset($spreadsheet);\r\n        ob_end_flush();\r\n        return true;\r\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/** * @param array $data \u6570\u636e * @param array $title \u8868\u5934 *  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,16],"tags":[29],"class_list":["post-529","post","type-post","status-publish","format-standard","hentry","category-php","category-thinkphp","tag-thinkphp"],"_links":{"self":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=529"}],"version-history":[{"count":0,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/529\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}