Example of an Excel file exported with header

Source: Internet
Author: User
This article describes how to use the header to export an Excel file. the specific implementation code is as follows. do not miss this article if you are interested. The code is as follows:


Class reportFormAction extends CommonAction {

Public function index (){
If ($ _ POST ){
// @ Param $ data array the data to be exported
// @ Param $ field string name of the column to export the csv file
// @ Param $ filename string name of the csv file to be exported
$ Where = '1 ';
$ Order = 'creative _ id desc ';
If ($ _ POST ['crea _ wh '] = 1) $ order. = "";
If ($ _ POST ['crea _ wh '] = 2) $ order. = "creative_time desc ";
$ Star = strtotime ($ _ POST ['control _ star']);
$ End = strtotime ($ _ POST ['control _ end']);
If ($ star & $ end) $ where. = "and (create_time between $ star and $ end )";
If ($ _ POST ['creative _ type'] = 1) $ where. = "and creative_type = 1 ";
If ($ _ POST ['creative _ type'] = 2) $ where. = "and creative_type = 2 ";
If ($ _ POST ['creative _ type'] = 3) $ where. = "and creative_type = 3 ";
If ($ _ POST ['sort _ name'] = 1) $ where. = "and sort_name = 'technical '";
If ($ _ POST ['sort _ name'] = 2) $ where. = "and sort_name = 'packaging class '";
If ($ _ POST ['sort _ name'] = 3) $ where. = "and sort_name = 'product class '";
If ($ _ POST ['sort _ name'] = 4) $ where. = "and sort_name = 'marketing class '";
If ($ _ POST ['sort _ name'] = 5) $ where. = "and sort_name = 'other class '";
If ($ _ POST ['two _ status'] = 0) {$ where. = "and two_status = 0"; $ fenlei1 = "one-sentence originality ";}
If ($ _ POST ['two _ status'] = 1) {$ where. = "and two_status = 1"; $ fenlei2 = "standard idea ";}
$ Csv = '';
$ Lists = M ('creative ')-> where ($ where)-> order ($ order)-> select ();
If (is_array ($ lists) & count ($ lists)> 0 ){


If (empty ($ filename )){
$ Filename = date('y-m-d', time({}.'.csv ';
}
Header ('content-type: application/vnd. ms-excel ');
Header ('content-Disposition: attachment; filename = '. $ filename );
Header ('pragma: no-cache ');
Header ('expires: 0 ');
If ($ _ POST ['two _ status'] = 0 ){
$ Csv = 'Category, creative ID, creative type, creative tag, creative title, creative description, comments, favorites, votes, submitted by, author '. "\ n ";
} Elseif ($ _ POST ['two _ status'] = 1 ){
$ Csv = 'Category, creative ID, creative type, creative tag, creative title, creative description, comments, favorites, voting, submitted by, author, collaborators, core innovation points, market plans, existing commercial cases, patent status, implementation methods, and release time '. "\ n ";
} Else {
$ Csv = 'Category, creative ID, creative type, creative tag, creative title, creative description, comments, favorites, voting, submitted by, author, collaborators, core innovation points, market plans, existing commercial cases, patent status, implementation methods, and release time '. "\ n ";
}
Foreach ($ lists as $ list => $ v ){
If ($ v ['creative _ type'] = 1 ){
$ Type = 'question ';
} Elseif ($ v ['creative _ type'] = 2 ){
$ Type = 'workary ';
} Elseif ($ v ['creative _ type'] = 3 ){
$ Type = 'originality ';
}
If ($ v ['two _ status'] = 0) $ fenlei = "one-sentence originality ";
If ($ v ['two _ status'] = 1) $ fenlei = "standard idea ";
If ($ _ POST ['two _ status'] = 0) {// A sentence
$ Csv. = $ fenlei1 .','. $ v ['creative _ id']. ",". $ v ['sort _ name']. ','. $ type. ','. $ v ['creative _ name']. ','. $ v ['creative _ description']. ','. $ v ['Comment _ num']. ','. $ v ['collect _ num']. ','. $ v ['vote _ num']. ','. $ v ['submitter ']. ','. $ v ['author']. "\ n ";
} Elseif ($ _ POST ['two _ status'] = 1) {// standard
$ Csv. = $ fenlei2 .','. $ v ['creative _ id']. ",". $ v ['sort _ name']. ','. $ type. ','. $ v ['creative _ name']. ','. $ v ['creative _ description']. ','. $ v ['Comment _ num']. ','. $ v ['collect _ num']. ','. $ v ['vote _ num']. ','. $ v ['submitter ']. ','. $ v ['author']. ','. $ v ['collaborator']. ','. $ v ['Innovation _ description']. ','. $ v ['marketing _ plan']. ','. $ v ['business _ case']. ','. $ v ['patent _ situation ']. ','. $ v ['discuss _ way']. ','. strtotime ($ v ['create _ time']). "\ n ";
} Else {// All
$ Csv. = $ fenlei. ','. $ v ['creative _ id']. ",". $ v ['sort _ name']. ','. $ type. ','. $ v ['creative _ name']. ','. $ v ['creative _ description']. ','. $ v ['Comment _ num']. ','. $ v ['collect _ num']. ','. $ v ['vote _ num']. ','. $ v ['submitter ']. ','. $ v ['author']. ','. $ v ['collaborator']. ','. $ v ['Innovation _ description']. ','. $ v ['marketing _ plan']. ','. $ v ['business _ case']. ','. $ v ['patent _ situation ']. ','. $ v ['discuss _ way']. ','. strtotime ($ v ['create _ time']). "\ n ";
}
}
Echo mb_convert_encoding ($ csv, "CP936", "UTF-8 ");

} Else {
$ This-> assign ("msg", $ msg );
}
Exit;
}




$ This-> display ();
}


Public function votecount (){
If ($ _ POST ){
// @ Param $ data array the data to be exported
// @ Param $ field string name of the column to export the csv file
// @ Param $ filename string name of the csv file to be exported
$ Where = '1 ';
If ($ _ POST ['crea _ order'] = 1) $ order = "c. vote_num desc ";
$ Star = strtotime ($ _ POST ['control _ star']);
$ End = strtotime ($ _ POST ['control _ end']);
If ($ star & $ end) $ where. = "and (c. vote_start_time between $ star and $ end) and (c. vote_end_time between $ star and $ end )";
$ SQL = "select c. sort_name, c. creative_id, c. creative_type, c. creative_name, c. creative_description, c. vote_num, c. comment_num, c. collect_num, c. create_time, u. username from cofco_creative as c left join cofco_userinfo as u on c. uid = u. uid where $ where order by $ order ";
$ Lists = M ()-> query ($ SQL );
$ Csv = '';
If (is_array ($ lists) & count ($ lists)> 0 ){


If (empty ($ filename )){
$ Filename = date('y-m-d', time({}.'.csv ';
}
Header ('content-type: application/vnd. ms-excel ');
Header ('content-Disposition: attachment; filename = '. $ filename );
Header ('pragma: no-cache ');
Header ('expires: 0 ');


$ Csv = 'originality tag, submitted by, originality category, originality title, originality brief, number of votes, comments, favorites, upload time'. "\ n ";
Foreach ($ lists as $ list => $ v ){
If ($ v ['creative _ type'] = 1 ){
$ Type = 'question ';
} Elseif ($ v ['creative _ type'] = 2 ){
$ Type = 'workary ';
} Elseif ($ v ['creative _ type'] = 3 ){
$ Type = 'originality ';
}
$ Time = date ("Y-m-d H: I: s", $ v ['create _ time']);
$ Csv. = $ v ['sort _ name']. ','. $ v ['username']. ','. $ type. ','. $ v ['creative _ name']. ','. $ v ['creative _ description']. ','. $ v ['vote _ num']. ','. $ v ['Comment _ num']. ','. $ v ['collect _ num']. ','. $ time. "\ n ";
}
Echo mb_convert_encoding ($ csv, "CP936", "UTF-8 ");

} Else {
$ This-> assign ("msg", $ msg );
}
Exit;
}

$ This-> display ();
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.