function export_excel () {
$title = $_REQUEST['title'];
$title_data = $_REQUEST['title_data'];
$sql_para = $_REQUEST['sql_para'];
$title_data_arr = array();
if ( $title_data ) $title_data_arr = unserialize( base64_decode( $title_data ));
if ( $sql_para ) $sql_para = base64_decode( $sql_para );
$Letter = range('A','Z');
$new_title_data_arr = array();
foreach ( $title_data_arr as $k=>$row ) {
$row['letter'] = $Letter[$k];
$new_title_data_arr[] = $row;
}
$objExcel = $this->create_excel_obj ( "Aoba", $title ); //加載調用
$index = 2;
foreach ( $new_title_data_arr as $key=>$row ) {
$width = intval( $row['width']) * 3;
$letter_value = $row['letter'];
$objExcel->getActiveSheet()->getColumnDimension ( $letter_value )->setWidth( $width );
$location = $letter_value.$index;
$excel_title = str_ireplace( "<br>", "\n", $row['title']);
$this->setTitleVal( $location , $excel_title, $objExcel ); // 設置標題
}
$index++;
$data = $this->oracle->GetArray( $sql_para );
foreach ( $data as $sub_data ) {
$num = 0;
foreach ( $sub_data as $field=>$value ){
$location2 = $new_title_data_arr[$num]['letter'].$index;
if ( $field <> 'ID') {
$value = $this->replace_dropdown_value( $field, $value );
if ( empty( $value)) $value = ' ';
$this->setTitleVal001( $location2 , $value , $objExcel ); //設置值
$num++;
}
}
$index++;
}
$this->create_excel_foot( $objExcel, $title ); // 輸出並下載 Excel
}php
function create_excel_obj ( $Creator, $title ) {
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel.php';
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel/Writer/Excel2007.php';
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel/Writer/Excel5.php';
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel/IOFactory.php';
$objExcel = new PHPExcel();
$objExcel->getProperties()->setCreator( $Creator );
$objExcel->getProperties()->setLastModifiedBy( $Creator );
$objExcel->getProperties()->setTitle("Office 2003 XLS Test Document");
$objExcel->getProperties()->setSubject("Office 2003 XLS Test Document");
$objExcel->getProperties()->setDescription("Test document for Office 2003 XLS, generated using PHP classes.");
$objExcel->getProperties()->setKeywords("office 2003 openxml php");
$objExcel->getProperties()->setCategory("Test result file");
$objExcel->setActiveSheetIndex(0);
$objExcel->getActiveSheet()->setCellValue( 'A1', $title );
$objStyleA1 = $objExcel->getActiveSheet()->getStyle( 'A1' );
$objFontA1 = $objStyleA1->getFont();
$objFontA1->setSize( 16 );
$objFontA1->setBold(true);
return $objExcel;
}
sql
function setTitleVal( $index, $val , $objExcel ) {
$objExcel->getActiveSheet()->setCellValue( $index, $val );
$objStyleOBJ = $objExcel->getActiveSheet()->getStyle( $index );
$objFontOBJ = $objStyleOBJ->getFont();
$objFontOBJ->setSize(12);
$objFontOBJ->setBold(true);
$objStyleA5 = $objExcel->getActiveSheet()->getStyle( $index );
$objBorderA5 = $objStyleA5->getBorders();
$objBorderA5->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objBorderA5->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objBorderA5->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objBorderA5->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objBorderA5->getTop()->getColor()->setARGB('00000000'); // color
$objBorderA5->getBottom()->getColor()->setARGB('00000000'); // color
$objBorderA5->getLeft()->getColor()->setARGB('00000000'); // color
$objBorderA5->getRight()->getColor()->setARGB('00000000'); // color
$objFillA5 = $objStyleA5->getFill();
$objFillA5->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objFillA5->getStartColor()->setARGB('CCCCCCCC');
$objAlignA5 = $objStyleA5->getAlignment();
$objAlignA5->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objAlignA5->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
}
function setTitleVal001( $index, $val , $objExcel , $alignment ='', $dataType = '' ) {
if ( empty( $dataType)) {
$objExcel->getActiveSheet()->setCellValueExplicit( $index, $val, PHPExcel_Cell_DataType::TYPE_STRING );
} else if ( $dataType == 'N') {
$objExcel->getActiveSheet()->setCellValueExplicit( $index, $val, PHPExcel_Cell_DataType::TYPE_NUMERIC );
//$objExcel->getActiveSheet()->getStyle( $index )->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT );
}
}oracle
function create_excel_foot( $objExcel, $title ) {
$objExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BPersonal cash register&RPrinted on &D');
$objExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objExcel->getProperties()->getTitle() . '&RPage &P of &N');
// 設置頁方向和規模
$objExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
$objExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$objExcel->setActiveSheetIndex(0);
//$timestamp = time();
$filename = str_replace( " ", "-", $title ).date( 'YmdHis' ) ;
if($ex == '2007') { //導出excel2007文檔
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
} else { //導出excel2003文檔
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
}app