thinkphp5实现导入导出

浏览:3884 发布日期:2018/05/09 分类:用法示例
thinkphp5加phpexcel实现导入导出
<?php
namespace app\home\controller;

use think\Config;
use think\Controller;
use think\Cookie;
use think\Db;
use think\Request;
use think\Session;
class Index extends Controller
{
public function index()
{
return view('index');
}


public function upload(){
// 获取表单上传文件
$file = request()->file('scwj');
// 移动到框架应用根目录/public/uploads/ 目录下
if($file){
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
if($info){
// // 成功上传后 获取上传信息
// // 输出 后缀
// echo $info->getExtension()."<br>";
// // 输出 路径
// echo $info->getSaveName()."<br>";
// // 输出 名字
// echo $info->getFilename()."<br>";

$filename='./Public/uploads/'.$info->getSaveName();
import('ExcelReader',EXTEND_PATH,'.class.php');
$ExcelReader=new \ExcelReader();
$arr=$ExcelReader->reader_excel($filename);
echo "<pre>";
print_r($arr);
echo "</pre>";
}else{
// 上传失败获取错误信息
echo $file->getError();
}
}
}

//导出一
public function export(){
$list=array(array('name' => '你','age' => 25 ),array('name' => '我','age' => 24 ) );

$file_name = date("YmdHis",time()).'数据';
$file_suffix = "xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file_name.$file_suffix");

$this->assign('list',$list);
return view('export');
}

//导出2
public function export2(){
$list=array(array('name' => '你','age' => 25 ),array('name' => '我','age' => 24 ) );

Vendor("PHPExcel.PHPExcel");//引入phpexcel类(注意你自己的路径)
$objPHPExcel = new \PHPExcel();
$title='导出';
$xlstitle = iconv('utf-8', 'gb2312', $title);//文件名称
$fileName = $title.date('_YmdHis');//文件名称

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);//设置列宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);//设置列宽度

//处理表头
$objPHPExcel->getActiveSheet()->setCellValue("A1","姓名")
->setCellValue("B1","年龄");
//处理数据
$j = 2;//行数
foreach($list as $key => $val){
$objPHPExcel->getActiveSheet()->setCellValue("A".$j,$val['name'])
->setCellValue("B".$j,$val['age']);
$j++;
}
//导出execl
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");

}


}







view代码:

index的:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="/home/index/upload" enctype="multipart/form-data" method="post">
<input type="file" name="scwj" /> <br>
<input type="submit" value="上传" />
</form>
</body>
</html>
export的:

<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 11">
</head>

<body>
<table border=1 cellpadding=0 cellspacing=0 >
<tr>
<td style='width:54pt;font-size:14.7px;font-family: "宋体";' align="center">序号</td>
<td style='width:54pt;font-size:14.7px;font-family: "宋体";' align="center">姓名</td>
<td style='width:54pt;font-size:14.7px;font-family: "宋体";' align="center">年龄</td>
</tr>

<volist name="list" id="v" key="k">

</volist>
{volist name="list" id="vo"}
<tr>
<td style='font-size:14.7px;font-family: "宋体";'>{$key}</td>
<td style='font-size:14.7px;font-family: "宋体";'>{$vo['name']}</td>
<td style='font-size:14.7px;font-family: "宋体";'>{$vo['age']}</td>
</tr>
{/volist}


</table>
</body>
</html>

附件 tp5实现excel导入导出.rar ( 878.11 KB 下载:110 次 )

评论( 相关
后面还有条评论,点击查看>>