header("Content-Type: text/html;charset=utf-8");
date_default_timezone_set('Asia/Shanghai');
$host = "
192.168.0.xxx"; //主机
$user = "
xxxxx"; //用户名
$passwd = "
xxxxx"; //密码
$sid = "orcl";
//oracle服务名
// Connect to Oracle server
$conn = oci_connect($user,$passwd,"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$host)(PORT = 1521))(CONNECT_DATA =(SID=$sid)))",'UTF8');
if (!$conn) {
die('Unable to connect or select database!');
}
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '
');
');
// Create new PHPExcel object
echo date('H:i:s') , " Create new PHPExcel object" , EOL;
$objPHPExcel = new PHPExcel();
// Set document properties
echo date('H:i:s') , " Set document properties" , EOL;
$objPHPExcel->getProperties()->setCreator("wangxuwei")
->setLastModifiedBy("wangxuwei")
->setTitle("PHPExcel Test document")
->setSubject("PHPExcel Test document")
->setDescription("Test document for PHPExcel, generated using PHP classes.")
->setKeywords("office PHPExcel php")
->setCategory("Test result file");
// Add some data
echo date('H:i:s') , " Add title" , EOL;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '学号')
->setCellValue('B1', '姓名')
->setCellValue('C1', '生日')
->setCellValue('D1', '性别')
->setCellValue('E1', '研究方向');
//执行一个简单查询,结果输出到excel文件
echo date('H:i:s') , " Add data from oracle server " , EOL;
$result=oci_parse($conn,'select sno,sname,bdate,sex,dir from student');
oci_execute($result); //执行SQL
$i=1;
while($row=oci_fetch_array($result, OCI_BOTH))
{
$i++;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i,$row['SNO'])
->setCellValue('B'.$i,$row['SNAME'])
->setCellValue('C'.$i,$row['BDATE'])
->setCellValue('D'.$i,$row['SEX'])
->setCellValue('E'.$i,$row['DIR']);
}
// Rename worksheet
echo date('H:i:s') , " Rename worksheet" , EOL;
$objPHPExcel->getActiveSheet()->setTitle('学生信息表');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Save Excel 2007 file
echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$callStartTime = microtime(true);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
// Save Excel 95 file
echo date('H:i:s') , " Write to Excel5 format" , EOL;
$callStartTime = microtime(true);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
// Echo done
echo date('H:i:s') , " Done writing files" , EOL;
echo 'Files have been created in ' , getcwd() , EOL;
// Clean up
oci_free_statement($result);
oci_close($conn);