@@ -78,86 +78,44 @@ public function actionCsv()
7878 $ fields = $ this ->getFieldsKeys ($ searchModel ->exportFields ());
7979 $ csvCharset = \Yii::$ app ->request ->post ('csvCharset ' );
8080
81- if ( $ csvCharset == ' Windows-1251 ' ) {
82- header ('Pragma: public ' );
83- header ('Expires: 0 ' );
84- header ('Cache-Control: must-revalidate, post-check=0, pre-check=0 ' );
85- header ('Content-Description: File Transfer ' );
86- header ( ' Content-Type: text/ csv' ) ;
87- $ filename = $ tableName . " .csv " ;
88- header ('Content-Disposition: attachment;filename= ' . $ filename );
89- header ( ' Content-Transfer-Encoding: binary ' );
81+ header ( ' Pragma: public ' );
82+ header ('Expires: 0 ' );
83+ header ('Cache-Control: must-revalidate, post-check=0, pre-check= 0 ' );
84+ header ('Content-Description: File Transfer ' );
85+ header ('Content-Type: text/csv ' );
86+ $ filename = $ tableName . " . csv" ;
87+ header ( ' Content-Disposition: attachment; filename= ' . $ filename ) ;
88+ header ('Content-Transfer-Encoding: binary ' );
89+ $ fp = fopen ( ' php://output ' , ' w ' );
9090
91- $ fp = fopen ( ' php://output ' , ' w ' );
91+ fputs ( $ fp , $ bom =( chr ( 0xEF ) . chr ( 0xBB ) . chr ( 0xBF ) ) );
9292
93- fputs ($ fp , $ bom =( chr (0xEF ) . chr (0xBB ) . chr (0xBF ) ));
94- if ($ fp )
95- {
96- echo '<br>1<br> ' ;
97- $ items = [];
98- $ i = 0 ;
99- foreach ($ fields as $ one ) {
100- $ items [$ i ] = iconv ('utf-8 ' , 'windows-1251 ' , $ searchModel ->getAttributeLabel ($ one ));
101- $ i ++;
102- }
103- fputcsv ($ fp , $ items , ", " , "' " );
104- $ items = [];
105- $ i = 0 ;
106- foreach ($ dataProvider ->getModels () as $ model ) {
107- foreach ($ searchModel ->exportFields () as $ one ) {
108- if (is_string ($ one )) {
109- $ items [$ i ] = iconv ('utf-8 ' , 'windows-1251 ' , $ model [$ one ]);
110- } else {
111- $ items [$ i ] = iconv ('utf-8 ' , 'windows-1251 ' , $ one ($ model ));
112- }
113- $ i ++;
114- }
115- fputcsv ($ fp , $ items , ", " , "' " );
116- $ items = [];
117- $ i = 0 ;
118- }
93+ if ($ fp )
94+ {
95+ $ items = [];
96+ $ i = 0 ;
97+ foreach ($ fields as $ one ) {
98+ $ items [$ i ] = $ one ;
99+ $ i ++;
119100 }
120- fclose ($ fp );
121- } else {
122- header ('Pragma: public ' );
123- header ('Expires: 0 ' );
124- header ('Cache-Control: must-revalidate, post-check=0, pre-check=0 ' );
125- header ('Content-Description: File Transfer ' );
126- header ('Content-Type: text/csv ' );
127- $ filename = $ tableName .".csv " ;
128- header ('Content-Disposition: attachment;filename= ' .$ filename );
129- header ('Content-Transfer-Encoding: binary ' );
130-
131- $ fp = fopen ('php://output ' , 'w ' );
132-
133- fputs ($ fp , $ bom =( chr (0xEF ) . chr (0xBB ) . chr (0xBF ) ));
134- if ($ fp )
135- {
136- $ items = [];
137- $ i = 0 ;
138- foreach ($ fields as $ one ) {
139- $ items [$ i ] = $ one ;
101+ fputs ($ fp , implode ($ items , '; ' )."\n" );
102+ $ items = [];
103+ $ i = 0 ;
104+ foreach ($ dataProvider ->getModels () as $ model ) {
105+ foreach ($ searchModel ->exportFields () as $ one ) {
106+ if (is_string ($ one )) {
107+ $ items [$ i ] = $ model [$ one ];
108+ } else {
109+ $ items [$ i ] = $ one ($ model );
110+ }
140111 $ i ++;
141112 }
142- fputcsv ($ fp , $ items , " ; " );
113+ fputs ($ fp , implode ( $ items , ' ; ' ). "\n " );
143114 $ items = [];
144115 $ i = 0 ;
145- foreach ($ dataProvider ->getModels () as $ model ) {
146- foreach ($ searchModel ->exportFields () as $ one ) {
147- if (is_string ($ one )) {
148- $ items [$ i ] = $ model [$ one ];
149- } else {
150- $ items [$ i ] = $ one ($ model );
151- }
152- $ i ++;
153- }
154- fputcsv ($ fp , $ items , "; " );
155- $ items = [];
156- $ i = 0 ;
157- }
158116 }
159- fclose ($ fp );
160117 }
118+ fclose ($ fp );
161119 }
162120
163121 public function actionWord ()
0 commit comments