namespace Home\Controller;
use Think\Controller;
class PointController extends CommonController {
public function daochu(){
$info = M('integral_filllogs as i')->join('jn_member as m on i.mid = m.id')
->join('jn_card as c on c.id = m.card_id')
->field('i.id,i.integral,m.member_name,m.phone,m.member_card_id,m.add_time,c.card_name')
->where($where)->find();
import("Org.Util.PHPExcel");
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
$objPHPExcel = new \PHPExcel();
import("Org.Util.PHPExcel.Reader.Excel5");
$objPHPExcel->getProperties()->setCreator("Admin")//创建人
->setLastModifiedBy("A")//最后修改人
->setTitle("积分EXCEL导出")//标题
->setSubject("积分EXCEL导出")//题目
->setDescription("产品")//描述
->setKeywords("excel")//关键字
->setCategory("result file"); //种类
$objPHPExcel->setActiveSheetIndex(0)
->setCellValueExplicit('A1', "序号")
->setCellValueExplicit('B1', "姓名")
->setCellValueExplicit('C1', "手机")
->setCellValueExplicit('D1', "卡号")
->setCellValueExplicit('E1', "卡类型")
->setCellValueExplicit('F1', "积分")
->setCellValueExplicit('G1', "办卡日期")
->setCellValueExplicit('H1', "门店");
// ->setCellValueExplicit('I1', "门店");
$objActSheet = $objPHPExcel->getActiveSheet();
foreach ($info as $k => $v) {
$k = $k + 1;
$num = $k + 1;
$objActSheet
->setCellValueExplicit('A' . $num, $v['id'], \PHPExcel_Cell_DataType::TYPE_STRING)
->setCellValueExplicit('B' . $num, $v['member_name'], \PHPExcel_Cell_DataType::TYPE_STRING)
->setCellValueExplicit('C' . $num, $v['phone'], \PHPExcel_Cell_DataType::TYPE_STRING)
->setCellValueExplicit('D' . $num, $v['member_card_id'], \PHPExcel_Cell_DataType::TYPE_STRING)
->setCellValueExplicit('E' . $num, ($v['member_name']), \PHPExcel_Cell_DataType::TYPE_STRING)
->setCellValueExplicit('F'.$num, $v['integral'],\PHPExcel_Cell_DataType::TYPE_STRING)
->setCellValueExplicit('G' . $num, $v['add_time'], \PHPExcel_Cell_DataType::TYPE_STRING)
->setCellValueExplicit('H'.$num,$shopname['shop_name'], \PHPExcel_Cell_DataType::TYPE_STRING);
$objActSheet->getColumnDimension('B')->setWidth(20);
$objActSheet->getColumnDimension('C')->setWidth(20);
$objActSheet->getColumnDimension('D')->setWidth(20);
$objActSheet->getColumnDimension('E')->setWidth(20);
$objActSheet->getColumnDimension('F')->setWidth(20);
$objActSheet->getColumnDimension('G')->setWidth(20);
$objActSheet->getColumnDimension('H')->setWidth(20);
$objActSheet->getColumnDimension('I')->setWidth(20);
$objActSheet->getColumnDimension('J')->setWidth(20);
$objActSheet->getColumnDimension('K')->setWidth(20);
$objActSheet->getColumnDimension('L')->setWidth(20);
$objActSheet->getColumnDimension('M')->setWidth(20);
$objActSheet->getColumnDimension('N')->setWidth(20);
}
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . time() . '.xls"');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
//...测试导出图片
public function testDown(){
$M=M('entry');
$data=$M->where(1)->select();
// 导出Exl
import("Org.Util.PHPExcel");
import("Org.Util.PHPExcel.Worksheet.Drawing");
import("Org.Util.PHPExcel.Writer.Excel2007");
$objPHPExcel = new \PHPExcel();
$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
$objActSheet = $objPHPExcel->getActiveSheet();
// 水平居中(位置很重要,建议在最初始位置)
$objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->setCellValue('A1', '姓名');
$objActSheet->setCellValue('B1', '手机号');
$objActSheet->setCellValue('C1', '头像');
$objActSheet->setCellValue('D1', '学校');
$objActSheet->setCellValue('E1', '特长');
$objActSheet->setCellValue('F1', '喜欢明星');
// 设置个表格宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(16);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(16);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(40);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(40);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(40);
// 垂直居中
$objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
foreach($data as $k=>$v){
$k +=2;
$objActSheet->setCellValue('A'.$k, $v['name']);
$objActSheet->setCellValue('B'.$k, $v['mobile']);
// 图片生成
$objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
$objDrawing[$k]->setPath('./Uploads/Images/2016-05-16/57397d14153f5.jpg');
// 设置宽度高度
$objDrawing[$k]->setHeight(40); //照片高度
$objDrawing[$k]->setWidth(80); //照片宽度
/*设置图片要插入的单元格*/
$objDrawing[$k]->setCoordinates('C'.$k);
// 图片偏移距离
$objDrawing[$k]->setOffsetX(12);
$objDrawing[$k]->setOffsetY(12);
$objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
// 表格内容
$objActSheet->setCellValue('D'.$k, $v['school']);
$objActSheet->setCellValue('E'.$k, $v['specialty']);
$objActSheet->setCellValue('F'.$k, $v['love_star']);
// 表格高度
$objActSheet->getRowDimension($k)->setRowHeight(80);
}
$fileName = '报名表';
$date = date("Y-m-d",time());
$fileName .= "_{$date}.xls";
$fileName = iconv("utf-8", "gb2312", $fileName);
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=\"$fileName\"");
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}
我的代码,既然之前一模一样的代码可以导出,那说明代码是没问题的,是不是导出数据太大,另外,没反应是什么表现,报错没有
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
说的是这行有错误
检查一下phpexcel文件是否存在?
或者加一个
require_once DIR . '/../' . 'simplewind\vendor\PHPExcel\PHPExcel.php';
实际地址填你phpexcel插件的地址。
我其实是因为另一个问题进来的。。。。点击导出后没反应,但是又不报错,但是另一个按钮却可以导出。
后来发现原来是没有return结果。
比如:
$result = ExcelWrite::excelExport('test',$init,$result,true);
return $result;
写成了
ExcelWrite::excelExport('test',$init,$result,true);
return $result;
少写了一个等于...然后那个$result之前有赋值其他值了,所以不会报错,只是提示下载
捂脸,如果有人发生类似错误的话,请不要重蹈我的覆辙。