thinphp3.2 excel 使用phpexcel导入导出
下载压缩包(或者大家也可以到phpexcel下载最新的)解压到 ThinkPHP\Library\Vendor\
然后在项目根目录新建目录uploads
一下代码导入导出 我用到的是mongodb大家可以根据需求修改下! <?php <br />
namespace Home\Controller;<br>
<br>
use Think\Controller;<br>
<br>
class ExcelController extends Controller<br>
{<br>
// 导出数据到excel表<br>
public function index()<br>
{ // excel表头<br>
<br>
$headArr[] = '_id';<br>
$headArr[] = 'id';<br>
$headArr[] = 'title';<br>
$headArr[] = 'content';<br>
$headArr[] = 'time';<br>
// 数据<br>
$data = D('NewsMongo')->limit('100')->select();<br>
$filename = "goods_list";<br>
$this->getExcel($filename, $headArr, $data); // $filename excel名称 $headArr excel表头<br>
}<br>
<br>
public function import()<br>
{<br>
$this->display();<br>
}<br>
<br>
public function upload()<br>
{<br>
header("Content-Type:text/html;charset=utf-8");<br>
$upload = new \Think\Upload(); // 实例化上传类<br>
$upload->maxSize = 3145728; // 设置附件上传大小<br>
$upload->exts = array(<br>
'xls',<br>
'xlsx'<br>
); // 设置附件上传类<br>
$upload->savePath = '/'; // 设置附件上传目录<br>
// 上传文件<br>
$info = $upload->uploadOne($_FILES['excelData']);<br>
$filename = './Uploads' . $info['savepath'] . $info['savename'];<br>
$exts = $info['ext'];<br>
// print_r($info);exit;<br>
if (! $info) { // 上传错误提示错误信息<br>
$this->error($upload->getError());<br>
} else { // 上传成功<br>
$this->goods_import($filename, $exts);<br>
$this->success('导入成功');<br>
}<br>
}<br>
<br>
public function save_import($data)<br>
{<br>
foreach ($data as $key => $val) {<br>
if ($key > 2) {<br>
$datas['id'] = $val['A'];<br>
$datas['tile'] = 'title';<br>
$datas['content'] = $val['D'];<br>
$datas['time'] = date('y-m-d h:i:s', time()); <br>
D('NewsMongo')->add($datas);<br>
}<br>
}<br>
}<br>
<br>
private function getExcel($fileName, $headArr, $data)<br>
{<br>
vendor('PHPExcel');<br>
$date = date("Y_m_d", time());<br>
$fileName .= "_{$date}.xls";<br>
$objPHPExcel = new \PHPExcel();<br>
$objProps = $objPHPExcel->getProperties();<br>
// 设置表头<br>
$key = ord("A");<br>
foreach ($headArr as $v) {<br>
$colum = chr($key);<br>
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);<br>
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);<br>
$key += 1;<br>
}<br>
$column = 2;<br>
$objActSheet = $objPHPExcel->getActiveSheet();<br>
// print_r($data);exit;<br>
foreach ($data as $key => $rows) { // 行写入<br>
$span = ord("A");<br>
foreach ($rows as $keyName => $value) { // 列写入<br>
$j = chr($span);<br>
$objActSheet->setCellValue($j . $column, $value);<br>
$span ++;<br>
}<br>
$column ++;<br>
}<br>
<br>
$fileName = iconv("utf-8", "gb2312", $fileName);<br>
// 重命名表<br>
// 设置活动单指数到第一个表,所以Excel打开这是第一个表<br>
$objPHPExcel->setActiveSheetIndex(0);<br>
header('Content-Type: application/vnd.ms-excel');<br>
header("Content-Disposition: attachment;filename=\"$fileName\"");<br>
header('Cache-Control: max-age=0');<br>
<br>
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');<br>
$objWriter->save('php://output'); // 文件通过浏览器下载<br>
exit();<br>
}<br>
<br>
protected function goods_import($filename, $exts = 'xls')<br>
{<br>
// 导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入<br>
vendor('PHPExcel');<br>
// 创建PHPExcel对象,注意,不能少了\<br>
$PHPExcel = new \PHPExcel();<br>
// 如果excel文件后缀名为.xls,导入这个类<br>
if ($exts == 'xls') {<br>
$PHPReader = new \PHPExcel_Reader_Excel5();<br>
} else <br>
if ($exts == 'xlsx') {<br>
$PHPReader = new \PHPExcel_Reader_Excel2007();<br>
}<br>
<br>
// 载入文件<br>
$PHPExcel = $PHPReader->load($filename);<br>
// 获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推<br>
$currentSheet = $PHPExcel->getSheet(0);<br>
// 获取总列数<br>
$allColumn = $currentSheet->getHighestColumn();<br>
// 获取总行数<br>
$allRow = $currentSheet->getHighestRow();<br>
// 循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始<br>
for ($currentRow = 1; $currentRow
// 从哪列开始,A表示第一列<br>
for ($currentColumn = 'A'; $currentColumn
// 数据坐标<br>
$address = $currentColumn . $currentRow;<br>
// 读取到的数据,保存到数组$arr中<br>
$data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();<br>
}<br>
}<br>
$this->save_import($data);<br>
}<br>
}<br>
?>
以下是导入库的提交页面,大家可以美化下用 nbsp;html><br>
<br>
<br>
<meta>
<br>
<meta>
<br>
<meta>
<br>
<script></script><br>
<title>excel</title>
<br>
<br>
<br>
<form>
<br>
<br>
<div>
<br>
<label>Excel表格:</label><br>
<input><br>
<br>
</div>
<br>
<div>
<br>
<img alt="thinphp3.2 excel 导入导出 " ><br>
<input><br>
</div>
<br>
</form>
<br>
<br>
PHPExcel.zip ( 2.48 MB 下载:152 次 )
AD:真正免费,域名+虚机+企业邮箱=0元