Phpexcel export based on thinkphp development

Source: Internet
Author: User

First we need to go to phpexcel official website to download the open source package

Download good later only need to put inside the classes folder extract out can use!

In thinkphp, the namespace of the file is still very important, I try to follow the other third-party names to write namespace but I still can't, and I did it in the end.

First step: Rename classes to Excel, and rename the phpexcel.php in the folder to PHPExcel.class.php

Then put it into the catalog file

Then in the control layer import, the direct use of the words will be error can not find the class file ... Perhaps the kernel inside the routing problem, so I was in the instantiation of the time to write the detailed address can actually come out

$objPHPExcel = new \org\excel\phpexcel ();   That's it

However, some methods of such a file are not available, if the call needs to precede the method with a \

such as this:
$objSheet->getstyle ("A1:z1")->getfill ()->setfilltype (\phpexcel_style_fill::fill_solid) Getstartcolor ()->setargb (' #00FF00 '); //Fill in the background color for the cell


Here is the specific code:

Excel Export
Public function Excel () {

//Query SQL build array
$loan = M ("loan");
$data = $loan->select ();

$objPHPExcel = new \org\excel\phpexcel ();//Instantiating a class file is equivalent to creating a new Excel table on the desktop
$objSheet = $objPHPExcel->getactivesheet ();//Get Current Activity sheet Action object
$objSheet->settitle (' Loan_info ');//Modify the name for the current activity sheet

$objSheet->getdefaultstyle ()->getalignment ()->setvertical (\phpexcel_style_alignment::vertical_center) ->sethorizontal (\phpexcel_style_alignment::horizontal_center);//Set cells to center vertically and horizontally
$objSheet->getstyle ("A1:z1")->getfont ()->setname ("Microsoft Jas Black")->setsize (true);//Set the font, font size, bold for range of cells
$objSheet->getstyle ("A1:z1")->getfill ()->setfilltype (\phpexcel_style_fill::fill_solid) Getstartcolor ()->setargb (' #00FF00 ');//Fill in the background color for the cell

//Add a border
$styleArray = Array (
' Borders ' = Array (
' Outline ' = Array (
' Style ' = \phpexcel_style_border::border_thick,
' Color ' = = Array (' ARGB ' = ' #F0F8FF '),
),
),
);
$objSheet->getstyle ("A1")->applyfromarray ($styleArray);
$objSheet->getstyle ("B1")->applyfromarray ($styleArray);
$objSheet->getstyle ("C1")->applyfromarray ($styleArray);
$objSheet->getstyle ("D1")->applyfromarray ($styleArray);
$objSheet->getstyle ("E1")->applyfromarray ($styleArray);
$objSheet->getstyle ("F1")->applyfromarray ($styleArray);
$objSheet->getstyle ("G1")->applyfromarray ($styleArray);
$objSheet->getstyle ("H1")->applyfromarray ($styleArray);
$objSheet->getstyle ("I1")->applyfromarray ($styleArray);
$objSheet->getstyle ("J1")->applyfromarray ($styleArray);
$objSheet->getstyle ("K1")->applyfromarray ($styleArray);
$objSheet->getstyle ("L1")->applyfromarray ($styleArray);
$objSheet->getstyle ("M1")->applyfromarray ($styleArray);
$objSheet->getstyle ("N1")->applyfromarray ($styleArray);
$objSheet->getstyle ("O1")->applyfromarray ($styleArray);
$objSheet->getstyle ("P1")->applyfromarray ($styleArray);
$objSheet->getstyle ("Q1")->applyfromarray ($styleArray);
$objSheet->getstyle ("R1")->applyfromarray ($styleArray);
$objSheet->getstyle ("S1")->applyfromarray ($styleArray);
$objSheet->getstyle ("T1")->applyfromarray ($styleArray);
$objSheet->getstyle ("U1")->applyfromarray ($styleArray);
$objSheet->getstyle ("V1")->applyfromarray ($styleArray);
$objSheet->getstyle ("W1")->applyfromarray ($styleArray);



$objSheet->setcellvalue ("A1", "ID")
->setcellvalue ("B1", "Company Name")
->setcellvalue ("C1", "Company logo")
->setcellvalue ("D1", "link Address")
->setcellvalue ("E1", "loan corresponding Crowd")
->setcellvalue ("F1", "propaganda language")
->setcellvalue ("G1", "Minimum limit Range")
->setcellvalue ("H1", "Maximum limit Range")
->setcellvalue ("I1", "minimum term range")
->setcellvalue ("J1", "Maximum term range")
->setcellvalue ("K1", "interest rate")
->setcellvalue ("L1", "Fastest lending time")
->setcellvalue ("M1", "interest-bearing method")
->setcellvalue ("N1", "whether certified")
->setcellvalue ("O1", "Application Conditions")
->setcellvalue ("P1", "Application Process")
->setcellvalue ("Q1", "required materials for Application")
->setcellvalue ("R1", "Application Time")
->setcellvalue ("S1", "Update Time")
->setcellvalue ("T1", "Number of successful loans")
->setcellvalue ("U1", "Charge per click")
->setcellvalue ("V1", "whether shelves")
->setcellvalue ("W1", "Show ordinal Order");//fill in the appropriate values for the cell

//Loop fill
$j = 2;
foreach ($data as $key = = $val) {
$objSheet->setcellvalue ("A". $j, $val [' ID '])
->setcellvalue ("B". $j, $val [' Corporate '])
->setcellvalue ("C". $j, $val [' img '])
->setcellvalue ("D". $j, $val [' Corporate_url '])
->setcellvalue ("E". $j, $val [' person '])
->setcellvalue ("F". $j, $val [' propaganda '])
->setcellvalue ("G". $j, $val [' Xquota '])
->setcellvalue ("H". $j, $val [' Dquota '])
->setcellvalue ("I". $j, $val [' xterm '])
->setcellvalue ("J". $j, $val [' dterm '])
->setcellvalue ("K". $j, $val [' Rate '])
->setcellvalue ("L". $j, $val [' Qtime '])
->setcellvalue ("M". $j, $val [' type '])
->setcellvalue ("N". $j, $val [' Authentication '])
->setcellvalue ("O". $j, $val [' condition '])
->setcellvalue ("P". $j, $val [' Process '])
->setcellvalue ("Q". $j, $val [' Science '])
->setcellvalueexplicit ("R". $j, $val [' Apply_time '],\phpexcel_cell_datatype::type_string)//Set date fixed for the number of the appropriate will not become the official can't understand the format
->setcellvalueexplicit ("S". $j, $val [' Up_time '],\phpexcel_cell_datatype::type_string)//Set date fixed for the number of the appropriate will not become the official can't understand the format
->setcellvalue ("T". $j, $val [' Succ_person '])
->setcellvalue ("U". $j, $val [' Click_money '])
->setcellvalue ("V". $j, $val [' is_shelves '])
->setcellvalue ("W". $j, $val [' Show_number ']);
$j + +;
}

//Set File save naming, encoding, and open Save path permissions
$FN = "Loan_". Time (). ". XLS ";
Header (' Content-type:application/vnd.ms-excel; Charset=utf-8 ');
Header ("Content-disposition:attachment;filename= $fn");//Tell the browser which name to output
Header (' cache-control:max-age=0 ');//Disable caching
$objWriter = \phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 ');//Generate Excel file
$objWriter->save (' php://output ');//Completely open save path
Exit
}

Phpexcel export based on thinkphp development

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.