Sample code for exporting styled Excel files with PHP

WBOY
Release: 2016-07-25 08:55:27
Original
982 people have browsed it
  1. /**
  2. * Export excel with styles
  3. * edit: bbs.it-home.org
  4. */
  5. include 'Writer.php';
  6. /* *** Prepare to export data*** */
  7. $head = 'One Week Schedule ';
  8. $data = array('Monday' => array( array('time' => '09:00', 'event' => 'Company regular meeting'),
  9. array('time' = > '14:00', 'event' => 'Department regular meeting')
  10. ),
  11. 'Tuesday' => array( array('time' => '09:30', 'event' => ; 'Breakfast with Mr. Stinsen')),
  12. 'Wednesday' => array(array('time' => '12:10', 'event' => 'Market Intermediate Report'),
  13. array ('time' => '15:30', 'event' => 'Marketing Department Strategic Deployment Meeting') ),
  14. 'Thursday' => array( array('time' => '', ' event' => '')),
  15. 'Friday' => array( array('time' => '16:00', 'event' => 'WoC Stock Seminar'),
  16. array( 'time' => '17:00', 'event' => 'Fly to Wall Street'),
  17. array('time' => '21:00', 'event' => 'Meet Clinton' ))
  18. );
  19. /* *** *** */
  20. $workbook = new Spreadsheet_Excel_Writer();
  21. $filename = date('YmdHis').'.xls';//csv
  22. $workbook-> ;send($filename); // Send the Excel file name for download
  23. $workbook->setVersion( 8 );
  24. $sheet = &$workbook->addWorksheet("Sheet1"); // Create a worksheet
  25. $sheet->setInputEncoding('utf-8'); //Character set
  26. $headFormat = &$workbook->addFormat(array('Size' => 14, 'Align' => 'center', 'Color' => 'white', 'FgColor' => 'brown', 'Bold'=>'1', 'Border' => '1'));//Define format
  27. $dayFormat = &$workbook->addFormat(array('Size' => 12, 'Align' => 'center', 'VAlign' => 'vcenter', 'FgColor' => 'green', 'Color ' => 'white', 'Border' => '1'));//Define the format
  28. $dataFormat = &$workbook->addFormat(array('Size' => 10, 'Align' = > 'left', 'Border' => '1', 'Color' => 'black', 'FgColor'=> 'cyan'));//Define format
  29. $sheet->setColumn (0, 0, 20); // Set the width
  30. $sheet->setColumn(1, 1, 15); // Set the width
  31. $sheet->setColumn(2, 2, 30); // Set the width
  32. $r = 0;
  33. $sheet->write(0, $r, $head, $headFormat); // Table title
  34. $sheet->mergeCells(0, 0, 0, 2); // Display across columns
  35. $r++; // Data starts from row 2
  36. foreach ($data as $day => $events){
  37. $c = 0;
  38. $sheet->write($r, $c , $day, $dayFormat);
  39. if (!$events){
  40. // No plans for the day
  41. $r++;
  42. } else {
  43. $startRow = $r;
  44. foreach ($events as $e){
  45. $c = 1;
  46. $sheet->write($r, $c++, $e['time'], $dataFormat); // Write data to the worksheet
  47. $sheet->write($r, $c++, $e['event'], $dataFormat); // Write data to the worksheet
  48. $r++;
  49. }
  50. // Merge $day cells
  51. $sheet->mergeCells($startRow, 0, $r - 1 , 0);
  52. }
  53. }
  54. $workbook->close(); // Complete download
  55. ?>
Copy code

Code description: $sheet = &$workbook->addWorksheet("Sheet1"); // Create a worksheet and return the call to the worksheet. Multiple worksheets can be created in one Excel workbook. $headFormat = &$workbook->addFormat($param); // Create a format. The following available formats are available. Please refer to the official instructions http://pear.php.net/manual/en/package.fileformats.spreadsheet-excel -writer.spreadsheet-excel-writer-workbook.addformat.php Align

Bold

Bottom

Top

Left

Right

Border

BorderColor

BottomColor

TopColor

RightColor

LeftColor

FgColor

BgColor

Color

Pattern

Underline

TextRotation

Size

NumFormat

Script

$workbook->send($filename); //Send HTTP Header, prepare to download, $filename is the downloaded file name sheet->setColumn($startCol, $endCol, $width); // Set column width $sheet->write($row, $col, $data, $format); //Write data to the worksheet, $row The row number to write, starting from 0 $col column number to write, starting from 0 $data data $format Style created using addFormat() $sheet->mergeCells($startRow, $startCol, $endRow, $endCol); // Merge cells $workbook->close(); // Download completed

That’s it. If you have time, save the above code as xxx.php and test it yourself to see how it works. After all, programming is all about hands-on practice.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template