Thinkphp Excel file export example

Source: Internet
Author: User
Tags foreach php file

Pay attention to the following points:

1. spl_autoload_register (array ('think', 'autoload') must be available '));
2. BOM headers cannot appear in the current Action and Parent Action (remember) or garbled characters will be exported.

Example 1

The code is as follows: Copy code
/* Export xls user personal information */
Function export (){
Header ("Content-type: application/octet-stream ");
Header ("Accept-Ranges: bytes ");
Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: attachment; filename = user information table". date ("Y-m-d"). ". xls ");
Header ("Pragma: no-cache ");
Header ("Expires: 0 ");
// Start exporting xls
$ Tag0 = iconv ("UTF-8", "GB2312", 'user ID ');
$ Tag1 = iconv ("UTF-8", "GB2312", 'Username ');
$ Tag2 = iconv ("UTF-8", "GB2312", 'Waiting for collect ');
$ Tag3 = iconv ("UTF-8", "GB2312", 'Total bonuses ');
$ Tag4 = iconv ("UTF-8", "GB2312", 'account login ');
$ Tag5 = iconv ("UTF-8", "GB2312", 'account city ');
$ Tag6 = iconv ("UTF-8", "GB2312", 'account opening address ');
$ Tag7 = iconv ("UTF-8", "GB2312", 'bank card number ');
$ Tag8 = iconv ("UTF-8", "GB2312", 'cardholder name ');
Echo "$ tag0 \ t $ tag1 \ t $ tag2 \ t $ tag3 \ t $ tag4 \ t $ tag5 \ t $ tag6 \ t $ tag7 \ n ";
//// Query a table
// $ Arr = M ('textpage')-> field ('username, count (id) as allcount, sum (price) as allprice ') -> group ('Username')-> select ();
$ Field = "crowd_textpage.tid, crowd_textpage.username, users. bankName, users. city, users. bankAddress,
Users. bankCard, users. bankUsr, count (crowd_textpage.id) as allcount, sum (crowd_textpage.price) as allprice ";
$ Arr = M ('textpage')-> field ($ field)
-> Join ('crowd _ user as users ON crowd_textpage.username = users. Username ')
-> Group ('crowd _ textpage. Username ')
-> Select ();
// Dump (M ('textpage')-> getLastSql (); die;
Foreach ($ arr as $ key => $ val ){
// $ Date = date ('Y-m-D', $ val ['pay _ time']);
$ Tid = iconv ("UTF-8", "GB2312", $ val ['tid']);
$ Tid = $ tid? $ Tid :'-';
$ Username = iconv ("UTF-8", "GB2312", $ val ['username']);
$ Username = $ username? $ Username :'-';
$ Allcount = iconv ("UTF-8", "GB2312", $ val ['allcount']);
$ Allcount = $ allcount? $ Allcount :'-';
$ Allprice = iconv ("UTF-8", "GB2312", $ val ['allprice']);
$ Allprice = $ allprice? $ Allprice :'-';
$ BankName = iconv ("UTF-8", "GB2312", $ val ['bankname']);
$ BankName = $ bankName? $ BankName :'-';
$ City = iconv ("UTF-8", "GB2312", $ val ['city']);
$ City = $ city? $ City :'-';
$ BankAddress = iconv ("UTF-8", "GB2312", $ val ['bankaddress']);
$ BankAddress = $ bankAddress? $ BankAddress :'-';
$ BankCard = iconv ("UTF-8", "GB2312", $ val ['bankcard ']);
$ BankCard = $ bankCard? $ BankCard :'-';
$ BankUsr = iconv ("UTF-8", "GB2312", $ val ['bankusr']);
$ BankUsr = $ bankUsr? $ BankUsr :'-';
Echo "$ tid \ t $ username \ t $ allcount \ t $ allprice \ t $ bankName \ t $ city
\ T $ bankAddress \ t' $ bankCard \ t $ bankUsr \ n ";
  }
}

Example 2

1: Download PHPexcel
2: Place the downloaded folder in thinkphp and name it "Classes" under \ Extend \ Vendor.
I have a PHPExcel file and a PHPExcel. Php file in this folder.
3:

The code is as follows: Copy code
Function pushExcel (){
$ User = D ("User ");
$ List = $ user-> relation (true)-> select ();
Vendor ("Classes. PHPExcel ");
Vendor ("Classes. PHPExcel. IOFactory ");
Vendor ("Classes. PHPExcel. Reader. Excel5 ");
// Create a processing object instance
$ ObjPhpExcel = new PHPExcel ();
$ ObjPhpExcel-> getActiveSheet ()-> getdefacolumcolumndimension ()-> setAutoSize (true); // you can specify the cell width.
// Set the table width manually
$ ObjPhpExcel-> getActiveSheet ()-> getColumnDimension ('g')-> setWidth (20 );
$ ObjPhpExcel-> getActiveSheet ()-> getColumnDimension ('h')-> setWidth (15 );
$ ObjPhpExcel-> getActiveSheet ()-> getColumnDimension ('I')-> setWidth (15 );
$ ObjPhpExcel-> getActiveSheet ()-> getColumnDimension ('J')-> setWidth (20 );
// Set the title
$ RowVal = array (0 => 'number', 1 => 'name', 2 => 'gender', 3 => 'age ', 4 => 'Department ',
5 => 'position ', 6 => 'mailbox', 7 => 'Office telephony ', 8 => 'mobile telephony ',
9 => 'address ');
Foreach ($ rowVal as $ k => $ r ){
$ ObjPhpExcel-> getActiveSheet ()-> getStyleByColumnAndRow ($ k, 1)
-> GetFont ()-> setBold (true); // bold font
$ ObjPhpExcel-> getActiveSheet ()-> getStyleByColumnAndRow ($ k, 1)->
GetAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_CENTER); // text Center
$ ObjPhpExcel-> getActiveSheet ()-> setCellValueByColumnAndRow ($ k, 1, $ r );
  }
// Set the current sheet index for subsequent content operations
$ ObjPhpExcel-> setActiveSheetIndex (0 );
$ ObjActSheet = $ objPhpExcel-> getActiveSheet ();
// Set the name of the sheet for the current activity
$ Title = "company address book ";
$ ObjActSheet-> setTitle ($ title );
// Set the cell content
Foreach ($ list as $ k => $ v)
  {
$ Num = $ k + 2;
$ ObjPhpExcel-> setActiveSheetIndex (0)
// Column A of Excel, uid is the key value of the array you found, and so on
-> SetCellValue ('A'. $ num, $ v ['userid'])
-> SetCellValue ('B'. $ num, $ v ['username'])
-> SetCellValue ('C'. $ num, $ v ['sex'])
-> SetCellValue ('D'. $ num, $ v ['age'])
-> SetCellValue ('E'. $ num, $ v ['Department '] ['departmentname'])
-> SetCellValue ('F'. $ num, $ v ['position'])
-> SetCellValue ('g'. $ num, $ v ['email '])
-> SetCellValue ('H'. $ num, $ v ['phone'])
-> SetCellValue ('I'. $ num, $ v ['lelephone'])
-> SetCellValue ('j'. $ num, $ v ['address']);
  }
$ Name = date ('Y-m-D'); // you can specify a file name.
Header ("Content-Type: application/force-download ");
Header ("Content-Type: application/octet-stream ");
Header ("Content-Type: application/download ");
Header ("Content-Transfer-Encoding: UTF-8 ");
Header ("Pragma: no-cache ");
Header ('content-Type: application/vnd. ms-excel ');
Header ('content-Disposition: attachment; filename = "'.{title.'_'.urlencode({name}.'.xls "');
Header ('cache-Control: max-age = 0 ');
$ ObjWriter = PHPExcel_IOFactory: createWriter ($ objPhpExcel, 'excel5 ');
$ ObjWriter-> save ('php: // output ');
}
It's been a good morning. Now I can paste the prepared one.
If the program is incompatible with ThinkPHP, add 'output _ encode' => false in config. php,
In addition, add event listening to buttons in html
<Input id = "toExcel" name = "button_toExcel" type = "button" value = "export Excel">
<Script type = "text/javascript">
$ (Function (){
$ ("# ToExcel"). click (function (){
Document. location. href = "_ URL _/pushExcel ";
});
});
</Script>

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.