导入Excel基本思路:
从百度下载一个PHPExcel类:https://github.com/PHPOffice/PHPExcel
用IOFactory.php类
//导入Excel
public function importExcel()
{
require_once ROOT_PATH . 'PHPExcel/Classes/PHPExcel/IOFactory.php';//导入PHPExcel文件中的IOFactory.php类
$file = request()->file('file');//获取文件,file是请求的参数名
$info = $file->move(ROOT_PATH . 'public' . DS . 'excel');//move将文件移动到项目文件的xxx
if($info){
$excel_path = $info->getSaveName(); //获取上传文件名
// $excel_suffix = $info->getExtension(); //获取上传文件后缀
$file_name = ROOT_PATH . 'public' . DS . 'excel' . DS . $excel_path; //上传文件的地址
$obj_PHPExcel = \PHPExcel_IOFactory::load($file_name); //加载文件内容
// echo "";
$excel_array=$obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式
array_shift($excel_array); //删除第一个数组(标题);
$arr = reset($excel_array); //获取字段名
unset($excel_array[0]); //删除字段名,剩下的都是存储到数据库的数据了!!
$data = [];
for($i = 0;$i < count($excel_array);$i++){
foreach ($arr as $key => $value){
$data[$i][$value] = $excel_array[$i+1][$key];//使数组的键值就是数据表的字段名
}
}
$res = GoodsModel::getInstance()->insertAll($data);
var_dump($res);
}
}
导出Excel:
方法一:用PHPExcel.php类
//导出Excel
public function exportExcel2(){
// 引入phpexcel核心类文件
require_once ROOT_PATH . 'PHPExcel/Classes/PHPExcel.php';
//实例化excel类
$objPHPExcel = new \PHPExcel();
// 操作第一个工作表
$objPHPExcel->setActiveSheetIndex(0);
// 设置sheet名
$objPHPExcel->getActiveSheet()->setTitle('xx列表');
// 设置表格宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
// 列名表头文字加粗
$objPHPExcel->getActiveSheet()->getStyle('A1:J1')->getFont()->setBold(true);
// 列表头文字居中
$objPHPExcel->getActiveSheet()->getStyle('A1:J1')->getAlignment()
->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// 列名赋值
$objPHPExcel->getActiveSheet()->setCellValue('A1', '货品名称');
$objPHPExcel->getActiveSheet()->setCellValue('B1', '规格型号');
$objPHPExcel->getActiveSheet()->setCellValue('C1', '包装单位');
$objPHPExcel->getActiveSheet()->setCellValue('D1', '规格单位');
$objPHPExcel->getActiveSheet()->setCellValue('E1', '包装规格');
$objPHPExcel->getActiveSheet()->setCellValue('F1', '标准售价(元)');
$field = ['name', 'spec_model', 'pack_unit', 'spec_unit','pack_spec', 'price'];//第二行列字段内容
$res = GoodsModel::getInstance()
->field($field)
->where(['company_id'=>$this->company_id])
->limit(2)//限制条数
->select();
// 数据起始行
$row_num = 2;
// 向每行单元格插入数据
foreach($res as $value)
{
// 设置所有垂直居中
$objPHPExcel->getActiveSheet()->getStyle('A' . $row_num . ':' . 'J' . $row_num)->getAlignment()
->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
// 设置价格为数字格式
$objPHPExcel->getActiveSheet()->getStyle('F' . $row_num)->getNumberFormat()
->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
// 居中
$objPHPExcel->getActiveSheet()->getStyle('A' . $row_num . ':' . 'F' . $row_num)->getAlignment()
->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// 设置单元格数值
$objPHPExcel->getActiveSheet()->setCellValue('A' . $row_num, $value['name']);
$objPHPExcel->getActiveSheet()->setCellValue('B' . $row_num, $value['spec_model']);
$objPHPExcel->getActiveSheet()->setCellValue('C' . $row_num, $value['pack_unit']);
$objPHPExcel->getActiveSheet()->setCellValue('D' . $row_num, $value['spec_unit']);
$objPHPExcel->getActiveSheet()->setCellValue('E' . $row_num, $value['pack_spec']);
$objPHPExcel->getActiveSheet()->setCellValue('F' . $row_num, $value['price']);
// $objPHPExcel->getActiveSheet()->setCellValue('F' . $row_num, $value['state'] ? '√' : '×');
// $objPHPExcel->getActiveSheet()->setCellValue('G' . $row_num, date('Y-m-d h:i:s',$value['statetime']));
$row_num++;
}
$outputFileName = 'goods_' . time() . '.xls';//导出的文件名
$xlsWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
header("Content-Type: application/force-download");//告诉浏览器强制下载
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="' . $outputFileName . '"');//attachment作为附件下载,inline在线下载,filename设置文件名
header("Content-Transfer-Encoding: binary");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");//设置浏览器响应缓存
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$xlsWriter->save("php://output");
echo file_get_contents($outputFileName);
}
方法二:直接用头部信息输出excel格式文件,内容以表格形式展示 。这种方法可能会导致excel文件格式和扩展名不一致,文件无法用于导入,如果只是导出看看数据可用这种方法。推荐方法一
//导出Excel
public function exportExcel($action,$id = []){
switch ($action){
case 'HP':
$comment = ['货品名称','规格型号','包装单位','规格单位','包装规格','标准售价(元)'];//第一行标题内容
$field = ['name', 'spec_model', 'pack_unit', 'spec_unit','pack_spec', 'price'];//第二行列字段内容
if($id){//查询指定的几条数据
$res = GoodsModel::getInstance()
->field($field)
->where(['company_id'=>$this->company_id,'id'=>['in',$id]])
->select();
}else{//查询所有数据
$res = GoodsModel::getInstance()
->field($field)
->where(['company_id'=>$this->company_id])
// ->limit(2)//限制条数
->select();
}
break;
default:
$this->displayByError('行动有误,撤退!!');
}
$strTable ='';
$strTable .= '';
//第一行的标题
foreach ($comment as $key => $value){
$strTable .= ''.$value.' '."\r\n";
}
$strTable .= ' ';
//第二行的列字段
$strTable .= '';
foreach ($field as $key => $value){
$strTable .= ''.$value.' '."\r\n";
}
$strTable .= ' ';
//第三行开始是导入数据库的数据
$strTable .= '';
for ($i = 0;$i < count($res);$i++){
for ($j = 0; $j < count($field);$j++){
$strTable .= ''.$res[$i][$field[$j]].' '."\r\n";
}
$strTable .= ' ';
}
$strTable .='
';
unset($res);
$this->downloadExcel($strTable,'GoodsExcel');
exit();
}
/**
* 导出excel需要设置的header
* @param $strTable /表格内容
* @param $filename /文件名
*/
function downloadExcel($strTable,$filename)
{
header("Content-type: application/vnd.ms-excel");//将查询结果导出到Excel
header("Content-Type: application/force-download");//告诉浏览器强制下载
header("Content-Disposition: attachment; filename=".$filename."_".date('Y-m-d').".xls");//attachment作为附件下载,inline在线下载,filename设置文件名
header('Expires:0');//浏览器不会响应缓存
header('Pragma:public');//Public指示响应可被任何缓存区缓存。
echo ''.$strTable.'';
}
导出Excel参考文献:https://blog.csdn.net/qq_41211900/article/details/81068901