22namespace Cyberduck \LaravelExcel \Exporter ;
33
44use Illuminate \Support \Collection ;
5- use Box \Spout \Writer \WriterFactory ;
5+ use Box \Spout \Writer \Common \ Creator \ WriterEntityFactory ;
66use Illuminate \Database \Query \Builder ;
77use Cyberduck \LaravelExcel \Serialiser \BasicSerialiser ;
88use Cyberduck \LaravelExcel \Contract \SerialiserInterface ;
@@ -56,6 +56,8 @@ public function setSerialiser(SerialiserInterface $serialiser)
5656
5757 abstract public function getType ();
5858
59+ abstract public function createWriter ();
60+
5961 public function save ($ filename )
6062 {
6163 $ writer = $ this ->create ();
@@ -74,7 +76,7 @@ public function stream($filename)
7476
7577 protected function create ()
7678 {
77- $ writer = WriterFactory:: create ( $ this ->type );
79+ $ writer = $ this ->createWriter ( );
7880 $ this ->callbacks ->each (function ($ elem ) use (&$ writer ) {
7981 call_user_func_array (array ($ writer , $ elem [0 ]), $ elem [1 ]);
8082 });
@@ -85,22 +87,27 @@ protected function makeRows($writer)
8587 {
8688 $ headerRow = $ this ->serialiser ->getHeaderRow ();
8789 if (!empty ($ headerRow )) {
88- $ writer ->addRow ($ headerRow );
90+ $ row = WriterEntityFactory::createRowFromArray ($ headerRow );
91+ $ writer ->addRow ($ row );
8992 }
9093 if ($ this ->data instanceof Builder) {
9194 if (isset ($ this ->chuncksize )) {
9295 $ this ->data ->chunk ($ this ->chuncksize );
9396 } else {
94- $ data = $ this ->data ->get ();
95- foreach ($ data as $ record ) {
96- $ writer ->addRow ($ this ->serialiser ->getData ($ record ));
97- }
97+ $ this ->addRowsDataToWriter ($ this ->data ->get (), $ writer );
9898 }
9999 } else {
100- foreach ($ this ->data as $ record ) {
101- $ writer ->addRow ($ this ->serialiser ->getData ($ record ));
102- }
100+ $ this ->addRowsDataToWriter ($ this ->data , $ writer );
103101 }
104102 return $ writer ;
105103 }
104+
105+ public function addRowsDataToWriter ($ data , $ writer )
106+ {
107+ foreach ($ data as $ record ) {
108+ $ recordData = $ this ->serialiser ->getData ($ record );
109+ $ row = WriterEntityFactory::createRowFromArray ($ recordData );
110+ $ writer ->addRow ($ row );
111+ }
112+ }
106113}
0 commit comments