PHP implementation export with style Excel instance sharing

Source: Internet
Author: User
Tags ole pear
Sometimes the customer complains to you that the Excel format that the software exports to them is too ugly, perhaps this article will help you. You can also set colors, font size, bold, merged cells, and so on while exporting data in PHP.

When you do the export, you need to export the custom table or the exported Excel format is too ugly.

You need to set the color, font size, bold, merged cells, and so on.


PHP Code:

/*** Export File * @return String*/public function Export () {$file _name  = "Transcript-". Date ("Y-m-d h:i:s", Time ()), $file _suffix = " XLS "; Header (" Content-type:application/vnd.ms-excel "); header (" Content-disposition:attachment; Filename= $file _name. $file _suffix ");//According to the business, you do the template assignment. $this->display ();}

HTML code:

Let's look at a more convenient component

Here you need to use the Pear two packages Spreadsheet Excel Writer and OLE if there are no separate from http://pear.php.net/package/Spreadsheet_Excel_Writer/and http://pear.php.net/package/OLE/download, unzip and put in the Pear directory.

The full code is as follows:

<?phpinclude ' writer.php ';/* * * * * * * * * * * * * * * * * * * * * * * * * * * * $head = ' one Week Schedule '; $data = Array (' Monday ' =                    ' Time ' = ' ", ' event ' = ' Company regular meeting '), Array (' time ' = ' + ', ' event ' = ' departmental Meeting ')         ), ' Tuesday ' = = Array (' time ' = ' 09:30 ', ' event ' + ' and ' Mr. Stinsen Breakfast '), ' Wednesday ' = = Array (' time ' = ' 12:10 ', ' event ' = ' Market mid-order report '), Array (' time ' + ' 15: ' Event ' = ' marketing Strategy Deployment Meeting '), ' Thursday ' = = Array (' time ' = ' = ', ' event ' = ') '), ' F Riday ' = = Array (' time ' = ' 16:00 ', ' event ' = ' WoC Stock Seminar '), Array (' time ' = ' 1 "' event ' = ' fly to Wall Street '), Array (' time ' = ' 21:00 ', ' event ' = ' Meet Clinton ')"); * * * * * * * * * * * * */$ workbook = new Spreadsheet_excel_writer (); $filename = Date (' Ymdhis '). XLS ';//csv$workbook->send ($filename); Send Excel file name for download $workbook->seTversion (8); $sheet = & $workbook->addworksheet ("Sheet1");          Create worksheet $sheet->setinputencoding (' Utf-8 '); Character Set $headformat = & $workbook->addformat (Array (' Size ' = +, ' Align ' = ' center ', ' Color ' = ' white ', ' Fg Color ' = = ' brown ', ' Bold ' = ' 1 ', ' Border ' = ' 1 ');//define Format $dayformat = & $workbook->addformat ('  Size ' = ' +, ' Align ' + ' center ', ' valign ' = ' vcenter ', ' fgcolor ' and ' green ', ' Color ' = ' white ', ' Border '  = ' 1 ');//define Format $dataformat = & $workbook->addformat (' Size ' = ' = ', ' Align ' = ' left ', ' Border ' =   ' 1 ', ' Color ' = ' black ', ' fgcolor ' = ' cyan ');//define the format $sheet->setcolumn (0, 0, 20);   Set width $sheet->setcolumn (1, 1, 15);   Set width $sheet->setcolumn (2, 2, 30);   Set width = 0;   $sheet->write (0, $r, $head, $headFormat);   Table title $sheet->mergecells (0, 0, 0, 2);   Cross-column display $r++;   Data starts from line 2nd, foreach ($data as $day + = $events) {$c = 0; $sheet->write ($r, $c, $day, $dayFormat);   if (! $events) {//the same day no plan $r + +;     } else {$startRow = $r;        foreach ($events as $e) {$c = 1;   $sheet->write ($r, $c + +, $e [' time '], $dataFormat);   Worksheets Write Data $sheet->write ($r, $c + +, $e [' event '], $dataFormat);     Worksheets Write Data $r + +;   }//Merge $day cell $sheet->mergecells ($startRow, 0, $r-1, 0); }} $workbook->close (); Complete the Download?>

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.