Export a style Excel file in PHP and export a style excel file in php.

Source: Internet
Author: User

Export a style Excel file in PHP and export a style excel file in php.

During work export, it is too difficult to export a custom table or discard the exported Excel format.

You need to set the color, font size, bold, and merge cells.

:


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 "); // assign values to the template based on the business. $ This-> display ();}

HTML code:

<Html xmlns: o = "urn: schemas-microsoft-com: office" xmlns: x = "urn: schemas-microsoft-com: office: excel "xmlns =" http://www.w3.org/TR/REC-html40 "> 

Let's look at a more convenient component.

Here you need to use PEAR's two software packages Spreadsheet Excel Writer and OLE, if not, you can download them from the http://pear.php.net/package/Spreadsheet_Excel_Writer/ and http://pear.php.net/package/OLE/ separately, unzip them to the PEAR directory.

The Code is as follows:

<? Phpinclude 'writer. php ';/***** prepare the exported data *****/$ head = 'one Week schedule '; $ data = array ('monday' => array ('time' => '09: 00', 'event' => 'regular meeting of the company '), array ('time' => '14: 00', 'event' => 'department meeting ')), 'tuesday' => array ('time' => '09: 30', 'event' => 'and Mr. stinsen breakfast '), 'wednesday' => array ('time' => '12: 10', 'event' => 'market medium-order Report '), array ('time' => '15: 30', 'event' => 'marketing Department strategic deployment meeting '), 'th Ursday '=> array ('time' => '', 'event' => '')), 'Friday' => array ('time' => '16: 00', 'event' => 'woc Stock seminar '), array ('time' => '17: 00', 'event' => 'fly to Walls'), array ('time' => '21: 00 ', 'event' => 'meet Clinton ');/********/$ workbook = new Spreadsheet_Excel_Writer (); $ filename = date('ymdhis'{.'xls '; // csv $ workbook-> send ($ filename); // send the Excel file name for download $ workbook-> setVersion (8); $ sheet = & $ Workbook-> addWorksheet ("Sheet1"); // create a worksheet $ sheet-> setInputEncoding ('utf-8 '); // character set $ headFormat = & $ workbook-> addFormat (array ('SIZE' => 14, 'align '=> 'center', 'color' => 'white ', 'fgcolor' => 'brown ', 'bold' => '1', 'border' => '1 ')); // define the format $ dayFormat = & $ workbook-> addFormat (array ('SIZE' => 12, 'align '=> 'center ', 'valign '=> 'vcenter', 'fgcolor' => 'green', 'color' => 'white', 'border' => '1 '));// Definition Format $ dataFormat = & $ workbook-> addFormat (array ('SIZE' => 10, 'align '=> 'left', 'border' => '1 ', 'color' => 'black', 'fgcolor' => 'cyany'); // defines the format $ sheet-> setColumn (0, 0, 20 ); // set the width $ sheet-> setColumn (1, 1, 15); // set the width $ sheet-> setColumn (2, 2, 30 ); // set the width of $ r = 0; $ sheet-> write (0, $ r, $ head, $ headFormat); // table title $ sheet-> mergeCells (0, 0, 0, 2); // cross-Column Display $ r ++; // data starting from row 2nd foreach ($ data as $ day => $ events ){ $ C = 0; $ sheet-> write ($ r, $ c, $ day, $ dayFormat); if (! $ Events) {// no plan for the current day $ r ++;} else {$ startRow = $ r; foreach ($ events as $ e) {$ c = 1; $ sheet-> write ($ r, $ c ++, $ e ['time'], $ dataFormat); // write data to a worksheet $ sheet-> write ($ r, $ c ++, $ e ['event'], $ dataFormat); // write data to a worksheet $ r ++ ;} // merge $ day cells $ sheet-> mergeCells ($ startRow, 0, $ r-1, 0) ;}}$ workbook-> close (); // complete the download?>

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.