public function _exportXLS() {
vendor("PHPExcel.PHPExcel");
$xlsTitle = iconv('utf-8', 'gb2312', '文件名'); //文件名称
$fileName = '文件名' . date('_YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
//$filename = "Registration".date("YmdHis").".xls";
//设定缓存模式为经gzip压缩后存入cache(还有多种方式请百度)
// $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_sqlite;
// $cacheSettings = array();
// \PHPExcel_Settings::setCacheStorageMethod($cacheMethod,$cacheSettings);
//实例化
$objPHPExcel = new \PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();
//设置宽度
$sheet->getColumnDimension('A')->setWidth(20);
$rows = range(A, Z); //超过Z请使用\PHPExcel_Cell::stringFromColumnIndex($i);
$head = array('表头1', '表头2');
foreach ($head as $i => $v) {
$sheet->setCellValue($rows[$i] . '1', $v);
}
//列数超过Z使用下面foreach
//foreach ($head as $i => $v) {
// $k=\PHPExcel_Cell::stringFromColumnIndex($i);
//$sheet->setCellValue($k.'1', $v);
//}
// Redirect output to a client’s web browser (Excel5)
// header('Content-Type: application/vnd.ms-excel');
// header('Content-Disposition: attachment;filename="'.$filename.'"');
// header('Cache-Control: max-age=0');
// // If you're serving to IE 9, then the following may be needed
// header('Cache-Control: max-age=1');
//
// // If you're serving to IE over SSL, then the following may be needed
// header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
// header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
// header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
// header ('Pragma: public'); // HTTP/1.0
$_M = M(CONTROLLER_NAME);
$condition = $this->_getCondition();
//分段取出数据,以防取出太多数据
$offset = 0;
$num = 1000;
$j = 2;
do {
//读取待导出的数据
$dataset = $_M->alias("mi")->where($condition)->limit($offset, $num)->select();
if (!empty($dataset)) {
// 逐行取出数据,不浪费内存
foreach ($dataset as $vo) {
//格式化数据
$row = array(
$vo['title1'], $vo['title2']
);
foreach ($row as $i => $v) {
$sheet->setCellValue($rows[$i] . $j, $v);
}
//列数超过Z使用下面foreach
//foreach ($row as $i => $v) {
// $k=\PHPExcel_Cell::stringFromColumnIndex($i);
// $sheet->setCellValue($k.$j, $v);
// }
//导出图片
if (!empty($vo['logo'])) {
$objDrawing = new \PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Photo');
$objDrawing->setDescription('Photo');
$objDrawing->setPath('Uploads/' . $vo['logo']);
$objDrawing->setHeight(170);
$objDrawing->setWidth(120);
$objDrawing->setCoordinates('C' . $j);
$objDrawing->setWorksheet($sheet);
$sheet->getRowDimension($j)->setRowHeight(50);
}
$j++;
}
$offset += $num;
ob_end_clean(); // Added by me
ob_start(); // Added by me
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}
} while (!empty($dataset));
}
vendor("PHPExcel.PHPExcel");
$xlsTitle = iconv('utf-8', 'gb2312', '文件名'); //文件名称
$fileName = '文件名' . date('_YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
//$filename = "Registration".date("YmdHis").".xls";
//设定缓存模式为经gzip压缩后存入cache(还有多种方式请百度)
// $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_sqlite;
// $cacheSettings = array();
// \PHPExcel_Settings::setCacheStorageMethod($cacheMethod,$cacheSettings);
//实例化
$objPHPExcel = new \PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();
//设置宽度
$sheet->getColumnDimension('A')->setWidth(20);
$rows = range(A, Z); //超过Z请使用\PHPExcel_Cell::stringFromColumnIndex($i);
$head = array('表头1', '表头2');
foreach ($head as $i => $v) {
$sheet->setCellValue($rows[$i] . '1', $v);
}
//列数超过Z使用下面foreach
//foreach ($head as $i => $v) {
// $k=\PHPExcel_Cell::stringFromColumnIndex($i);
//$sheet->setCellValue($k.'1', $v);
//}
// Redirect output to a client’s web browser (Excel5)
// header('Content-Type: application/vnd.ms-excel');
// header('Content-Disposition: attachment;filename="'.$filename.'"');
// header('Cache-Control: max-age=0');
// // If you're serving to IE 9, then the following may be needed
// header('Cache-Control: max-age=1');
//
// // If you're serving to IE over SSL, then the following may be needed
// header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
// header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
// header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
// header ('Pragma: public'); // HTTP/1.0
$_M = M(CONTROLLER_NAME);
$condition = $this->_getCondition();
//分段取出数据,以防取出太多数据
$offset = 0;
$num = 1000;
$j = 2;
do {
//读取待导出的数据
$dataset = $_M->alias("mi")->where($condition)->limit($offset, $num)->select();
if (!empty($dataset)) {
// 逐行取出数据,不浪费内存
foreach ($dataset as $vo) {
//格式化数据
$row = array(
$vo['title1'], $vo['title2']
);
foreach ($row as $i => $v) {
$sheet->setCellValue($rows[$i] . $j, $v);
}
//列数超过Z使用下面foreach
//foreach ($row as $i => $v) {
// $k=\PHPExcel_Cell::stringFromColumnIndex($i);
// $sheet->setCellValue($k.$j, $v);
// }
//导出图片
if (!empty($vo['logo'])) {
$objDrawing = new \PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Photo');
$objDrawing->setDescription('Photo');
$objDrawing->setPath('Uploads/' . $vo['logo']);
$objDrawing->setHeight(170);
$objDrawing->setWidth(120);
$objDrawing->setCoordinates('C' . $j);
$objDrawing->setWorksheet($sheet);
$sheet->getRowDimension($j)->setRowHeight(50);
}
$j++;
}
$offset += $num;
ob_end_clean(); // Added by me
ob_start(); // Added by me
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}
} while (!empty($dataset));
}