thinkphp3.2 and Phpexcel Perfect case

Source: Internet
Author: User
This article to share the content is thinkphp3.2 and phpexcel perfect case, has a certain reference value, the need for friends can refer to



Export EXL Public Function Look_down () {$id = I (' get.id ');        $m = m (' offer_goods ');        $where [' offer_id '] = $id;                $data = $m->field (' Goods_id,goods_sn,goods_name,barcode,goods_type,price ')->select ();        Export ExL Import ("Org.Util.PHPExcel");        Import ("Org.Util.PHPExcel.Worksheet.Drawing");        Import ("Org.Util.PHPExcel.Writer.Excel2007");        $objPHPExcel = new \phpexcel ();            $objWriter = new \phpexcel_writer_excel2007 ($objPHPExcel);                $objActSheet = $objPHPExcel->getactivesheet (); Center horizontally (position is important, recommended at initial position) $objPHPExcel->setactivesheetindex (0)->getstyle (' A ')->getalignment ()        Sethorizontal (\phpexcel_style_alignment::horizontal_center); $objPHPExcel->setactivesheetindex (0)->getstyle (' B1 ')->getalignment ()->sethorizontal (\phpexcel_        Style_alignment::horizontal_center); $objPHPExcel->setactivesheetindex (0)->getstyle (' C ')->getalignment ()->sethorizontAl (\phpexcel_style_alignment::horizontal_center); $objPHPExcel->setactivesheetindex (0)->getstyle (' D ')->getalignment ()->sethorizontal (\phpexcel_style        _alignment::horizontal_center); $objPHPExcel->setactivesheetindex (0)->getstyle (' E ')->getalignment ()->sethorizontal (\phpexcel_style        _alignment::horizontal_center); $objPHPExcel->setactivesheetindex (0)->getstyle (' F ')->getalignment ()->sethorizontal (\phpexcel_style                _alignment::horizontal_center);        $objActSheet->setcellvalue (' A1 ', ' Commodity article number ');        $objActSheet->setcellvalue (' B1 ', ' Product name ');        $objActSheet->setcellvalue (' C1 ', ' commodity map ');        $objActSheet->setcellvalue (' D1 ', ' commodity barcode ');        $objActSheet->setcellvalue (' E1 ', ' commodity attributes ');        $objActSheet->setcellvalue (' F1 ', ' quote (HKD) ');        Set table width $objPHPExcel->getactivesheet ()->getcolumndimension (' A ')->setwidth (16); $objPHPExcel->getactivesheet ()->getcolumndimension (' B ')->setwIdth (80);        $objPHPExcel->getactivesheet ()->getcolumndimension (' C ')->setwidth (15);        $objPHPExcel->getactivesheet ()->getcolumndimension (' D ')->setwidth (20);        $objPHPExcel->getactivesheet ()->getcolumndimension (' E ')->setwidth (12);                $objPHPExcel->getactivesheet ()->getcolumndimension (' F ')->setwidth (12); Center vertically $objPHPExcel->getactivesheet ()->getstyle (' A ')->getalignment ()->setvertical (\phpexcel_style_        Alignment::vertical_center); $objPHPExcel->getactivesheet ()->getstyle (' B ')->getalignment ()->setvertical (\phpexcel_style_        Alignment::vertical_center); $objPHPExcel->getactivesheet ()->getstyle (' D ')->getalignment ()->setvertical (\phpexcel_style_        Alignment::vertical_center); $objPHPExcel->getactivesheet ()->getstyle (' E ')->getalignment ()->setvertical (\phpexcel_style_        Alignment::vertical_center); $objPHPExcel->getactivesheet ()->getstyle (' F ')Getalignment ()->setvertical (\phpexcel_style_alignment::vertical_center);            Processing table Data foreach ($data as $k = = $v) {$k +=2;                $objActSheet->setcellvalue (' A '. $k, $v [' goods_sn ']);                                            $objActSheet->setcellvalue (' B '. $k, $v [' goods_name ']);            $img = M (' goods ')->where (' goods_id = '. $v [' goods_id '])->field (' Goods_thumb ')->find ();            Image Generation $objDrawing [$k] = new \phpexcel_worksheet_drawing ();            $objDrawing [$k]->setpath ('./upload/'. $img [' goods_thumb ']); Set width height $objDrawing [$k]->setheight (80);//Photo height $objDrawing [$k]->setwidth (80);            Photo Width/* Set the cell to be inserted in the picture */$objDrawing [$k]->setcoordinates (' C '. $k);            Image offset Distance $objDrawing [$k]->setoffsetx (12);            $objDrawing [$k]->setoffsety (12);            $objDrawing [$k]->setworksheet ($objPHPExcel->getactivesheet ());            Table Contents $objActSheet->setcellvalue (' D '. $k, $v [' barcode ']);                $objActSheet->setcellvalue (' E '. $k, $v [' Goods_type ']);                            $objActSheet->setcellvalue (' F '. $k, $v [' Price '];                    Table Height $objActSheet->getrowdimension ($k)->setrowheight (80);        } $fileName = ' quote table ';        $date = Date ("Y-m-d", Time ());        $fileName. = "_{$date}.xls";        $fileName = Iconv ("Utf-8", "gb2312", $fileName);        Rename table//$objPHPExcel->getactivesheet ()->settitle (' Test ');        Set the activity Order index to the first table, so Excel opens this is the first table $objPHPExcel->setactivesheetindex (0);        Header (' Content-type:application/vnd.ms-excel ');        Header ("content-disposition:attachment;filename=\" $fileName \ "");        Header (' cache-control:max-age=0 ');        $objWriter = \phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 '); $objWriter->save (' php://output '); File download via browser//END        } 




Precautions:
1.phpexcel Download Address: http://phpexcel.codeplex.com/

2. Put the files under the classes directory (phpexcel.php and Phpexcel folders) in the Thinkphp\library\org\util directory (or in other directories, just to load the class without problems). and change the phpexcel.php to PHPExcel.class.php (this step is important, you need to load the PHP files are changed to xxx.class.php)

3. Manually import the class (note the "\" symbol)
Import ("Org.Util.PHPExcel");
$objPHPExcel = new \phpexcel ();
(I tried to automatically load the class, it is troublesome, because Phpexcel files need to add "namespace org\util;", so simply choose to manually load the class)

4. Horizontal Center, vertical center, height and other settings. Notice where you put it, if you put it at the end, then it takes effect. Put to the head, the first line takes effect (the code above is the first line in effect, if placed in foreach is the next line to take effect).

5.import Introduction. Import ("Org.Util.PHPExcel.Writer.Excel2007"); File Location: org\util\phpexcel\writer\excel2007.class.php

6. The image address must be local.
K]->setpath ('./upload/'. $img [' goods_thumb ']); Picture location: Installation directory/upload/xxx


Problems that are prone to arise:
1. ' Class xxx not found ' is not successful, you can first put a function ss () {} in xxx.class.php and then output under XX->SS () output to see the results.

Analysis Address: http://www.thinkphp.cn/code/2112.html


Transferred from: http://www.cnblogs.com/wesky/p/5624002.html
Related Article

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.