setRGB("999999"); $font_color = new PHPExcel_Style_Color(); $font_color->setRGB("ffffff"); $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0) ->setTitle("Book"); $col = 0; $field_ary = array( array ( "title"=>"#", "width"=>10 ), array ( "title"=>"ISBN", "width"=>100 ), array ( "title"=>"書名", "width"=>100 ), array ( "title"=>"定價", "width"=>100 ), array ( "title"=>"作者", "width"=>100 ), array ( "title"=>"類別", "width"=>100 ), array ( "title"=>"出版社", "width"=>100 ), array ( "title"=>"內容簡介", "width"=>100 ), array ( "title"=>"出版日期", "width"=>100 ), ); function getNameFromNumber($num) { $numeric = $num % 26; $letter = chr(65 + $numeric); $num2 = intval($num / 26); if ($num2 > 0) { return getNameFromNumber($num2 - 1) . $letter; } else { return $letter; } } foreach ( $field_ary as $k => $v ) { $objPHPExcel->getActiveSheet()->SetCellValueByColumnAndRow($col, 1, $v["title"]); $objPHPExcel->getActiveSheet()->getStyle( getNameFromNumber ( $col )."1")->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet()->getStyle( getNameFromNumber ( $col )."1")->getFill()->setStartColor($bg_color); $objPHPExcel->getActiveSheet()->getStyle( getNameFromNumber ( $col )."1")->getFont()->setColor($font_color); $objPHPExcel->getActiveSheet()->getColumnDimension( getNameFromNumber ( $col ) )->setWidth( $v["width"] ); $col ++; } $sql="SELECT * FROM `node` WHERE `type` = 'book' ORDER BY `id` ASC"; $rs=$db->query($sql); $rows = 2; $counter = 1; while ( $row = $db->fetch_array($rs) ) { $sql_tag = "SELECT * FROM `tag` WHERE `id` = '".$row['tag']."'"; $row_tag = $db->getrow($sql_tag); $sql_publisher = "SELECT * FROM `publication` where `id` = '".$row['publisher']."'"; $row_publisher = $db->getrow($sql_publisher); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ( 0, $rows, $counter ); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ( 1, $rows, convert("'".$row["isbn"]."'") ); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ( 2, $rows, convert($row["title"]) ); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ( 3, $rows, convert($row["price"]) ); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ( 4, $rows, convert($row["author"]) ); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ( 5, $rows, convert($row_tag["name"]) ); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ( 6, $rows, convert($row_publisher["name"]) ); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ( 7, $rows, convert($row["content"]) ); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ( 8, $rows, convert($row["publishing_date"]) ); $counter ++; $rows ++; } function convert ( $text ) { return html_entity_decode ( strip_tags ( $text ), 0, "UTF-8" ); } autoSize ( $objPHPExcel, $col ); $name = ""; $filename = "Book - [".date("Y-m-d Hi")."] ".$name.".xls"; header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="'.$filename.'"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save("php://output"); function autoSize( $objPHPExcel, $col ) { for ( $i = 0; $i < $col; $i++ ) { $objPHPExcel->getActiveSheet()->getColumnDimension( chr ( 65 + $i ))->setAutoSize(true); } } ?>