PHPExcel插件的使用
下载phpExcel插件,将下载的文件中的classes文件夹下的phpexcel文件夹复制到项目下。
PHPExcel文件导入
文件导入的界面文件如下:
<form method="post" action="fileInput.php" enctype="multipart/form-data" >
<input type="file" name="file" id="file"/>
<input type="submit" name="submit" id="addPro" value="导入"/>
</form>
fileInput.php如下:
$file = $_FILES['file'];
$tmp_name = $file['tmp_name'];
//获取当前脚本所在路径
$dir = dirname(__FILE__);
require $dir."/phpexcel/PHPExcel/IOFactory.php";//引入读取excel类文件
//加载上传的临时文件
$objPHPExcel = PHPExcel_IOFactory::load($tmp_name);
$flag = 0;
//获取excel中的第一个sheet对象
$sheet = $objPHPExcel->getSheet(0);
//获取excel中记录的行数
$rowCount = $sheet->getHighestRow();
//获取excel中记录的列数
$colCount = $sheet->getHighestColumn();
$pd = new ProblemData();
//逐行从excel表中的第2行开始插入。
for($i = 2; $i <= $rowCount; $i++){
//遍历列
for($j = 'A'; $j <= $colCount; $j++){
$value = $objPHPExcel->getActiveSheet()->getCell("$j$i")->getValue();
if($j == 'A'){
$type = (int)$value;
}else if($j == 'B') {
$chapter = (int)$value;
}else if($j == 'C') {
$stem = addslashes($value);
}else if($j == 'D') {
$option1 = addslashes($value);
}else if($j == 'E') {
$option2 = addslashes($value);
}else if($j == 'F') {
$option3 = addslashes($value);
}else if($j == 'G') {
$option4 = addslashes($value);
}else if($j == 'H') {
$answer = addslashes($value);
}else if($j == 'I') {
$analyze = addslashes($value);
}
}
$problem = new Problem($type,$chapter,$stem,$option1,$option2,$option3,$option4,$answer,$analyze,$contributor,$contributorNumber);
//获取excel
if($pd->addProblem($problem)){
//成功导入
}else{
$flag = 1;
echo "导入这个题目的时候出错了:".'<br />';
echo "题目类型:".$problem->type.'<br />';
echo "题干:".$problem->stem.'<br />';
echo "选项1".$problem->option1.'<br />';
echo "选项2".$problem->option2.'<br />';
echo "正确答案:".$problem->answer.'<br />';
echo "解析:".$problem->analyze.'<br />';
break;
}
}
PHPExcel文件导出
从数据库表中导出记录到excel中去
<?php
/**
* Created by PhpStorm.
* User: LCL
* Date: 2017/3/22
* Time: 13:33
*/
require_once '../loaddata/ProblemData.php';
header("Content-type: text/html; charset=utf8");
$dir = dirname(__FILE__); //当前脚本路径
require $dir."/phpexcel/PHPExcel/IOFactory.php";//引入读取excel类文件
$objPHPExcel = new PHPExcel();//实例化PHPExcel对象,相当于创建一个excel文件(逻辑上类比)
//获取活动的sheet对象
$sheet = $objPHPExcel->getActiveSheet();
//设置sheet的标题
$sheet->setTitle("problems");
//excel表中的sheet中的第一行的值设置各字段值
$sheet->setCellValue("A1","题目类型");
$sheet->setCellValue("B1","章节");
$sheet->setCellValue("C1","题干");
$sheet->setCellValue("D1","选项1");
$sheet->setCellValue("E1","选项2");
$sheet->setCellValue("F1","选项3");
$sheet->setCellValue("G1","选项4");
$sheet->setCellValue("H1","正确答案");
$sheet->setCellValue("I1","解析");
$pd = new ProblemData();
//将数据库中的记录查询到$rows中,再填充到excel表格
if(($rows = $pd->getAllProblems()) != null){
$counts = count($rows);
$row = 2;
foreach ($rows as $one){
//设置单元格的值
$sheet->setCellValue("A".$row,(int)$one['pro_type']);
$sheet->setCellValue("B".$row,(int)$one['chapter_id']);
$sheet->setCellValue("C".$row,$one['pro_stem']);
$sheet->setCellValue("D".$row,$one['pro_option1']);
$sheet->setCellValue("E".$row,$one['pro_option2']);
$sheet->setCellValue("F".$row,$one['pro_option3']);
$sheet->setCellValue("G".$row,$one['pro_option4']);
$sheet->setCellValue("H".$row,$one['pro_answer']);
$sheet->setCellValue("I".$row,$one['pro_analyze']);
$row++;
}
//创建输出流并保存文件
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename="CSAPP.xls"');
header("Content-Transfer-Encoding:binary");
$objWriter->save('php://output');
}else{
alertMessage("导出题库出错","Location: http://hnucsapp.applinzi.com/server/views/problemManage.php");
}
?>