...
|
...
|
@@ -278,203 +278,147 @@ class Litestoregoods extends Backend |
|
|
/**
|
|
|
* 导出
|
|
|
*/
|
|
|
public function export()
|
|
|
{
|
|
|
//1.从数据库中导出需要进行要导出的数据
|
|
|
$goods = new Goods();
|
|
|
$list = Db::name('litestore_goods_spec')->alias('c')
|
|
|
->join('fa_litestore_goods g', 'g.goods_id = c.goods_id', 'LEFT')
|
|
|
->select();
|
|
|
// ->each(function (&$item) {
|
|
|
// $item['goods'] = Db::name('litestore_goods')
|
|
|
// ->where('goods_id', $item['goods_id'])->find();
|
|
|
// });
|
|
|
if (empty($list)) {
|
|
|
$this->error('没有可导出的数据');
|
|
|
}
|
|
|
//重要补助
|
|
|
//2.加载PHPExcle类库
|
|
|
vendor('PHPExcel.PHPExcel');
|
|
|
//3.实例化PHPExcel类
|
|
|
$objPHPExcel = new \PHPExcel();
|
|
|
//4.激活当前的sheet表
|
|
|
$objPHPExcel->setActiveSheetIndex(0);
|
|
|
//5.设置表格头(即excel表格的第一行)
|
|
|
$objPHPExcel->setActiveSheetIndex(0)
|
|
|
->setCellValue('A1', '商品ID')
|
|
|
->setCellValue('B1', '商品名称')
|
|
|
->setCellValue('C1', '商品原价')
|
|
|
->setCellValue('D1', '折扣')
|
|
|
->setCellValue('E1', '商品现价')
|
|
|
->setCellValue('F1', '已销数量')
|
|
|
->setCellValue('G1', '导出时间');
|
|
|
// ->setCellValue('G1', '院系')
|
|
|
// ->setCellValue('H1', '专业');
|
|
|
//设置A列水平居中
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
|
|
|
->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
|
|
|
//设置单元格宽度
|
|
|
//6.循环刚取出来的数组,将数据逐一添加到excel表格。
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(10);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
|
|
|
|
|
|
for ($i = 0; $i < count($list); $i++) {
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $list[$i]['goods_id']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['goods_name']);//商品名称
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['line_price']);//原价
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $list[$i]['discount']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['goods_price']);//标签码
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $list[$i]['goods_sales']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), datetime(time()));//标签码
|
|
|
}
|
|
|
//7.设置保存的Excel表格名称
|
|
|
// $filename = '商品信息' . date('ymd', time()) . '.xls';
|
|
|
// //8.设置当前激活的sheet表格名称;
|
|
|
// $objPHPExcel->getActiveSheet()->setTitle('商品信息');
|
|
|
// //9.设置浏览器窗口下载表格
|
|
|
ob_end_clean();
|
|
|
ob_start();
|
|
|
// header("Content-Type: application/force-download");
|
|
|
// header("Content-Type: application/octet-stream");
|
|
|
// header("Content-Type: application/download");
|
|
|
// header('Content-Disposition:inline;filename="' . $filename . '"');
|
|
|
//// 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='.$filename.'');
|
|
|
////// header("Content-Transfer-Encoding:binary");
|
|
|
////// //生成excel文件
|
|
|
////// $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
|
|
|
////// ini_set('display_errors', TRUE);
|
|
|
////// ini_set('display_startup_errors', TRUE);
|
|
|
//// header('pragma:public');
|
|
|
//// header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $filename . '.xls"');
|
|
|
//// header("Content-Disposition:inline;filename=$filename.xls"); //attachment新窗口打印inline本窗口打印
|
|
|
// $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
|
|
|
// $objWriter->save('php://output');
|
|
|
$title = 'ahhah';
|
|
|
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
header('Content-Disposition: attachment;filename="' . $title . '.xlsx"');
|
|
|
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
|
|
|
|
|
|
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
|
|
|
$objWriter->save('php://output'); //文件通过浏览器下载
|
|
|
exit();
|
|
|
}
|
|
|
|
|
|
// public function export()
|
|
|
// {
|
|
|
// if ($this->request->isPost()) {
|
|
|
// set_time_limit(0);
|
|
|
// $search = $this->request->post('search');
|
|
|
// $ids = $this->request->post('ids');
|
|
|
// $filter = $this->request->post('filter');
|
|
|
// $op = $this->request->post('op');
|
|
|
// $columns = $this->request->post('columns');
|
|
|
//
|
|
|
// $excel = new PHPExcel();
|
|
|
//
|
|
|
// $excel->getProperties()
|
|
|
// ->setCreator("FastAdmin")
|
|
|
// ->setLastModifiedBy("FastAdmin")
|
|
|
// ->setTitle("标题")
|
|
|
// ->setSubject("Subject");
|
|
|
// $excel->getDefaultStyle()->getFont()->setName('Microsoft Yahei');
|
|
|
// $excel->getDefaultStyle()->getFont()->setSize(12);
|
|
|
//
|
|
|
// $this->sharedStyle = new PHPExcel_Style();
|
|
|
// $this->sharedStyle->applyFromArray(
|
|
|
// array(
|
|
|
// 'fill' => array(
|
|
|
// 'type' => PHPExcel_Style_Fill::FILL_SOLID,
|
|
|
// 'color' => array('rgb' => '000000'),
|
|
|
// ),
|
|
|
// 'font' => array(
|
|
|
// 'color' => array('rgb' => "000000"),
|
|
|
// ),
|
|
|
// 'alignment' => array(
|
|
|
// 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
|
|
|
// 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
|
|
|
// 'indent' => 1,
|
|
|
// ),
|
|
|
// 'borders' => array(
|
|
|
// 'allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
|
|
|
// ),
|
|
|
// ));
|
|
|
//
|
|
|
// $worksheet = $excel->setActiveSheetIndex(0);
|
|
|
// $worksheet->setTitle('标题');
|
|
|
//
|
|
|
// $whereIds = $ids == 'all' ? '1=1' : ['id' => ['in', explode(',', $ids)]];
|
|
|
// $this->request->get(['search' => $search, 'ids' => $ids, 'filter' => $filter, 'op' => $op]);
|
|
|
// list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
|
|
//
|
|
|
// $line = 1;
|
|
|
// $list = [];
|
|
|
// $this->model
|
|
|
// ->field($columns)
|
|
|
// ->where($where)
|
|
|
// ->where($whereIds)
|
|
|
// ->chunk(100, function ($items) use (&$list, &$line, &$worksheet) {
|
|
|
// $styleArray = array(
|
|
|
// 'font' => array(
|
|
|
// 'bold' => true,
|
|
|
// 'color' => array('rgb' => 'FF0000'),
|
|
|
// 'size' => 15,
|
|
|
// 'name' => 'Verdana',
|
|
|
// ));
|
|
|
// $list = $items = collection($items)->toArray();
|
|
|
// foreach ($items as $index => $item) {
|
|
|
// $line++;
|
|
|
// $col = 0;
|
|
|
// foreach ($item as $field => $value) {
|
|
|
//
|
|
|
// $worksheet->setCellValueByColumnAndRow($col, $line, $value);
|
|
|
// $worksheet->getStyleByColumnAndRow($col, $line)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
|
|
|
// $worksheet->getCellByColumnAndRow($col, $line)->getStyle()->applyFromArray($styleArray);
|
|
|
// $col++;
|
|
|
// }
|
|
|
// }
|
|
|
// });
|
|
|
// $first = array_keys($list[0]);
|
|
|
// foreach ($first as $index => $item) {
|
|
|
// $worksheet->setCellValueByColumnAndRow($index, 1, __($item));
|
|
|
// }
|
|
|
//
|
|
|
// $excel->createSheet();
|
|
|
// // Redirect output to a client’s web browser (Excel2007)
|
|
|
// $title = date("YmdHis");
|
|
|
// header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
// header('Content-Disposition: attachment;filename="' . $title . '.xlsx"');
|
|
|
// 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
|
|
|
// //1.从数据库中导出需要进行要导出的数据
|
|
|
// $list = Db::name('litestore_goods_spec')->alias('c')
|
|
|
// ->join('fa_litestore_goods g', 'g.goods_id = c.goods_id', 'LEFT')
|
|
|
// ->select();
|
|
|
// if (empty($list)) {
|
|
|
// $this->error('没有可导出的数据');
|
|
|
// }
|
|
|
// //重要补助
|
|
|
// //2.加载PHPExcle类库
|
|
|
// vendor('PHPExcel.PHPExcel');
|
|
|
// //3.实例化PHPExcel类
|
|
|
// $objPHPExcel = new \PHPExcel();
|
|
|
// //4.激活当前的sheet表
|
|
|
// $objPHPExcel->setActiveSheetIndex(0);
|
|
|
// //5.设置表格头(即excel表格的第一行)
|
|
|
// $objPHPExcel->setActiveSheetIndex(0)
|
|
|
// ->setCellValue('A1', '商品ID')
|
|
|
// ->setCellValue('B1', '商品名称')
|
|
|
// ->setCellValue('C1', '商品原价')
|
|
|
// ->setCellValue('D1', '折扣')
|
|
|
// ->setCellValue('E1', '商品现价')
|
|
|
// ->setCellValue('F1', '已销数量')
|
|
|
// ->setCellValue('G1', '导出时间');
|
|
|
// //设置A列水平居中
|
|
|
// $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
|
|
|
// ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
|
|
|
// //设置单元格宽度
|
|
|
// //6.循环刚取出来的数组,将数据逐一添加到excel表格。
|
|
|
// $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(10);
|
|
|
// $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
|
|
|
// $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
|
|
|
// $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
|
|
|
// $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(20);
|
|
|
// $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
|
|
|
//
|
|
|
// $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
|
|
|
// $objWriter->save('php://output');
|
|
|
// return;
|
|
|
// for ($i = 0; $i < count($list); $i++) {
|
|
|
// $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $list[$i]['goods_id']);//ID
|
|
|
// $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['goods_name']);//商品名称
|
|
|
// $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['line_price']);//原价
|
|
|
// $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $list[$i]['discount']);//ID
|
|
|
// $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['goods_price']);//标签码
|
|
|
// $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $list[$i]['goods_sales']);//ID
|
|
|
// $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), datetime(time()));//标签码
|
|
|
// }
|
|
|
// ob_end_clean();
|
|
|
// ob_start();
|
|
|
// $title = 'ahhah';
|
|
|
// header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
// header('Content-Disposition:attachment;filename="' . $title . '.xlsx"');
|
|
|
// header('Cache-Control: max-age=0');
|
|
|
// header('Cache-Control: max-age=1');
|
|
|
// 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
|
|
|
// $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
|
|
|
// $objWriter->save('php://output'); //文件通过浏览器下载
|
|
|
// exit();
|
|
|
// }
|
|
|
|
|
|
public function export()
|
|
|
{
|
|
|
if ($this->request->isPost()) {
|
|
|
set_time_limit(0);
|
|
|
$search = $this->request->post('search');
|
|
|
$ids = $this->request->post('ids');
|
|
|
$filter = $this->request->post('filter');
|
|
|
$op = $this->request->post('op');
|
|
|
$columns = $this->request->post('columns');
|
|
|
|
|
|
//$excel = new PHPExcel();
|
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
|
|
$spreadsheet->getProperties()
|
|
|
->setCreator("FastAdmin")
|
|
|
->setLastModifiedBy("FastAdmin")
|
|
|
->setTitle("标题")
|
|
|
->setSubject("Subject");
|
|
|
$spreadsheet->getDefaultStyle()->getFont()->setName('Microsoft Yahei');
|
|
|
$spreadsheet->getDefaultStyle()->getFont()->setSize(12);
|
|
|
|
|
|
$worksheet = $spreadsheet->setActiveSheetIndex(0);
|
|
|
$whereIds = $ids == 'all' ? '1=1' : ['id' => ['in', explode(',', $ids)]];
|
|
|
$this->request->get(['search' => $search, 'ids' => $ids, 'filter' => $filter, 'op' => $op]);
|
|
|
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
|
|
|
|
|
$line = 1;
|
|
|
|
|
|
//设置过滤方法
|
|
|
$this->request->filter(['strip_tags']);
|
|
|
|
|
|
//如果发送的来源是Selectpage,则转发到Selectpage
|
|
|
if ($this->request->request('keyField')) {
|
|
|
return $this->selectpage();
|
|
|
}
|
|
|
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
|
|
$total = $this->model
|
|
|
->where($whereIds)
|
|
|
->order($sort, $order)
|
|
|
->count();
|
|
|
|
|
|
$list = $this->model
|
|
|
->where($whereIds)
|
|
|
->order($sort, $order)
|
|
|
->limit($offset, $limit)
|
|
|
->select();
|
|
|
|
|
|
$list = collection($list)->toArray();
|
|
|
$result = array("total" => $total, "rows" => $list);
|
|
|
|
|
|
$first = array_keys($list[0]);
|
|
|
foreach ($first as $index => $item) {
|
|
|
$worksheet->setCellValueByColumnAndRow($index, 1, __($item));
|
|
|
}
|
|
|
|
|
|
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load(__DIR__ . '/muban/test.xls'); //读取模板
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); //指向激活的工作表
|
|
|
$worksheet->setTitle('模板测试标题');
|
|
|
|
|
|
for($i=0;$i<$total;++$i){
|
|
|
//向模板表中写入数据
|
|
|
$worksheet->setCellValue('A1', '模板测试内容'); //送入A1的内容
|
|
|
$worksheet->getCell('B2')->setValue($result['rows'][$i]['week']); //星期
|
|
|
$worksheet->getCell('d2')->setValue($result['rows'][$i]['genderdata']); //性别
|
|
|
$worksheet->getCell('f2')->setValue($result['rows'][$i]['hobbydata']); //爱好
|
|
|
$worksheet->getCell('b3')->setValue($result['rows'][$i]['title']); //标题
|
|
|
$worksheet->getCell('b4')->setValue($result['rows'][$i]['content']); //内容
|
|
|
|
|
|
|
|
|
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
|
|
|
//下载文档
|
|
|
header('Content-Type: application/vnd.ms-excel');
|
|
|
header('Content-Disposition: attachment;filename="'. date('Y-m-d') . $result['rows'][$i]['admin_id'] .'_test'.'.xlsx"');
|
|
|
header('Cache-Control: max-age=0');
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
|
$writer->save('php://output');
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|