Use phpexcel to import database data in excel (excel filtering), export excel, and phpexcelexcel
For more information, see the code:
<? Phpdate_default_timezone_set ("PRC"); error_reporting (E_ALL); error_reporting (0); ini_set ('display _ errors', TRUE); ini_set ('display _ startup_errors ', TRUE ); define ('eol ', (PHP_SAPI = 'cli ')? PHP_EOL: '<br/>'); require_once ('.. /Classes/PHPExcel. php '); require_once ("config. php "); require_once (" mysql. class. php "); // generate a purchase report based on time $ time = date (" a "); $ minute = date (" I "); $ apm = ""; if ($ time = 'pm ') {$ apm = $ time; $ stime = mktime (12, 00, 00, date ('M'), date ('D ') -1, date ('y'); $ etime = mktime (11,59, 59, date ('M'), date ('D '), date ('y');} else {$ apm = $ time; $ stime = mktime (12, 00, 00, date ('M'), date ('D ') -1, date (' Y'); $ etime = mktime (11,59, 59, date ('M'), date ('D'), date ('y '));} // instantiate the excel class $ objPHPExcel = new PHPExcel (); /////// obtain the document information ////// $ objProps = $ objPHPExcel-> getProperties (); //// // print_r ($ objProps); // echo "<br/> "; //// // $ objProps-> setDescription ("test_123456"); // print_r ($ objProps); $ objPHPExcel-> setActiveSheetIndex (0) -> setCellValue ('a5 ', 'item Code')-> setCellValue ('b5', 'hangzhou')-> setCellValue ('c5 ',' Item name ')-> setCellValue ('d5', 'purchase volume '); // you can specify the selected sheet table name $ objPHPExcel-> getActiveSheet ()-> setTitle ('ancestral name '); // set the font style $ objPHPExcel-> getActiveSheet ()-> getStyle ('a1')-> getFont ()-> setName ('arial')-> setSize (25 ); ////-> setUnderline (true); //-> getColor ()-> setARGB ('ffffff000000 '); ///-> setBold (true); // merge cells to assign values to cells (values, strings, and formulas) $ objPHPExcel-> getActiveSheet ()-> mergeCells ('a1: d3 ')-> setCellValue ('a1', 'zhongyi list'); // $ objPHP Excel-> getActiveSheet ()-> mergeCells ('a4: D4 ')-> setCellValue ('a4', "= SUM (E4: F4 )"); $ date_now = date ("Y-m-d"); $ objPHPExcel-> getActiveSheet ()-> mergeCells ('a4: D4 ')-> setCellValue ('a4 ', "procurement Date :". $ date_now. "". $ apm. ""); // set the width of A single column $ objPHPExcel-> getActiveSheet ()-> getColumnDimension ('A')-> setWidth (15); $ objPHPExcel-> getActiveSheet () -> getColumnDimension ('B')-> setWidth (20); // $ objPHPExcel-> getActiveSheet ()-> getColum NDimension ('G')-> setRowHeight (50);/$ objPHPExcel-> getActiveSheet ()-> getColumnDimension ('C')-> setWidth (44 ); $ objPHPExcel-> getActiveSheet ()-> getColumnDimension ('D')-> setWidth (15 ); // large border style border bold $ lineBORDER = array ('borders' => array ('outline' => array ('style' => PHPExcel_Style_Border: BORDER_THICK, 'color' => array ('arg' => '123 '),),),); // Header style $ head = array ('font' => array ('bold '=> true), 'al Ignment '=> array ('horizontal' => PHPExcel_Style_Alignment: HORIZONTAL_CENTER, 'vertical '=> PHPExcel_Style_Alignment: VERTICAL_CENTER ),); // title style $ title = array ('font' => array ('bold '=> true ),); // CENTER alignment $ CENTER = array ('alignment '=> array ('horizontal' => dimensions: HORIZONTAL_CENTER, 'vertical '=> PHPExcel_Style_Alignment: VERTICAL_CENTER ),); // align RIGHT to $ RIGHT = array ('alignment '=> Array ('horizontal '=> PHPExcel_Style_Alignment: HORIZONTAL_RIGHT, 'version' => PHPExcel_Style_Alignment: VERTICAL_CENTER ),); // fine border style $ linestyle = array ('borders' => array ('outline' => array ('style' => PHPExcel_Style_Border: BORDER_THIN, 'color' => array ('arg' => 'ff000000'),); $ objPHPExcel-> getActiveSheet ()-> getStyle ('a1: D3 ') -> applyFromArray ($ head); //-> getAlignment ()-> getHorizontal (''); //-> GetBorders ()-> getTop ()-> setBorderStyle (''); //-> setWrapText (true); automatically wrap $ objPHPExcel-> getActiveSheet () -> getStyle ('a4: D4 ')-> applyFromArray ($ RIGHT); $ objPHPExcel-> getActiveSheet ()-> getStyle ('a5: D5 ') -> applyFromArray ($ title); // fill color // $ objPHPExcel-> getActiveSheet ()-> getStyle ('a1')-> getFill () -> getStartColor ()-> setARGB ('ffff000000'); // insert data $ dsql-> Execute ('omebrand _ list', "select I. goods_id, sum ('nums ') AS num, I. name, I. addon, I. price, g. bn as B, I. bn as h, g. goods_id, I. goods_id, I. order_idFROM 'sdb _ B2C _order_items 'as I, sdb_ B2C _goods as gWHERE I. order_id in (select order_id from sdb_ B2C _orders where status = 'active' and createtime between $ stime and $ etime) and I. goods_id = g. goods_id and g. cat_id = 173 group by h "); $ m = 0; unset ($ re); while ($ row = $ dsql-> GetObject ('omebrand _ list ')) {$ re [$ m] = get_obj Ect_vars ($ row); $ m ++;} $ row_count = 5; $ objPHPExcel-> setActiveSheetIndex (0)-> setCellValue ('a6 ', 12325416541) -> setCellValue ('b6 ', 4962132165262)-> setCellValue ('c6', 121515212515241521)-> setCellValue ('d6 ', 96215465415 ); foreach ($ re as $ r =>$ dataRow) {$ baseRow = 6; $ row = $ baseRow + $ r; $ bn = $ dataRow [h]; $ goods_id = $ dataRow [goods_id]; $ spec_value = ""; $ aa = unserialize ($ dataRow [addon]); if ($ aa ['Product _ attr']) {foreach ($ aa ['product _ attr'] as $ arr_special_info) {$ spec_value = $ arr_special_info ['value'];} preg_match_all ('/\\-? \ D + \\.? \ D */I ', $ spec_value, $ row1); $ num = $ row1 [0] [0]; $ all = $ num * $ dataRow [num]; if ($ spec_value = '') {$ all = $ dataRow ['num']; // $ prce = $ dataRow [price];} $ objPHPExcel-> setActiveSheetIndex (0)-> setCellValue ('A '. $ row, $ dataRow ['B'])-> setCellValue ('B '. $ row, $ bn)-> setCellValue ('C '. $ row, $ dataRow ['name'])-> setCellValue ('D '. $ row, $ all); $ objPHPExcel-> getActiveSheet ()-> getStyle ('A '. $ row_count)-> applyFromArray ($ Linestyle); $ objPHPExcel-> getActiveSheet ()-> getStyle ('B '. $ row_count)-> applyFromArray ($ linestyle); $ objPHPExcel-> getActiveSheet ()-> getStyle ('C '. $ row_count)-> applyFromArray ($ linestyle); $ objPHPExcel-> getActiveSheet ()-> getStyle ('D '. $ row_count)-> applyFromArray ($ linestyle); $ baseRow ++; $ row_count ++;} $ objPHPExcel-> getActiveSheet ()-> getStyle ('A '. $ row_count)-> applyFromArray ($ linestyle); $ objPHPExcel-> getA CtiveSheet ()-> getStyle ('B '. $ row_count)-> applyFromArray ($ linestyle); $ objPHPExcel-> getActiveSheet ()-> getStyle ('C '. $ row_count)-> applyFromArray ($ linestyle); $ objPHPExcel-> getActiveSheet ()-> getStyle ('D '. $ row_count)-> applyFromArray ($ linestyle); $ objPHPExcel-> getActiveSheet ()-> getStyle ('a5: d '. $ row_count)-> applyFromArray ($ CENTER); $ objPHPExcel-> getActiveSheet ()-> getStyle ('a1: d '. $ row_count)-> applyFromArray ($ LineBORDER); // set the print margin $ objPHPExcel-> getActiveSheet ()-> getPageMargins ()-> setTop (0); $ objPHPExcel-> getActiveSheet ()-> getPageMargins () -> setRight (0); $ objPHPExcel-> getActiveSheet ()-> getPageMargins ()-> setLeft (0); $ objPHPExcel-> getActiveSheet ()-> getPageMargins () -> setBottom (0); // set the paper type $ objPHPExcel-> getActiveSheet ()-> getPageSetup ()-> setPaperSize (PHPExcel_Worksheet_PageSetup: PAPERSIZE_A4 ); // set automatic filtering $ objPHPExce L-> getActiveSheet ()-> setAutoFilter ('a5: d '. $ row_count); // set automatic line feed $ objPHPExcel-> getActiveSheet ()-> getStyle ('b6: B '. $ row_count)-> getAlignment ()-> setWrapText (true); // You Can format the number $ objPHPExcel-> getActiveSheet ()-> getStyle ('a6: '. $ row_count)-> getNumberFormat ()-> setFormatCode ('000000'); // set the security level $ md = md5 (time (); $ md = substr ($ md, 0, 8); $ objPHPExcel-> getActiveSheet ()-> getProtection ()-> setPassword ("$ md"); $ objPHPExcel-> GetActiveSheet ()-> getProtection ()-> setSheet (true); // $ objPHPExcel-> getActiveSheet ()-> getProtection ()-> setSort (true ); $ objPHPExcel-> getActiveSheet ()-> getProtection ()-> setInsertRows (true); $ objPHPExcel-> getActiveSheet ()-> getProtection ()-> setFormatCells (true ); // Add an image/* $ obj = $ objPHPExcel-> getActiveSheet (); $ objDrawing = new PHPExcel_Worksheet_Drawing (); $ objDrawing-> setName ('wsyimg '); $ objDrawing-> setDescr Iption ('image inserted by zhy '); $ objDrawing-> setPath ('. /wsy.jpg '); $ objDrawing-> setHeight (50); $ objDrawing-> setCoordinates ('h23'); $ objDrawing-> setOffsetX (60 ); $ objDrawing-> setRotation (-10);/$ objDrawing-> getShadow ()-> setVisible (true); $ objDrawing-> getShadow ()-> setDirection (-20 ); /$ objDrawing-> setWorksheet ($ obj); * // header and footer // $ objPHPExcel-> getActiveSheet ()-> getHeaderFooter ()-> setOddHeader ('zhy '); // $ ObjPHPExcel-> getActiveSheet ()-> getHeaderFooter ()-> setOddFooter ('end'); $ objPHPExcel-> setActiveSheetIndex (0 ); $ tname = date ('Y-m-dH ', time (); $ tnam = iconv ('utf-8', 'gbk', 'ancestral Order '); $ tname = $ tnam. $ tname; // save Excel 2007 // $ objWriter = new PHPExcel_Writer_Excel2007 ($ objPHPExcel); // $ objWriter-> save (str_replace ('. php ', '.xlsx', _ FILE _); // save Excel 5 // $ objWriter = new PHPExcel_Writer_Excel5 ($ objPHPExcel); // $ ObjWriter-> save (str_replace ('. php ', '.xls', _ FILE _); $ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5'); $ objWriter-> save (str_replace ('. php ', '.xls', _ FILE _); // $ url = "/data/home/htdocs/ec/public/files /". date ("Y "). "/". date ("Ym "). "/"; createDir ($ url); function createDir ($ dir) {if (! Is_dir ($ dir) {mkdir ($ dir, 0777, true); chmod ($ dir, 0777); chown ($ dir, 'daemon'); chgrp ($ dir, 'daemon') ;}}$ name = 'forexmple _ excel '; rename (str_replace ('. php ', '.xls', _ FILE _), comment ');?>
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!