作者:走丢的鞋带2702934823 | 来源:互联网 | 2013-08-06 10:28
作为一个php爱好者来说,我们需要经常性的了解一些开源的项目,这对于我们来说未尝不是很好的提升,为了我们能够轻松编程
thinkphp导入导出
(一)导入Excel
第一,在前台html页面进行上传文件:如:
导入Excel表:
第二,在对应的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];
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 ( 'die' );
}
$res = Service ( 'ExcelToArray' )->read ( $savePath . $file_name );
//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];
$data ['uname'] = $v [3];
$data ['institute'] = $v [4];
$result = M ( 'user' )->add ( $data );
if (! $result)
{
$this->error ( '导入数据库失败' );
}
}
}
}
第三:ExcelToArrary类,用来引用phpExcel并处理Excel数据的
备注:ExcelToArrary类建在根目录下的 addons /services/ExcelToArrary.class.php中
class ExcelToArrary extends Service{
public function __construct() {
include_once('./Excel/PHPExcel.php');
}
public function read($filename,$encode='utf-8'){
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$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包附在最后。
(二)Excel的导出(相对于导入简单多了)
第一,先查出数据库里面要生成Excel的数据,如:
$data= M('User')->findAll(); //查出数据
$name='Excelfile'; //生成的Excel文件文件名
$res=service('ExcelToArrary')->push($data,$name);
第二,ExcelToArrary类,用来引用phpExcel并处理数据的
class ExcelToArrary extends Service{
public function __construct() {
/*导入phpExcel核心类 注意 :你的路径跟我不一样就不能直接复制*/
include_once('./Excel/PHPExcel.php');
}
/* 导出excel函数*/
public function push($data,$name='Excel'){
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("php爱好者")
->setLastModifiedBy("php爱好者")
->setTitle("数据EXCEL导出")
->setSubject("数据EXCEL导出")
->setDescription("备份数据")
->setKeywords("excel")
->setCategory("result file");
foreach($data as $k => $v){
$num=$k+1;
$objPHPExcel->setActiveSheetIndex(0)
//Excel的第A列,uid是你查出数组的键值,下面以此类推
->setCellValue('A'.$num, $v['uid'])
->setCellValue('B'.$num, $v['email'])
->setCellValue('C'.$num, $v['password'])
}
$objPHPExcel->getActiveSheet()->setTitle('User');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$name.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}