PHPExcel下载地址:PHPExcel下载地址
thinkphp 5.1版本取消`Loader :: import`方法以及`import`和`vendor`助手函数,推荐全部使用命名空间方式的类以及自动加载机制,如果必须使用请直接改为php内置的`include`或者` require`语法。
导入:
<?php
//导入
public function upload()
{
//获取表单上传文件
$file = request()->file('excel');
if($file){
$file_types = explode(".", $_FILES ['myfile'] ['name']); // ["name"] => string(25) "excel文件名.xls"
$file_type = $file_types [count($file_types) - 1];//xls后缀
/*判别是不是.xls文件,判别是不是excel文件*/
if (strtolower($file_type) != "xls" && strtolower($file_type) != "xlsx") {
$this->error('不是Excel文件,重新上传');
die;
}
$a = dirname($_SERVER['SCRIPT_FILENAME'])."/";
$b = dirname(realpath($ a))."/";
$path = $b.'public/upload/excel';
$info = $file->move($path);
$file_path = $path.'/'.$info->getSaveName(); //上传后的EXCEL路径
switch ($extension) {
case 'xls':
$type = 'Excel5';
break;
case 'xlsx':
$type = 'Excel2007';
break;
}
// 引入核心文件
require'../extend/PHPExcel/PHPExcel.php';
$objReader = \PHPExcel_IOFactory::createReader($type);
$obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8'); //加载文件内容,编码utf-8
/*$sheet = $obj_PHPExcel->getSheet(0); //激活当前的表
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数*/
$excel_array=$obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式
array_shift($excel_array); //删除第一个数组(标题);
}
}
导出:
//导出
public function daochu(){
$xlsName = "Production";
$xlsCell = array(
array('id','商品id'),
array('is_sale','上下架'),
array('name','商品名称'),
array('brand_id','品牌名称'),
array('catalog_id','分类名称'),
array('supplier_id','供应商'),
array('original_price','市场价'),
array('cost_price','成本价'),
array('directsaleprice','售价')
);
$xlsData = Db::name('production')
->where('is_delete',0)
->field('id,is_sale,name,brand_id,catalog_id,supplier_id,original_price,cost_price,directsaleprice')
->select();
foreach ($xlsData as $k => $v)
{
$res = Db::name('production')->where('id',$v['id'])->find();
if($res['is_sale'] == 1)
{
$xlsData[$k]['is_sale'] = '下架';
}
else
{
$xlsData[$k]['is_sale'] = '上架';
}
if(intval($res['brand_id']))
{
$brandinfo = Db::name('brand')->where('id',$res['brand_id'])->find();
$xlsData[$k]['brand_id'] = $brandinfo['name'];
}
if(intval($res['catalog_id']))
{
$cataloginfo = Db::name('catalog')->where('id',$res['catalog_id'])->find();
$xlsData[$k]['catalog_id'] = $cataloginfo['name'];
}
if(intval($res['supplier_id']))
{
$supplierinfo = Db::name('supplier')->where('id',$res['supplier_id'])->find();
$xlsData[$k]['supplier_id'] = $supplierinfo['name'];
}
}
$this->exportExcel($xlsName,$xlsCell,$xlsData);;
}
public function exportExcel($expTitle,$expCellName,$expTableData){
$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
$fileName =$xlsTitle . date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
$cellNum = count($expCellName);
$dataNum = count($expTableData);
// 引入核心文件
require'../extend/PHPExcel/PHPExcel.php';
$objPHPExcel = new \PHPExcel();
$cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
$objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
// $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' Export time:'.date('Y-m-d H:i:s'));
for($i=0;$i<$cellNum;$i++){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
}
// Miscellaneous glyphs, UTF-8
for($i=0;$i<$dataNum;$i++){
for($j=0;$j<$cellNum;$j++){
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
}
}
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');
exit;
}