![]() |
FastExcelWriter v.6 |
fastexcelwriterは、 fastexcelphpプロジェクトの一部です。
このライブラリは、軽量で超高速になるように設計されており、メモリの使用量を最小限に抑えます。
FastExcelWriterは、XLSX形式(Office 2007+)でExcel互換性のあるスプレッドシートを作成し、多くの機能がサポートされています。
ジャンプ:
composerを使用して、 FastExcelWriterをプロジェクトにインストールします。
composer require avadim/fast-excel-writer
Sheet::setRowOptions() 、 Sheet::setColOptions() 、Sheet :: Sheet::setRowStyles() setRowStyle() setRowStyleArray() 、 setRowDataStyle() Sheet::setColStyles() 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 以下またはin /demoフォルダーの使用例を見つけることができます
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には2種類のコメントがあります -コメントとメモ(スレッドコメントとメモの違いを参照)。メモは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に書き込みたい場合は、「shared_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 |
このパッケージが便利だと思う場合は、コーヒーを1杯サポートして寄付できます。
または、Githubで星をください:)