//加载PHPExcel的类
$this->load->library('PHPExcel/PHPExcel');
$this->load->library('PHPExcel/PHPExcel/IOFactory.php');
//创建PHPExcel实例
$excel = new PHPExcel();
$IOFactory = new IOFactory();
$objPHPExcel = new PHPExcel(); //实例化一个PHPExcel()对象
$objSheet = $objPHPExcel->getActiveSheet(); //选取当前的sheet对象
$objSheet->setTitle('helen'); //对当前sheet对象命名
//常规方式:利用setCellValue()填充数据
$objSheet->setCellValue('A1', '用户id')
->setCellValue('B1', '订单id')
->setCellValue('C1', '订单号')
->setCellValue('D1', '娃娃')
->setCellValue('E1', '收货人')
->setCellValue('F1', '手机')
->setCellValue('G1', '省')
->setCellValue('H1', '市')
->setCellValue('I1', '县')
->setCellValue('J1', '详细')
->setCellValue('K1', '备注');; //利用setCellValues()填充数据
//取巧模式:利用fromArray()填充数据
$info[] = ['用户ID','订单ID','订单号','娃娃','收货人','电话','省','市','县','详细','备注'];
foreach($list as $key=>$val)
{
$info[] = [
$val['user_id'],$val['ex_id'],$val['order_num'],$val['name'],$val['phone'], $val['province'],$val['city'],
$val['county'],$val['detailed'],$val['remark']
];
}
$objSheet->fromArray($info); //利用fromArray()直接一次性填充数据
$objWriter = $IOFactory::createWriter($objPHPExcel,'Excel2007'); //设定写入excel的类型
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="11.xls"');
header("Content-Transfer-Encoding:binary");
$objWriter->save('php://output');
导入
if (!empty ($_FILES ['file_stu'] ['name'])) {
$tmp_file = $_FILES ['file_stu'] ['tmp_name'];
$file_types = explode(".", $_FILES ['file_stu'] ['name']);
$file_type = $file_types [count($file_types) - 1];
/*判别是不是.xls文件,判别是不是excel文件*/
if (strtolower($file_type) != "xls") {
echo ('不是Excel文件,重新上传');
}
// 上传文件到服务器目录
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/upload/order_info/';
// 允许上传哪些类型
$config['allowed_types'] = 'xls';
// 上传后的文件名,用uniqid()保证文件名唯一
$config['file_name'] = $name=date('Ymdhis').uniqid();
// 加载上传库
$this->load->library('upload', $config);
// 上传文件,这里的pic是视图中file控件的name属性
$result = $this->upload->do_upload('file_stu');
// 如果上传成功,获取上传文件的信息
if (!$result)
{
jsRedirect("cuowu !", 'admin/order_form/lst');
}
$file = $this->upload->data();
$file = $file['full_path'];
$this->load->library('PHPExcel/PHPExcel');
$PHPExcel = new PHPExcel();
$this->load->library('PHPExcel/PHPExcel/IOFactory.php');
$IOFactory = new IOFactory();
$IOFactory = $IOFactory::load($file);
$reader = $IOFactory->getWorksheetIterator();
//循环读取sheet
foreach($reader as $sheet)
{
//读取表内容
$content = $sheet->getRowIterator();
//逐行处理
$res_arr = array();
foreach($content as $key => $items)
{
$rows = $items->getRowIndex(); //行
$columns = $items->getCellIterator(); //列
$row_arr = array();
//确定从哪一行开始读取
if($rows < 1){
continue;
}
//逐列读取
foreach($columns as $head => $cell)
{
//获取cell中数据
$data = $cell->getValue();
$row_arr[] = $data;
}
$res_arr[] = $row_arr;
}
}