threshold / php-excel
PHP 库,仅用于编写和仅用于 XLSX 文件,以快速和可扩展的方式,box/spout 包作为基础。
v1.1.0
2024-07-12 11:30 UTC
Requires
- php: >=7.4.0
- ext-dom: *
- ext-iconv: *
- ext-libxml: *
- ext-mbstring: *
- ext-simplexml: *
- ext-xmlreader: *
- ext-zip: *
README
PHP 库,仅用于编写和仅用于 XLSX 文件,以快速和可扩展的方式,box/spout 包作为基础。
安装
打开命令控制台,进入您的项目目录,并执行以下命令以下载此捆绑包的最新稳定版本
$ composer require threshold/php-excel
此命令需要您全局安装了 Composer,如 Composer 文档的 安装章节 所述。
使用方法
简单 XLSX 文件生成,立即下载
$writer = new \Threshold\PhpExcel\Writer\Writer(); $writer ->setShouldCreateNewSheetsAutomatically(false) ->setShouldUseInlineStrings(true) ->openToBrowser("test.xlsx"); $sheet = $writer->getCurrentSheet(); $sheet ->setName("Statistics") ->setMergeRanges(["A1:B1"]); $writer->addRow(\Threshold\PhpExcel\Writer\Factory\WriterEntityFactory::createRow([ \Threshold\PhpExcel\Writer\Factory\WriterEntityFactory::createCell( "Statistics", (new \Threshold\PhpExcel\Writer\Entity\Style\Style()) ->setCellAlignment(\Threshold\PhpExcel\Writer\Entity\Style\CellAlignment::CENTER) ->setFontBold()) ])); $writer->close();
生成并保存简单 XLSX 文件到指定位置
$writer = new \Threshold\PhpExcel\Writer\Writer(); $writer ->setShouldCreateNewSheetsAutomatically(false) ->setShouldUseInlineStrings(true) ->openToFile("path/to/save/file/test.xlsx"); $sheet = $writer->getCurrentSheet(); $sheet ->setName("Statistics") ->setMergeRanges(["A1:B1"]); $writer->addRow(\Threshold\PhpExcel\Writer\Factory\WriterEntityFactory::createRow([ \Threshold\PhpExcel\Writer\Factory\WriterEntityFactory::createCell( "Statistics", (new \Threshold\PhpExcel\Writer\Entity\Style\Style()) ->setCellAlignment(\Threshold\PhpExcel\Writer\Entity\Style\CellAlignment::CENTER) ->setFontBold()) ])); $writer->close();
递归生成 XLSX 文件,最终只返回一个文件
// In each round, you must state that you are in the last round and how many rounds there will be in total. $loopMax = $request->query->get("loopMax", 1); $loopCounter = $request->query->get("loopCounter", 1); $writer = new \Threshold\PhpExcel\Writer\Writer(); $writer ->setTempFolder("your/temp/folder/path/if/you/want") ->setShouldCreateNewSheetsAutomatically(false) ->setShouldUseInlineStrings(true) ->setDefaultRowStyle((new \Threshold\PhpExcel\Writer\Entity\Style\Style()) ->setFontName("Calibri") ->setFontSize(11)) ->setLoop($loopMax, $loopCounter); // Here you have to pass the circles, // from this it will know whether the writing will continue // or the file must be closed. // $writer->close() until the last round returns the name of the temporary folder // created in the first round in the temporary path, // this must be specified here in order to continue generating the XLSX file. if ($request->query->get("tempFolderName")) { $writer ->setTempFolderName($request->query->get("tempFolderName")); } $writer->openToFile("php://output") $sheet = $writer->getCurrentSheet(); $sheet ->setName("Statistics"); if ($loopCounter === 1) { // The merged cells are counted from the last written row in each round, // so for example the cell merging of the address only needs to be entered in the first round. // In later rounds, the "A1:B1" cell merge will not apply to the first row, for example, // if we wrote 15 rows in the previous round, then cells A and B of the 16th row will be merged. // In this case, "A1:B1" will be as if we entered "A16:B16" in the first round. $sheet->setMergeRanges(["A1:B1"]); $writer->addRow(\Threshold\PhpExcel\Writer\Factory\WriterEntityFactory::createRow([ \Threshold\PhpExcel\Writer\Factory\WriterEntityFactory::createCell("Statistics", (new \Threshold\PhpExcel\Writer\Entity\Style\Style()) ->setCellAlignment(\Threshold\PhpExcel\Writer\Entity\Style\CellAlignment::CENTER) ->setBorder((new \Threshold\PhpExcel\Writer\Entity\Style\Border()) ->setBorderTop(\Threshold\PhpExcel\Writer\Entity\Style\Color::BLACK, \Threshold\PhpExcel\Writer\Entity\Style\Border::WIDTH_MEDIUM, \Threshold\PhpExcel\Writer\Entity\Style\Border::STYLE_SOLID) ->setBorderRight(\Threshold\PhpExcel\Writer\Entity\Style\Color::BLACK, \Threshold\PhpExcel\Writer\Entity\Style\Border::WIDTH_MEDIUM, \Threshold\PhpExcel\Writer\Entity\Style\Border::STYLE_SOLID) ->setBorderBottom(\Threshold\PhpExcel\Writer\Entity\Style\Color::BLACK, \Threshold\PhpExcel\Writer\Entity\Style\Border::WIDTH_MEDIUM, \Threshold\PhpExcel\Writer\Entity\Style\Border::STYLE_SOLID) ->setBorderLeft(\Threshold\PhpExcel\Writer\Entity\Style\Color::BLACK, \Threshold\PhpExcel\Writer\Entity\Style\Border::WIDTH_MEDIUM, \Threshold\PhpExcel\Writer\Entity\Style\Border::STYLE_SOLID)) ->setFontName("Arial") ->setFontBold() ->setFontSize(20)), \Threshold\PhpExcel\Writer\Factory\WriterEntityFactory::createCell("", (new \Threshold\PhpExcel\Writer\Entity\Style\Style()) // Create empty cells with same Style to have borders ->setCellAlignment(\Threshold\PhpExcel\Writer\Entity\Style\CellAlignment::CENTER) ->setBorder((new \Threshold\PhpExcel\Writer\Entity\Style\Border()) ->setBorderTop(\Threshold\PhpExcel\Writer\Entity\Style\Color::BLACK, \Threshold\PhpExcel\Writer\Entity\Style\Border::WIDTH_MEDIUM, \Threshold\PhpExcel\Writer\Entity\Style\Border::STYLE_SOLID) ->setBorderRight(\Threshold\PhpExcel\Writer\Entity\Style\Color::BLACK, \Threshold\PhpExcel\Writer\Entity\Style\Border::WIDTH_MEDIUM, \Threshold\PhpExcel\Writer\Entity\Style\Border::STYLE_SOLID) ->setBorderBottom(\Threshold\PhpExcel\Writer\Entity\Style\Color::BLACK, \Threshold\PhpExcel\Writer\Entity\Style\Border::WIDTH_MEDIUM, \Threshold\PhpExcel\Writer\Entity\Style\Border::STYLE_SOLID) ->setBorderLeft(\Threshold\PhpExcel\Writer\Entity\Style\Color::BLACK, \Threshold\PhpExcel\Writer\Entity\Style\Border::WIDTH_MEDIUM, \Threshold\PhpExcel\Writer\Entity\Style\Border::STYLE_SOLID)) ->setFontBold()->setFontSize(20)) ])); } // We write this line in each round. In the first round it goes to cell "A2", in the second round to cell "A3" and so on. $writer->addRow(\Threshold\PhpExcel\Writer\Factory\WriterEntityFactory::createRow([ \Threshold\PhpExcel\Writer\Factory\WriterEntityFactory::createCell("Round: " . $loopCounter) ])); if (($writerResponse = $writer->close()) === true) { header('Content-Type: application/zip'); header('Content-Disposition: attachment; filename="test.xlsx"'); } else { header('Content-Type: application/json; charset=utf-8'); echo json_encode(["tempFolderName" => $writerResponse]); } exit;