box/spout使用记录

box/spout使用记录spout 升级命名 3 0 后变动较多 且官网挂了 记录下文档包地址 https packagist org packages box spoutgithub 地址 https github com box spoutcompose 地址 composerrequ spout 读写 包空间有变动 只保留了部分示例性代码 useBox Spout Reader

spout 升级命名3.0后变动较多,且官网挂了,记录下文档

包地址 https://packagist.org/packages/box/spout github地址 https://github.com/box/spout composer地址 composer require box/spout 

读写

//包空间有变动 ,只保留了部分示例性代码 use Box\Spout\Reader\Common\Creator\ReaderEntityFactory; use Box\Spout\Writer\Common\Creator\WriterEntityFactory; / * @param $filename 文件名 string * @param $admin 用户名 string * @param $headerValues 头标题 [] * @param $cellValues 表数据 [[],[]] * @return mixed */ public function createCSV($filename,$admin,$headerValues,$cellValues) { list($savePath,$salPath) = $this->createFile($filename,$admin); $writer = WriterEntityFactory::createCSVWriter(); $writer->openToFile($savePath); WriterEntityFactory::createWriterFromFile('.csv'); //这个单元格有自己的样式 // $cell1 = WriterEntityFactory::createCell('foowahhh',$cellStyle1); // $cell2 = WriterEntityFactory::createCell(,$cellStyle2); // $row2 = WriterEntityFactory::createRow([$cell1,$cell2]); $header = WriterEntityFactory::createRowFromArray($headerValues); $writer->addRows([$header]); foreach($cellValuesas $value){ $row = WriterEntityFactory::createRowFromArray($value); $writer->addRows([$row]); } $writer->close(); return $salPath; } public function createFile($fileName,$admin_nickname) { //返回相对路径和绝对路径 return [$savePath,$salPath]; } public function createXLSX() { $writer = WriterEntityFactory::createXLSXWriter(); } public function createODS() { $writer = WriterEntityFactory::createODSWriter(); } public function readCSV() { $reader = ReaderEntityFactory::createCSVReader(); } public function readXLSX() { $reader = ReaderEntityFactory::createXLSXReader(); } public function readODS() { $reader = ReaderEntityFactory::createODSReader(); } 

示例-样式-styles

文档 https://github.com/box/spout/tree/master/docs

//包空间有变动 use Box\Spout\Writer\Common\Creator\Style\StyleBuilder use Box\Spout\Writer\Common\Creator\Style\BorderBuilder use Box\Spout\Common\Entity\Style\Border use Box\Spout\Common\Entity\Style\BorderPart use Box\Spout\Common\Entity\Style\Color use Box\Spout\Common\Entity\Style\Style //支持字体,背景,边框以及对齐样式。 $cellStyle1 = (new StyleBuilder()) ->setFontBold() ->setFontSize(15) ->setFontColor(Color::BLUE) ->setShouldWrapText() ->setBackgroundColor(Color::YELLOW) ->build(); //向行添加边框需要一个Border对象 $border = (new BorderBuilder()) ->setBorderBottom(Color::GREEN, Border::WIDTH_THIN, Border::STYLE_DASHED) ->build(); $style = (new StyleBuilder()) ->setBorder($border) ->build(); //Spout将为所有创建的行使用默认样式。可以通过以下方式覆盖此样式: $defaultStyle = (new StyleBuilder()) ->setFontName('Arial') ->setFontSize(11) ->build(); 

部分文档内容

Category Property API
Font Bold StyleBuilder::setFontBold()
Italic StyleBuilder::setFontItalic()
Underline StyleBuilder::setFontUnderline()
Strikethrough StyleBuilder::setFontStrikethrough()
Font name StyleBuilder::setFontName(‘Arial’)
Font size StyleBuilder::setFontSize(14)
Font color StyleBuilder::setFontColor(Color::BLUE)
StyleBuilder::setFontColor(Color::rgb(0, 128, 255))

