fast excel writer

其他类别 2025-08-20


FastExcelWriter v.6

FastExcelWriterFastExcelphp项目的一部分,由

  • FastExcelWriter-创建Excel电子表格
  • FastExcelReader-阅读Excel电子表格
  • FastExCeltemplator-生成XLSX模板的Excel电子表格
  • FastExcellaravel-特殊Laravel Edition

介绍

该库设计为轻巧,超快速,需要最少的内存使用量。

FastExcelWriter以XLSX格式创建Excel兼容电子表格(Office 2007+),并支持许多功能:

  • 获取UTF-8编码输入
  • 多个工作表
  • 支持货币/日期/数字单元格式,公式和主动超链接
  • 支持大多数用于细胞,行,列 - 颜色,边框,字体等的样式选项。
  • 您可以设置行的高度和列的宽度(包括自动宽度计算)
  • 您可以在XLSX文件中添加公式,笔记和图像
  • 支持使用/没有密码的工作簿和表格保护
  • 支持页面设置 - 页面边距,页面大小
  • 插入多个图表
  • 支持数据验证和条件格式

跳到:

  • 版本6的更改
    • 版本6.1的重要更改
  • 版本5的更改
    • 版本5.8的重要更改
  • 简单示例
  • 高级示例
  • 添加笔记
  • 添加图像
  • 工作簿
    • 工作簿设置
    • 设置工作簿的元数据
    • 为临时文件设置目录
    • 帮助者方法
  • 床单
    • 创建,选择和删除表
    • 表设置
    • 页面设置
    • 行的设置
    • 列的设置
    • 自动列宽度
    • 组/轮廓行和列
    • 定义名称范围
    • 冷冻窗格和自动滤器
    • 设置活跃的纸张和单元格
    • 打印设置
  • 写作
    • 用第与直接编写行
    • 直接写入细胞
    • 编写单元格值
    • 合并细胞
    • 细胞格式
    • 公式
    • 超链接
    • 使用丰富的文字
  • 样式
    • 样式属性作为数组
    • 定义样式属性的班级样式
    • 细胞样式
    • 行样式
    • 列样式
    • 其他列选项
    • 应用样式(流利的界面)
    • 应用边界
    • 应用字体
    • 应用颜色
    • 应用文字样式
  • 图表
    • 简单用法
    • 组合图
    • 多个图表
    • 图表类型
    • 有用的图表方法
  • 保护工作簿和床单
    • 工作簿保护
    • 保护工作表
    • 细胞锁定/解锁
  • 数据验证
    • 简单用法
    • 定义过滤器
    • 检查价值类型
    • 自定义过滤器
    • 所有数据验证设置
    • 超过64K验证规则
  • 条件格式
    • 简单用法
    • 一般细胞值检查
    • 表达
    • 梯度填充,具体取决于值(颜色等级)
    • 单元格内的数据带(数据库)
  • API参考
  • FastExcelWriter vs phpspreadsheet
  • 您想支持FastExcelWriter吗?

安装

使用composerFastExcelWriter安装到您的项目中:

 composer require avadim/fast-excel-writer

版本6的更改

  • 数据验证支持

版本6.1的重要更改

  • Sheet::setRowOptions()Sheet::setColOptions()Sheet::setRowStyles()Sheet::setColStyles()已弃用,而不是它们应该使用其他功能: setRowStyle()setRowStyleArray()setRowDataStyle()setRowDataStyleArray() setColStyle() setColStyleArray()setColDataStyle()setColDataStyleArray()
  • Sheet::setRowStyle()Sheet::setColStyle()已更改,现在它们为整个行或列设置了样式(即使它们为空)

版本5的更改

  • 一般新闻是图表支持

版本5.8的重要更改

在v.5.8之前

 $ sheet -> writeCell ( 12345 ); // The number 12345 will be written into the cell
$ sheet -> writeCell ( ' 12345 ' ); // The number 12345 will also be written here

在版本5.8及以后

 $ sheet -> writeCell ( 12345 ); // The number 12345 will be written into the cell
$ sheet -> writeCell ( ' 12345 ' ); // Here the string '12345' will be written into the cell

如果要保留以前的向后兼容性行为,则在创建工作簿时应使用选项“ auto_convert_number”。

 $ excel = Excel:: create ([ ' Sheet1 ' ], [ ' auto_convert_number ' => true ]);
$ sheet = $ excel -> sheet ();
$ sheet -> writeCell ( ' 12345 ' ); // String '12345' will be automatically converted to a number 

版本4的更改

  • 现在图书馆的工作速度更快
  • 添加了一个流利的界面以应用样式。
  • 新方法和代码重构

用法

您可以在下面或在/演示文件夹中找到使用示例

简单示例

 use  avadim  FastExcelWriter  Excel ;

$ data = [
    [ ' 2003-12-31 ' , ' James ' , ' 220 ' ],
    [ ' 2003-8-23 ' , ' Mike ' , ' 153.5 ' ],
    [ ' 2003-06-01 ' , ' John ' , ' 34.12 ' ],
];

