...
|
...
|
@@ -2,6 +2,7 @@ |
|
|
|
|
|
namespace app\admin\controller\litestore;
|
|
|
|
|
|
use addons\fastexport\library\ExportLib;
|
|
|
use app\admin\model\litestore\Litestoregoodsspec;
|
|
|
use app\api\model\Goods;
|
|
|
use app\common\controller\Backend;
|
...
|
...
|
@@ -12,7 +13,12 @@ use PHPExcel_Style; |
|
|
use PHPExcel_Style_Alignment;
|
|
|
use PHPExcel_Style_Border;
|
|
|
use PHPExcel_Style_Fill;
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
use think\Db;
|
|
|
use think\Exception;
|
|
|
use think\exception\PDOException;
|
|
|
|
|
|
/**
|
|
|
*
|
...
|
...
|
@@ -278,147 +284,88 @@ class Litestoregoods extends Backend |
|
|
/**
|
|
|
* 导出
|
|
|
*/
|
|
|
// public function export()
|
|
|
// {
|
|
|
// //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);
|
|
|
//
|
|
|
// 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));
|
|
|
//1.从数据库中导出需要进行要导出的数据
|
|
|
$list = Db::name('litestore_goods_spec')->alias('c')
|
|
|
->join('fa_litestore_goods g', 'g.goods_id = c.goods_id', 'LEFT')
|
|
|
->select();
|
|
|
$list = collection($list)->toArray();
|
|
|
foreach ($list as &$item) {
|
|
|
if (!empty($item['spec_sku_id'])) {
|
|
|
$sku_value = explode('_', $item['spec_sku_id']);
|
|
|
if (count($sku_value) > 1) {
|
|
|
$key = Db::name('litestore_spec_value')->where('id', $sku_value[0])->value('spec_value');
|
|
|
$value = Db::name('litestore_spec_value')->where('id', $sku_value[1])->value('spec_value');
|
|
|
$item['sku_value'] = $key . '_' . $value;
|
|
|
} else {
|
|
|
$key = Db::name('litestore_spec_value')->where('id', $sku_value[0])->value('spec_value');
|
|
|
$item['sku_value'] = $key;
|
|
|
}
|
|
|
} else {
|
|
|
$item['sku_value'] = '';
|
|
|
}
|
|
|
|
|
|
$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']); //内容
|
|
|
}
|
|
|
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('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);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->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]['sku_value']);//商品规格
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $list[$i]['line_price']);//原价
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['discount']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $list[$i]['goods_price']);//标签码
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $list[$i]['goods_sales']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), datetime(time()));//标签码
|
|
|
}
|
|
|
ob_end_clean();
|
|
|
ob_start();
|
|
|
$title = '商品列表';
|
|
|
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();
|
|
|
}
|
|
|
|
|
|
|
|
|
$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;
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|