Php implementation code for writing excel files

Source: Internet
Author: User
Php implementation code for writing excel files
This article introduces how to use php to write the code for an excel file, including the title and content. For more information, see.

Php writes an excel file, including the title. This is often used in daily development. for example, some contact lists and company address book operations will use php to operate excel files.

Code:

     SetActiveSheetIndex (0); // Set the activity page $ activeSheet = $ objPHPExcel-> getActiveSheet (); // get the current activity page $ activeSheet-> getdefacolumcolumndimension () -> setWidth (16); // Set the column width to 16 $ activeSheet-> setTitle ("Contact"); // Set the sheet name // set the title. The first column of the cell is 0, first Behavior 1 $ colNum = count ($ titles); for ($ I = 0; $ I <$ colNum; $ I ++) {$ activeSheet-> getStyleByColumnAndRow ($ I, 1)-> getFont ()-> setBold (true); // set the title to bold $ activeSheet-> setCellValueByColumnAndRo W ($ I, 1, $ titles [$ I]);} // sets the body. row starts from 2, the first row has been set as the title $ rowNum = count ($ elements); for ($ row = 0; $ row <$ rowNum; $ row ++) {for ($ col = 0; $ col <$ colNum; $ col ++) {$ activeSheet-> getStyle ($ this-> numToEn ($ col ). ($ row + 2)-> getNumberFormat ()-> setFormatCode (PHPExcel_Style_NumberFormat: FORMAT_TEXT); $ activeSheet-> setCellValue ($ this-> numToEn ($ col ). ($ row + 2 ),"". $ elements [$ row] [$ col]); // add Space to prevent long numbers from being converted into scientific notation $ activeSheet-> getStyle ($ this-> numToEn ($ col ). ($ row + 2)-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_CENTER );}} // Get the name of the saved file $ filetype = $ this-> getFileType ($ filename); // read the excel file ob_end_clean () based on the file type; $ objWriter = null; if ($ filetype = "xlsx") {$ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel2007 '); header ('content-Type: application/vnd. o Penxmlformats-officedocument.spreadsheetml.sheet '); header ('content-Disposition: attachment; filename = '. urlencode ($ filename); header ('cache-Control: max-age = 0'); ob_end_clean (); $ objWriter-> save ('php: // output'); $ objPHPExcel-> disconnectWorksheets (); unset ($ objPHPExcel); exit;} elseif ($ filetype = "xls") {$ objWriter = PHPExcel_IOFactory :: createWriter ($ objPHPExcel, 'excel5'); header ('content-Type: Application/vnd. ms-excel '); header ('content-Disposition: attachment; filename = '. urlencode ($ filename); header ('cache-Control: max-age = 0'); ob_end_clean (); $ objWriter-> save ('php: // output'); $ objPHPExcel-> disconnectWorksheets (); unset ($ objPHPExcel); exit;} else {return false;} catch (Exception $ e) {HWS :: logErr ('Excel ERROR :'. $ e-> getMessage (); $ errText = "an error occurred while creating the file. contact the administrator and try again later! "; Return array ('createfileerr' => $ errText );}} /*** get file type ** @ param $ filenamePath file path or file name */private function getFileType ($ filenamePath) {if (! $ FilenamePath) {return false;} $ filenameArr = explode ('/', $ filenamePath); $ count = count ($ filenameArr ); $ filename = $ filenameArr [$ count-1]; $ filetypeArr = explode ('. ', $ filename); $ count = count ($ filetypeArr); $ filetype = $ filetypeArr [$ count-1]; return $ filetype ;} /** generate up to two letters corresponding to the EXCEL file column */private function numToEn ($ num) {$ asc = 0; $ en = ""; $ num = (int) $ num + 1; if ($ num <26) // determine whether two values are required for the specified number {If (int) $ num <10) {$ asc = ord ($ num); $ en = chr ($ asc + 16 );} else {$ num_g = substr ($ num,); $ num_s = substr ($ num,); $ asc = ord ($ num_g ); $ en = chr ($ asc + 16 + 10 * $ num_s);} else {$ num_complementation = floor ($ num/26 ); $ en_q = $ this-> numToEn ($ num_complementation); $ en_h = $ num % 26! = 0? $ This-> numToEn ($ num-$ num_complementation * 26): "A"; $ en = $ en_q. $ en_h;} return $ en ;}

Code description: two ob_end_clean () in the exportFileContacts method; $ objPHPExcel-> disconnectWorksheets (); unset ($ objPHPExcel); it is used to clear the buffer and prevent excel export errors and cannot be opened, A format error is reported.

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.