欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

Thinkphp+phpexcel导出和导入

程序员文章站 2022-04-04 10:41:26
...
在网上找了很多excel导出和导入示例,代码不是很简洁,很多出错,自己在excel方法和控制器改了下。
首先我们看下导出的HTML导出接着我们看下Index/excelExport 导出的PHP代码:$list = M("user")->field("id,username,password")->order("id DESC")->limit(50)->select();
$title = array('ID', '用户名', '密码'); //设置要导出excel的表头
exportExcel($list, '素材火', $title);
exportExcel方法如下:function exportExcel($data, $savefile = null, $title = null, $sheetname = 'sheet1') {
import("Org.Util.PHPExcel");
//若没有指定文件名则为当前时间戳
if (is_null($savefile)) {
$savefile = time();
}
//若指字了excel表头,则把表单追加到正文内容前面去
if (is_array($title)) {
array_unshift($data, $title);
}
$objPHPExcel = new \PHPExcel();
//Excel内容
$head_num = count($data);

foreach ($data as $k => $v) {
$obj = $objPHPExcel->setActiveSheetIndex(0);
$row = $k + 1; //行
$nn = 0;

foreach ($v as $vv) {
$col = chr(65 + $nn); //列
$obj->setCellValue($col . $row, $vv); //列,行,值
$nn++;
}
}
//设置列头标题
for ($i = 0; $i $alpha = chr(65 + $i);
$objPHPExcel->getActiveSheet()->getColumnDimension($alpha)->setAutoSize(true); //单元宽度自适应
$objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->setName("Candara"); //设置字体
$objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->setSize(12); //设置大小
$objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); //设置颜色
$objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //水平居中
$objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中
$objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->setBold(true); //加粗
}

$objPHPExcel->getActiveSheet()->setTitle($sheetname); //题目
$objPHPExcel->setActiveSheetIndex(0); //设置当前的sheet
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $savefile . '.xls"'); //文件名称
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); //Excel5 Excel2007
$objWriter->save('php://output');
}
接着我们看下导入的表单:








thinkphp上传Index/upload:$upload = new \Think\Upload(); // 实例化上传类
$upload->maxSize = 3145728; // 设置附件上传大小
$upload->exts = array('xls', 'xlsx'); // 设置附件上传类
$upload->savePath = '/'; // 设置附件上传目录
// 上传文件
$info = $upload->uploadOne($_FILES['file']);
$filename = 'Uploads' . $info['savepath'] . $info['savename'];
$exts = $info['ext'];
if (!$info) {// 上传错误提示错误信息
$this->error($upload->getError());
} else {// 上传成功
$this->goods_import($filename, $exts);
}
goods_import导入方法:function goods_import($filename, $exts = 'xls') {
//导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
import("Org.Util.PHPExcel");
//创建PHPExcel对象,注意,不能少了\
$PHPExcel = new \PHPExcel();
//如果excel文件后缀名为.xls,导入这个类

if ($exts == 'xls') {
import("Org.Util.PHPExcel.Reader.Excel5");
$PHPReader = new \PHPExcel_Reader_Excel5();
} else if ($exts == 'xlsx') {
import("Org.Util.PHPExcel.Reader.Excel2007");
$PHPReader = new \PHPExcel_Reader_Excel2007();
}
//载入文件
$PHPExcel = $PHPReader->load($filename);
//获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
$currentSheet = $PHPExcel->getSheet(0);
//获取总列数
$allColumn = $currentSheet->getHighestColumn();
//获取总行数
$allRow = $currentSheet->getHighestRow();
//循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
for ($currentRow = 1; $currentRow //从哪列开始,A表示第一列
for ($currentColumn = 'A'; $currentColumn //数据坐标
$address = $currentColumn . $currentRow;
//读取到的数据,保存到数组$arr中
$data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
}
}
$this->save_import($data);
}
phpexcel导入和导出演示下载:http://www.sucaihuo.com/php/140.html

AD:真正免费,域名+虚机+企业邮箱=0元