fast excel writer
v6.2.0
![]() |
FastExcelWriter v.6 |
FastExcelWriter是FastExcelphp項目的一部分,由
該庫設計為輕巧,超快速,需要最少的內存使用量。
FastExcelWriter以XLSX格式創建Excel兼容電子表格(Office 2007+),並支持許多功能:
跳到:
使用composer將FastExcelWriter安裝到您的項目中:
composer require avadim/fast-excel-writer
Sheet::setRowOptions() , Sheet::setColOptions() , Sheet::setRowStyles()和Sheet::setColStyles()已棄用,而不是它們應該使用其他功能: setRowStyle() , setRowStyleArray() , setRowDataStyle() , setRowDataStyleArray() setColStyle() setColStyleArray() , setColDataStyle() , setColDataStyleArray()Sheet::setRowStyle()和Sheet::setColStyle()已更改,現在它們為整個行或列設置了樣式(即使它們為空) 在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 您可以在下面或在/演示文件夾中找到使用示例
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 -> 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()向任何單元格添加註釋
$ sheet1 -> writeCell ( ' Text to A1 ' );
$ sheet1 -> addNote ( ' A1 ' , ' This is a note for cell A1 ' );
$ sheet1 -> writeCell ( ' Text to B1 ' )-> addNote ( ' This is a note for B1 ' );
$ sheet1 -> 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
$ sheet1 -> addNote ( ' E4:F8 ' , " This note n will added to E4 " );
// You can split text into multiple lines
$ sheet1 -> addNote ( ' D7 ' , " Line 1 n Line 2 " );您可以更改一些註釋選項。註釋的允許選項是:
'96pt''55.5pt''#FFFFE1'false $ sheet1 -> 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'
];
$ sheet1 -> writeCell ( ' Text to B1 ' )-> addNote ( ' This is a note for B1 ' , $ noteStyle );
// This note is visible when the Excel workbook is displayed
$ sheet1 -> addNote ( ' C8 ' , ' This note is always visible ' , [ ' show ' => true ]);另外,您可以在筆記中使用豐富的文字
$ richText = new avadim FastExcelWriter RichText ( ' here is <c=f00>red</c> and <c=00f>blue</c> text ' );
$ sheet1 -> addNote ( ' C8 ' , $ richText );有關使用豐富文本的更多信息,請參見此處:使用豐富的文本
您可以在base64中將圖像插入到本地文件,URL或圖像字符串中
// Insert an image to the cell A1 from local path
$ sheet1 -> addImage ( ' A1 ' , ' path/to/file ' );
// Insert an image to the cell A1 from URL
$ sheet1 -> addImage ( ' A1 ' , ' https://site.com/image.jpg ' );
// Insert an image to the cell A1 from base64 string
$ sheet1 -> 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)
$ sheet1 -> addImage ( ' B2 ' , ' path/to/file ' , [ ' width ' => 150 ]);
// Set height to 150 pixels (with will change proportionally)
$ sheet1 -> addImage ( ' C3 ' , ' path/to/file ' , [ ' height ' => 150 ]);
// Set size in pixels
$ sheet1 -> addImage ( ' D4 ' , ' path/to/file ' , [ ' width ' => 150 , ' height ' => 150 ]);
// Add hyperlink to the image
$ sheet1 -> addImage ( ' D4 ' , ' path/to/file ' , [ ' width ' => 150 , ' height ' => 150 , ' hyperlink ' => ' https://www.google.com/ ' ]);默認情況下,字符串直接寫入床單。這會增加文件大小,但會加快數據編寫並節省內存。如果要將字符串寫入共享字符串XML,則需要使用“共享_String”選項。
$ excel = Excel:: create ([], [ ' shared_string ' => true ]);Phpspreadsheet是一個完美的庫,具有閱讀和編寫許多文檔格式的精彩功能。 FastExcelWriter只能寫入XLSX格式,但是它的內存使用量很快,並且使用最少。
FastExcelWriter :
phpspreadsheet(P)和FastExcelWriter(F)的基準,不帶樣式的電子表格生成
| 行X Cols | 時間p | 時間f | 記憶p | 內存f |
|---|---|---|---|---|
| 1000 x 5 | 0.98秒 | 0.19秒 | 2,048 kb | 2,048 kb |
| 1000 x 25 | 4.68秒 | 1.36秒 | 14,336 kb | 2,048 kb |
| 5000 x 25 | 23.19秒 | 3.61秒 | 77,824 kb | 2,048 kb |
| 10000 x 50 | 105.8秒 | 13.02秒 | 256,000 kb | 2,048 kb |
如果您發現此包裝有用,則可以支持我喝杯咖啡:
或者只是在github上給我一顆星星:)