实时查询导出excel的技术实现

本文章主要是讲如何做一个实时查询导出excel的应用。
首先要复习的知识点是:
json格式数据特点 ,以及php操作json格式数据的函数(json_decode() json_encode()) ,phpexcel(http://phpexcel.codeplex.com/) 的使用,还有post请求与get请求的特点,我对这几个知识点不详细介绍,后面用的时候略讲
原理如下:
将查询后得到的数据以json格式存贮在显示查询结果的页面中,设置属性为隐藏,例如:

[html]
<form metho="post" action="">
<!–
//不用GET是因为GET方法传递数据的长度有限制
//注意<{data}>为结果数据 value后面的是单引号,不能用双引号,因为json格式数据中用的是双引号。 –> data=_POST[‘data’]; if(get_magic_quotes_gpc())//如果get_magic_quotes_gpc()是打开的 { data=stripslashes(data);//将字符串进行处理 } data=json_decode(data,true); objPHPExcel = new PHPExcel();

// Set properties
objPHPExcel->getProperties()->setCreator(“Maarten Balliauw”) ->setLastModifiedBy(“Maarten Balliauw”) ->setTitle(“Office 2007 XLSX Test Document”) ->setSubject(“Office 2007 XLSX Test Document”) ->setDescription(“Test document for Office 2007 XLSX, generated using PHP classes.”) ->setKeywords(“office 2007 openxml php”) ->setCategory(“Test result file”); sy=array("A","B","C","D","E","F","G","H","I","J","K","L","M","N");

num=count(data);
foreach(data[num-1] as key=>value){
objPHPExcel->setActiveSheetIndex(0)->setCellValue(sy[key].”1″, value,PHPExcel_Cell_DataType::TYPE_STRING);

}
i=2; foreach (data as key=>row){
if(i>num){
break;

}
j=0; foreach(row as innerkey=>innerrow){

objPHPExcel->setActiveSheetIndex(0)->setCellValue(sy[j].i, innerrow,PHPExcel_Cell_DataType::TYPE_STRING); //objPHPExcel->setActiveSheetIndex(0)->setCellValue("A".j, innerrow,PHPExcel_Cell_DataType::TYPE_STRING);
//echo innerrow.”
“;
j++;}

// Miscellaneous glyphs, UTF-8

// Rename sheet}

i++; } objPHPExcel->getActiveSheet()->setTitle(‘报表’);

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
objPHPExcel->setActiveSheetIndex(0); name=time();

// Redirect output to a client’s web browser (Excel5)
header(‘Content-Type: application/vnd.ms-excel’);
header(‘Content-Disposition: attachment;filename="’.name.’.xls”‘); header(‘Cache-Control: max-age=0’); objWriter = PHPExcel_IOFactory::createWriter(objPHPExcel, ‘Excel5’); objWriter->save(‘php://output’);
exit;
?>

[/php]
对上面代码不熟悉,参看PHPEXCLE文档