-
- /**
- * Export excel with styles
- * edit: bbs.it-home.org
- */
- include 'Writer.php';
-
- /* *** Prepare to export data*** */
- $head = 'One Week Schedule ';
- $data = array('Monday' => array( array('time' => '09:00', 'event' => 'Company regular meeting'),
- array('time' = > '14:00', 'event' => 'Department regular meeting')
- ),
- 'Tuesday' => array( array('time' => '09:30', 'event' => ; 'Breakfast with Mr. Stinsen')),
- 'Wednesday' => array(array('time' => '12:10', 'event' => 'Market Intermediate Report'),
- array ('time' => '15:30', 'event' => 'Marketing Department Strategic Deployment Meeting') ),
- 'Thursday' => array( array('time' => '', ' event' => '')),
- 'Friday' => array( array('time' => '16:00', 'event' => 'WoC Stock Seminar'),
- array( 'time' => '17:00', '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 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 format
- $dayFormat = &$workbook->addFormat(array('Size' => 12, 'Align' => 'center', 'VAlign' => 'vcenter', 'FgColor' => 'green', 'Color ' => 'white', 'Border' => '1'));//Define the format
- $dataFormat = &$workbook->addFormat(array('Size' => 10, 'Align' = > 'left', 'Border' => '1', 'Color' => 'black', 'FgColor'=> 'cyan'));//Define 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
-
- $r = 0;
- $sheet->write(0, $r, $head, $headFormat); // Table title
- $sheet->mergeCells(0, 0, 0, 2); // Display across columns
-
- $r++; // Data starts from row 2
- foreach ($data as $day => $events){
- $c = 0;
- $sheet->write($r, $c , $day, $dayFormat);
- if (!$events){
- // No plans for the day
- $r++;
- } else {
- $startRow = $r;
- foreach ($events as $e){
- $c = 1;
- $sheet->write($r, $c++, $e['time'], $dataFormat); // Write data to the worksheet
- $sheet->write($r, $c++, $e['event'], $dataFormat); // Write data to the worksheet
- $r++;
- }
- // Merge $day cells
- $sheet->mergeCells($startRow, 0, $r - 1 , 0);
- }
- }
- $workbook->close(); // Complete download
- ?>
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.
|