$ excel = Excel:: create ([ ' Sheet1 ' ]);
$ sheet = $ excel -> sheet ();

// Write heads
$ sheet -> writeRow ([ ' Date ' , ' Name ' , ' Amount ' ]);

// Write data
foreach ( $ data as $ rowData ) {
    $ rowOptions = [
        ' height ' => 20 ,
    ];
    $ sheet -> writeRow ( $ rowData , $ rowOptions );
}

$ excel -> save ( ' simple.xlsx ' );

另外,您可以将生成的文件下载到客户端(发送到浏览器)

 $ excel = Excel:: create ();
$ sheet = $ excel -> sheet ();

$ sheet -> writeCell ( 12345 ); // write integer
$ sheet -> writeCell ( 123.45 ); // write float
$ sheet -> writeCell ( ' 12345 ' ); // write string
$ sheet -> writeCell ( true ); // write boolean value
$ sheet -> writeCell ( fn () => $ sheet -> getCurrentCell ()); // write result of function

$ excel -> download ( ' download.xlsx ' );

高级示例

 use  avadim  FastExcelWriter  Excel ;

$ head = [ ' Date ' , ' Name ' , ' Amount ' ];
$ data = [
    [ ' 2003-12-31 ' , ' James ' , ' 220 ' ],
    [ ' 2003-8-23 ' , ' Mike ' , ' 153.5 ' ],
    [ ' 2003-06-01 ' , ' John ' , ' 34.12 ' ],
];
$ headStyle = [
    ' font ' => [
        ' style ' => ' bold '
    ],
    ' text-align ' => ' center ' ,
    ' vertical-align ' => ' center ' ,
    ' border ' => ' thin ' ,
    ' height ' => 24 ,
];

$ excel = Excel:: create ([ ' Sheet1 ' ]);
$ sheet = $ excel -> sheet ();

// Write the head row (sets style via array)
$ sheet -> writeHeader ( $ head , $ headStyle );

// The same result with new fluent interface
$ sheet -> writeHeader ( $ head )
    -> applyFontStyleBold ()
    -> applyTextAlign ( ' center ' , ' center ' )
    -> applyBorder (Style:: BORDER_STYLE_THIN )
    -> applyRowHeight ( 24 );

// Sets columns options - format and width (the first way)
$ sheet
    -> setColFormats ([ ' @date ' , ' @text ' , ' 0.00 ' ])
    -> setColWidths ([ 12 , 14 , 5 ]);

// The seconds way to set columns options
$ sheet
    // column and options
    -> setColDataStyle ( ' A ' , [ ' format ' => ' @date ' , ' width ' => 12 ])
    // column letter in lower case
    -> setColDataStyle ( ' b ' , [ ' format ' => ' @text ' , ' width ' => 24 ])
    // column can be specified by number
    -> setColDataStyle ( 3 , [ ' format ' => ' 0.00 ' , ' width ' => 15 , ' color ' => ' #090 ' ])
;

// The third way - all options in multilevel array (first level keys point to columns)
$ sheet
    -> setColDataStyle ([
        ' A ' => [ ' format ' => ' @date ' , ' width ' => 12 ],
        ' B ' => [ ' format ' => ' @text ' , ' width ' => 24 ],
        ' C ' => [ ' format ' => ' 0.00 ' , ' width ' => 15 , ' color ' => ' #090 ' ],
    ]);

$ rowNum = 1 ;
foreach ( $ data as $ rowData ) {
    $ rowOptions = [
        ' height ' => 20 ,
    ];
    if ( $ rowNum % 2 ) {
        $ rowOptions [ ' fill-color ' ] = ' #eee ' ;
    }
    $ sheet -> writeRow ( $ rowData , $ rowOptions );
}

$ excel -> save ( ' simple.xlsx ' );

添加笔记

Excel和注释中当前有两种评论类型(请参阅线程注释和注释之间的区别)。注释是Excel中的旧样式评论(在浅黄色背景上的文字)。您可以使用方法addNote()向任何单元格添加注释

addNote('D7', "Line 1nLine 2"); ">
 $ sheet -> writeCell ( ' Text to A1 ' );
$ sheet -> addNote ( ' A1 ' , ' This is a note for cell A1 ' );

$ sheet -> writeCell ( ' Text to B1 ' )-> addNote ( ' This is a note for B1 ' );
$ sheet -> writeTo ( ' C4 ' , ' Text to C4 ' )-> addNote ( ' Note for C1 ' );

// If you specify a range of cells, then the note will be added to the left top cell
$ sheet -> addNote ( ' E4:F8 ' , " This note n will added to E4 " );

// You can split text into multiple lines
$ sheet -> addNote ( ' D7 ' , " Line 1 n Line 2 " );

您可以更改一些注释选项。注释的允许选项是:

  • 宽度- 默认值为'96pt'
  • 高度- 默认值是'55.5pt'
  • fill_color-默认值为'#FFFFE1'
  • 显示- 默认值为false
