![]() |
FastExcelWriter v.6 |
Fastexcelwriter 는 FastexcelPhp 프로젝트 의 일부입니다.
이 라이브러리는 가볍고 초고속으로 설계되었으며 최소한의 메모리 사용이 필요합니다.
FastexcelWriter는 XLSX 형식 (Office 2007+)으로 Excel 호환 스프레드 시트를 생성하며 많은 기능이 지원됩니다.
점프 :
composer 사용하여 FastexcelWriter를 프로젝트에 설치하십시오.
composer require avadim/fast-excel-writer
Sheet::setRowOptions() , Sheet::setColOptions() , Sheet::setRowStyles() 및 setrowstyles () 및 Sheet::setColStyles() 더 이상 사용되지 않으며, 다른 함수를 사용해야합니다 : setRowStyle() , setRowStyleArray() , setrowdatastyle (), setRowDataStyleArray() setRowDataStyle() setrowdatylearray (), 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의 구식 주석입니다 (밝은 노란색 배경의 텍스트). Method 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 |
이 패키지가 유용하다고 생각되면 커피 한 잔을 지원하고 기부 할 수 있습니다.
아니면 그냥 나에게 Github에 별을 줘 :)