Login
网站首页 > 文章中心 > 其它

phpexcel导入导出

作者:小编 更新时间:2023-09-18 09:41:50 浏览量:199人看过

thinkphp⑤0 如何导入phpexcel插件,能详细些点吗

在控制器中的使用方法

vendor("PHPExcel.PHPExcel");

$objPHPExcel?=?new?\PHPExcel();

$file_name?=?date('YmdHis').'.xls';

header("Content-Disposition:attachment;?filename=".$file_name);

$objActSheet?=?$objPHPExcel-getActiveSheet()-setTitle('成绩单');

$objActSheet-SetCellValue('A1',?'ID');

$objActSheet-SetCellValue('B1',?'名称');

$objActSheet-SetCellValue('C1',?'分数');

$list?=?"";//数据列表

if($list){

foreach?($list?as?$k?=?$v){

$objActSheet-SetCellValue('A'.$i,''.?$v['id']);

$objActSheet-SetCellValue('B'.$i,''.?$v['name']);

$objActSheet-SetCellValue('C'.$i,''.?$v['score']);

}

$objWriter-save('php://output');//输出到浏览器

exit;

PHP如何导入导出Excel

抄段例子出来:

PHP代码

php

/**

* PHPExcel

*

* This library is free software; you can redistribute it and/or

* modify it under the terms of the GNU Lesser General Public

* License as published by the Free Software Foundation; either

* This library is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

* Lesser General Public License for more details.

* You should have received a copy of the GNU Lesser General Public

* License along with this library; if not, write to the Free Software

* @category PHPExcel

* @package PHPExcel

* @license

*/

/** Error reporting */

error_reporting(E_ALL);

/** Include path **/

set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/');

/** PHPExcel */

include 'PHPExcel.php';

// Create new PHPExcel object

echo date('H:i:s') . " Create new PHPExcel object\n";

$objPHPExcel = new PHPExcel();

// Set properties

echo date('H:i:s') . " Set properties\n";

$objPHPExcel-getProperties()-setCreator("Maarten Balliauw");

$objPHPExcel-getProperties()-setLastModifiedBy("Maarten Balliauw");

$objPHPExcel-getProperties()-setCategory("Test result file");

// Add some data

echo date('H:i:s') . " Add some data\n";

$objPHPExcel-setActiveSheetIndex(0);

$objPHPExcel-getActiveSheet()-setCellValue('A1◆, 'Hello');

$objPHPExcel-getActiveSheet()-setCellValue('C1◆, 'Hello');

// Rename sheet

echo date('H:i:s') . " Rename sheet\n";

$objPHPExcel-getActiveSheet()-setTitle('Simple');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet

$objWriter-save(str_replace('.php', '.xlsx', __FILE__));

// Echo done

echo date('H:i:s') . " Done writing file.\r\n";

thinkphp怎么用phpexcel导出数据到excel

(一)导入Excel

第一,在前台html页面进行上传文件:如:

复制代码 代码如下:

form method="post" action="php文件" enctype="multipart/form-data"

input type="submit" value="导入" /

/form

第二,在对应的php文件进行文件的处理

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")

$this-error ( '不是Excel文件,重新上传' );

/*设置上传路径*/

$savePath = SITE_PATH . '/public/upfile/Excel/';

/*以时间来命名上传的文件*/

$str = date ( 'Ymdhis' );

$file_name = $str . "." . $file_type;

/*是否上传成功*/

if (! copy ( $tmp_file, $savePath . $file_name ))

$this-error ( '上传失败' );

/*

*对上传的Excel数据进行处理生成编程数据,这个函数会在下面第三步的ExcelToArray类中

注意:这里调用执行了第三步类里面的read函数,把Excel转化为数组并返回给$res,再进行数据库写入

$res = Service ( 'ExcelToArray' )-read ( $savePath . $file_name );

重要代码 解决Thinkphp M、D方法不能调用的问题

如果在thinkphp中遇到M 、D方法失效时就加入下面一句代码

//spl_autoload_register ( array ('Think', 'autoload' ) );

/*对生成的数组进行数据库的写入*/

foreach ( $res as $k = $v )

if ($k != 0)

$data ['uid'] = $v [0];

$data ['password'] = sha1 ( '111111' );

$data ['email'] = $v [1];

$result = M ( 'user' )-add ( $data );

if (! $result)

$this-error ( '导入数据库失败' );

第三:ExcelToArrary类,用来引用phpExcel并处理Excel数据的

class ExcelToArrary extends Service{

public function __construct() {

/*导入phpExcel核心类 注意 :你的路径跟我不一样就不能直接复制*/

include_once('./Excel/PHPExcel.php');

*以下基本都不要修改

$objReader-setReadDataOnly(true);

$objPHPExcel = $objReader-load($filename);

$objWorksheet = $objPHPExcel-getActiveSheet();

$highestRow = $objWorksheet-getHighestRow();

$highestColumn = $objWorksheet-getHighestColumn();

$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

$excelData = array();

for ($row = 1; $row = $highestRow; $row◆◆) {

for ($col = 0; $col $highestColumnIndex; $col◆◆) {

$excelData[$row][] =(string)$objWorksheet-getCellByColumnAndRow($col, $row)-getValue();

return $excelData;

以上就是土嘎嘎小编为大家整理的phpexcel导入导出相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章