Alignment Wrap text `StyleBuilder::setShouldWrapText(true

New sheet creation

It is also possible to change the behavior of the writer when the maximum number of rows (1,048,576) have been written in the current sheet:

use Box\Spout\Writer\WriterFactory; use Box\Spout\Common\Type; $writer = WriterFactory::create(Type::ODS); $writer->setShouldCreateNewSheetsAutomatically(true); // default value $writer->setShouldCreateNewSheetsAutomatically(false); // will stop writing new data when limit is reached 

Using custom temporary folder

Processing XLSX and ODS files require temporary files to be created. By default, Spout will use the system default temporary folder (as returned by sys_get_temp_dir()). It is possible to override this by explicitly setting it on the reader or writer:

use Box\Spout\Writer\WriterFactory; use Box\Spout\Common\Type; $writer = WriterFactory::create(Type::XLSX); $writer->setTempFolder($customTempFolderPath); 

Strings storage (XLSX writer)

XLSX files support different ways to store the string values:

  • Shared strings are meant to optimize file size by separating strings from the sheet representation and ignoring strings duplicates (if a string is used three times, only one string will be stored)
  • Inline strings are less optimized (as duplicate strings are all stored) but is faster to process

In order to keep the memory usage really low, Spout does not optimize strings when using shared strings. It is nevertheless possible to use this mode.

use Box\Spout\Writer\WriterFactory; use Box\Spout\Common\Type; $writer = WriterFactory::create(Type::XLSX); $writer->setShouldUseInlineStrings(true); // default (and recommended) value $writer->setShouldUseInlineStrings(false); // will use shared strings 

Date/Time formatting

use Box\Spout\Reader\ReaderFactory; use Box\Spout\Common\Type; $reader = ReaderFactory::create(Type::XLSX); $reader->setShouldFormatDates(false); // default value $reader->setShouldFormatDates(true); // will return formatted dates 

Playing with sheets

When creating a XLSX or ODS file, it is possible to control which sheet the data will be written into. At any time, you can retrieve or set the current sheet:

$firstSheet = $writer->getCurrentSheet(); $writer->addRow($rowForSheet1); // writes the row to the first sheet $newSheet = $writer->addNewSheetAndMakeItCurrent(); $writer->addRow($rowForSheet2); // writes the row to the new sheet $writer->setCurrentSheet($firstSheet); $writer->addRow($anotherRowForSheet1); // append the row to the first sheet 

It is also possible to retrieve all the sheets currently created:

$sheets = $writer->getSheets();

If you rely on the sheet’s name in your application, you can access it and customize it this way:

// Accessing the sheet name when reading foreach ($reader->getSheetIterator() as $sheet) { $sheetName = $sheet->getName(); } // Accessing the sheet name when writing $sheet = $writer->getCurrentSheet(); $sheetName = $sheet->getName(); // Customizing the sheet name when writing $sheet = $writer->getCurrentSheet(); $sheet->setName('My custom name'); 

Please note that Excel has some restrictions on the sheet’s name:

  • it must not be blank
  • it must not exceed 31 characters
  • it must not contain these characters: \ / ? * : [ or ]
  • it must not start or end with a single quote
  • it must be unique

Handling these restrictions is the developer’s responsibility. Spout does not try to automatically change the sheet’s name, as one may rely on this name to be exactly what was passed in.

Fluent interface

Because fluent interfaces are great, you can use them with Spout:

use Box\Spout\Writer\WriterFactory; use Box\Spout\Common\Type; $writer = WriterFactory::create(Type::XLSX); $writer->setTempFolder($customTempFolderPath) ->setShouldUseInlineStrings(true) ->openToFile($filePath) ->addRow($headerRow) ->addRows($dataRows) ->close(); 

Running tests

On the master branch, only unit and functional tests are included. The performance tests require very large files and have been excluded.

If you just want to check that everything is working as expected, executing the tests of the master branch is enough.

If you want to run performance tests, you will need to checkout the perf-tests branch. Multiple test suites can then be run, depending on the expected output:

  • phpunit – runs the whole test suite (unit + functional + performance tests)
  • phpunit –exclude-group perf-tests – only runs the unit and functional tests
  • phpunit –group perf-tests – only runs the performance tests

For information, the performance tests take about 30 minutes to run (processing 1 million rows files is not a quick thing).

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/212975.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午6:50
下一篇 2026年3月18日 下午6:50


相关推荐

  • 15个Cursor AI功能,让你省去50%的编码时间!

    15个Cursor AI功能,让你省去50%的编码时间!

    2026年3月16日
    2
  • 数字信封工作原理

    数字信封工作原理数字信封是指发送方使用接收方的公钥来加密对称密钥后所得的数据 其目的是用来确保对称密钥传输的安全性 采用数字信封时 接收方需要使用自己的私钥才能打开数字信封得到对称密钥 nbsp nbsp nbsp 数字信封的加 解密过程如图 1 19 所示 甲也要事先获得乙的公钥 具体说明如下 对应图中的数字序号 图 1 19 nbsp 数字信封的加解密过程示意图 1 甲使用对称密钥对明文进行加密 生成密文信息 2

    2026年3月19日
    3
  • 大数据——Java 知识点整理

    大数据——Java 知识点整理1 JDK 和 JRE 有什么区别 JDK JavaDevelopm 的简称 java 开发工具包 提供了 java 的开发环境和运行环境 JRE JavaRuntimeE 的简称 java 运行环境 为 java 的运行提供了所需环境 具体来说 JDK 其实包含了 JRE 同时还包含了编译 java 源码的编译器 javac 还包含了许多 java 程序调试和分析的工具 要运行 java 程序 只需要安装 JRE 就可以了 如果需要编写 java 程序 则还需要安装 JDK 2 java

    2026年3月26日
    2
  • 清明梦超能力者黄YY(idx数组)

    清明梦超能力者黄YY(idx数组)清明梦超能力者黄YYhttps://www.nowcoder.com/acm/contest/206/I题目描述黄YY是一个清明梦超能力者,同时也是一个记忆大师。他能够轻松控制自己在梦中的一切,

    2022年7月1日
    35
  • c语言角度换成弧度的编码,角度弧度转换代码

    c语言角度换成弧度的编码,角度弧度转换代码搜索热词下面是编程之家 jb51 cc 通过网络收集整理的代码片段 编程之家小编现在分享给大家 也给大家做个参考 defd2r puts Enterthevalu t gets chomp to f correspondin 3 14 t 180puts Thevalueofgi

    2026年3月26日
    2
  • H3C交换机配置命令大全【转载】[通俗易懂]

    H3C交换机配置命令大全【转载】[通俗易懂]杭州华三通信技术有限公司(简称H3C),致力于IP技术与产品的研究、开发、生产、销售及服务。H3C不但拥有全线路由器和以太网交换机产品,还在网络安全、IP存储、IP监控、语音视讯、WLAN、SOHO及软件管理系统等领域稳健成长。在以太网领域,H3C经历多年的耕耘和发展,积累了大量业界领先的知识产权和专利,可提供业界从核心到接入10多个系列上百款交换机产品。所有产品全部运行H3C自主知识产权的…

    2022年6月20日
    33

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号