200, // equivalent to '200pt' 'height' => 100, // equivalent to '100pt' 'fill_color' => 'fcc', // equivalent to '#ffcccc' ]; $sheet->writeCell('Text to B1')->addNote('This is a note for B1', $noteStyle); // This note is visible when the Excel workbook is displayed $sheet->addNote('C8', 'This note is always visible', ['show' => true]);">
 $ sheet -> addNote ( ' A1 ' , ' This is a note for cell A1 ' , 
    [ ' width ' => ' 200pt ' , ' height ' => ' 100pt ' , ' fill_color ' => ' #ffcccc ' ]);

// Parameters "width" and "height" can be numeric, by default these values are in points
// The "fill_color" parameter can be shortened
$ noteStyle = [
    ' width ' => 200 , // equivalent to '200pt'
    ' height ' => 100 , // equivalent to '100pt'
    ' fill_color ' => ' fcc ' , // equivalent to '#ffcccc'
];
$ sheet -> writeCell ( ' Text to B1 ' )-> addNote ( ' This is a note for B1 ' , $ noteStyle );

// This note is visible when the Excel workbook is displayed
$ sheet -> addNote ( ' C8 ' , ' This note is always visible ' , [ ' show ' => true ]);

另外,您可以在笔记中使用丰富的文字

 $ richText = new  avadim  FastExcelWriter  RichText ( ' here is red and blue text ' );
$ sheet -> addNote ( ' C8 ' , $ richText );

有关使用丰富文本的更多信息,请参见此处:使用丰富的文本

添加图像

您可以在base64中将图像插入到本地文件,URL或图像字符串中

 $ sheet -> addImage ( $ cell , $ imageFile , $ imageStyle );

// Insert an image to the cell A1 from local path
$ sheet -> addImage ( ' A1 ' , ' path/to/file ' );

// Insert an image to the cell A1 from URL
$ sheet -> addImage ( ' A1 ' , ' https://*sit*e.com*/image.jpg ' );

// Insert an image to the cell A1 from base64 string
$ sheet -> addImage ( ' A1 ' , ' data:image/jpeg;base64,/9j/4AAQ... ' );

// Insert an image to the cell B2 and set with to 150 pixels (height will change proportionally)
$ sheet -> addImage ( ' B2 ' , ' path/to/file ' , [ ' width ' => 150 ]);

// Set height to 150 pixels (with will change proportionally)
$ sheet -> addImage ( ' C3 ' , ' path/to/file ' , [ ' height ' => 150 ]);

// Set size in pixels
$ sheet -> addImage ( ' D4 ' , ' path/to/file ' , [ ' width ' => 150 , ' height ' => 150 ]);

// Add hyperlink to the image
$ sheet -> addImage ( ' D4 ' , ' path/to/file ' , [ ' width ' => 150 , ' height ' => 150 , ' hyperlink ' => ' https://www.g**oog*le.com/ ' ]);

图像样式的可用键:

  • “宽度” - 图像的宽度
  • “高度” - 图像高度
  • “超链接” - 超链接的URL
  • 'x' - 相对于单元格的左边界相对于像素的偏移
  • 'y' - 相对于单元格的顶部边界相对于像素的偏移

重要:在MS Excel中,值“ X”不能大于亲属单元的列宽度,并且值“ y”不能大于行高度

共享字符串

默认情况下,字符串直接写入床单。这会增加文件大小,但会加快数据编写并节省内存。如果要将字符串写入共享字符串XML,则需要使用“共享_String”选项。

 $ excel = Excel:: create ([], [ ' shared_string ' => true ]);

FastExcelWriter vs phpspreadsheet

Phpspreadsheet是一个完美的库,具有阅读和编写许多文档格式的精彩功能。 FastExcelWriter只能写入XLSX格式,但是它的内存使用量很快,并且使用最少。

FastExcelWriter

  • 7-9倍更快
  • 使用8-10次使用的记忆更少
  • 支持编写大型100k+行电子表格

Phpspreadsheet的基准(无样式的生成)

行X Cols 时间 记忆
1000 x 5 0.98秒 2,048 kb
1000 x 25 4.68秒 14,336 kb
5000 x 25 23.19秒 77,824 kb
10000 x 50 105.8秒 256,000 kb

FastExcelwriter的基准(无样式的生成)

行X Cols 时间 记忆
1000 x 5 0.19秒 2,048 kb
1000 x 25 1.36秒 2,048 kb
5000 x 25 3.61秒 2,048 kb
10000 x 50 13.02秒 2,048 kb

您想支持FastExcelWriter吗?

如果您发现此包装有用,则可以支持我喝杯咖啡:

  • USDT(TRC20)tssufvjehqbjckeygnnr1cpswy6jznbzk7
  • USDT(ERC20)0x5244519D65035AF868A010C2F68A086F473FC82B
  • ETH 0x5244519D65035AF868A010C2F68A08A086F473FC82B

或者只是在github上给我一颗星星:)

下载源码

通过命令行克隆项目:

git clone https://github.com/aVadim483/fast-excel-writer.git