概述
第一步:安装,使用composer安装扩展。composer require phpoffice/phpexcel
第二步:封装<?php
namespace ExcelService;
use thinkDb;
use thinkException;
use thinkLoader;
use thinkLog;
use PHPExcel_IOFactory;
use PHPExcel;
use PHPExcel_Worksheet_Drawing;
class ExcelService
{
/**
* TP5 Excel专用类库
* $excel=new Excel();
* $table_name="member"; // 数组则视为结果 字符串则视为表名
* $field=["id"=>"序号","guid"=>"项目代码","name"=>"项目名称"];
* $map=["status"=>1]; // 表名时 条件
* $map2=["status"=>-1]; // 表名时 条件
* $excel->setExcelName("下载装修项目")
* ->createSheet($table_name,$field,$map)
* ->createSheet($table_name,$field,$map2)
* ->downloadExcel();
*
*/
protected $objPHPExcel;
public $xlsReader;
public static $instance;
protected $sheetNum=0;
protected $error;
protected $columnWidth;
protected $rowHeight=20;
protected $excelName;
protected $sheetTitle;
protected $isLoad=false;
//如果你的字段列数超过26字母 会报错
protected $letterArray=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
/**
* 项目初始化
* Excel constructor.
*/
public function __construct()
{
$this->objPHPExcel= new PHPExcel();
if(!$this->isLoad){
//新建时删除默认页面
$this->objPHPExcel->disconnectWorksheets();
}
}
/**
* 静态初始化方法
* Power: Mikkle
* Email:776329498@qq.com
* @return static
*/
public static function instance()
{
if (is_null(self::$instance)) {
self::$instance = new static();
}
return self::$instance;
}
/**
* Power: Mikkle
* Email:776329498@qq.com
* @param string $path
* @return static
* @throws Exception
* @throws PHPExcel_Reader_Exception
*/
static public function loadExcel($path="/test.xls",$ext =''){
if (is_null(self::$instance)) {
self::$instance = new static();
}
$excel = self::$instance;
try {
try {
if ($ext == "csv") {
$xlsReader = PHPExcel_IOFactory::createReader('CSV')->setDelimiter(',')->setEnclosure('"')->setSheetIndex(0);
$xlsReader->setReadDataOnly(true); //
$xlsReader->setLoadSheetsOnly(true);
$xlsReader->setInputEncoding('GBK');
$excel->xlsReader=$xlsReader->load($path);
}else{
$xlsReader = PHPExcel_IOFactory::createReader("Excel2007");
$xlsReader->setReadDataOnly(true); //
$xlsReader->setLoadSheetsOnly(true);
$excel->xlsReader=$xlsReader->load($path);
}
} catch (Exception $e) {
$xlsReader = PHPExcel_IOFactory::createReader("Excel5");
$xlsReader->setReadDataOnly(true); //
$xlsReader->setLoadSheetsOnly(true);
$excel->xlsReader=$xlsReader->load($path);
}
} catch (Exception $e) {
throw new Exception("读取EXCEL失败");
}
return $excel;
}
public function getSheetByName($name){
if (isset($this->xlsReader)){
return $this->xlsReader->getSheetByName($name);
}else{
return false;
}
}
public function getSheetNames(){
if (isset($this->xlsReader)){
return $this->xlsReader->getSheetNames();
}else{
return false;
}
}
/**
* Power: Mikkle
* Email:776329498@qq.com
* @return mixed
*/
public function getExcelObject(){
return $this->xlsReader;
}
public function getAllSheets(){
if (isset($this->xlsReader)){
return $this->xlsReader->getAllSheets();
}else{
return false;
}
}
public function getSheetCount(){
if (isset($this->xlsReader)){
return $this->xlsReader->getSheetCount();
}else{
return false;
}
}
public function getSheetArrayByIndex($index=0){
if (isset($this->xlsReader)){
return $this->xlsReader->getSheet($index)->toArray();
}else{
return false;
}
}
/**
* 设置下载的Excel名称
* Power: Mikkle
* Email:776329498@qq.com
* @param $name
* @return $this
*/
public function setExcelName($name){
$this->excelName=$name;
return $this;
}
public function setSheetTitle($name)
{
$this->sheetTitle=$name;
return $this;
}
public function getSheetTitle()
{
return $this->sheetTitle ? $this->sheetTitle : "";
}
/**
* 返回EXCEL名称
* Power: Mikkle
* Email:776329498@qq.com
* @return string
*/
public function getExcelName()
{
return $this->excelName ? $this->excelName : "新建的数据表格";
}
/**
* 创建
* @param string|array $table = string 的时候则为表名 = array 的时候 则为结果集
* @param array $field 要导出的字段
* @param array $map 查询参数
* @return this
*/
public function createSheet($table,$field=[],$map=[]){
if (empty($table) ||empty($field)|| !is_array($field)){
$this->error="生成Excel的[table]或[field]参数不正确";
throw new Exception("生成Excel的[table]或[field]参数不正确");
return $this;
}
$sheet_num = $this->getNewSheetNum();
$objPHPExcel=$this->objPHPExcel;
$objPHPExcel->createSheet($sheet_num);
$objPHPExcel->setActiveSheetIndex($sheet_num);
$sheet=$objPHPExcel->getActiveSheet();
if ($this->getSheetTitle()) {
$objPHPExcel->getActiveSheet()->setTitle($this->getSheetTitle());
}
//设置默认行高
$sheet->getDefaultRowDimension()->setRowHeight($this->rowHeight);
$titleStyleArray = [
'font' => [
'bold' => true
],
'alignment' => [
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
]
];
$field_title=array_values($field);
$letter_array = $this->letterArray;
foreach($field_title as $item=>$value){
if(isset($this->columnWidth)){
if(is_array($this->columnWidth) && count($field)==count($this->columnWidth)){
$sheet->getColumnDimension($letter_array[$item])->setWidth($this->columnWidth[$item]);
}elseif(is_integer($this->columnWidth)){
$sheet->getColumnDimension($letter_array[$item])->setWidth($this->columnWidth);
}else{
$sheet->getColumnDimension($letter_array[$item])->setAutoSize(true);
}
}else{
$sheet->getColumnDimension($letter_array[$item])->setAutoSize(true);
}
//标题加粗
$sheet->getStyle($letter_array[$item]."1")->getFont()->setBold(true);
$sheet->setCellValue($letter_array[$item]."1",$value);
}
// p($table);
if (is_string($table)) {
$list = Db::table($table)->field($field)->where($map)->select();
}else if(is_array($table)){
$list = $table;
}else{
throw new Exception("Table错误:支持数组或数据表名称");
return $this;
}
if ($list){
foreach($list as $item=>$value ){
$value=array_values($value);
foreach($value as $i=>$v){
$sheet->setCellValue($letter_array[$i].($item+2),$value[$i]);
}
}
}
$color='FFFF0000';
$width = count($field_title)+1;
$rows = count($list)+1;
//边框样式
$styleArray = [
'borders' => [
'allborders' => [
// 'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的
'style' => PHPExcel_Style_Border::BORDER_THIN,//细边框
// 'color' => array('argb' => $color),
],
],
];
$objPHPExcel->getActiveSheet()->getStyle("A1:{$this->letterArray[ $width ]}{$rows}")->applyFromArray($styleArray);
return $this;
}
/**
* 下载当前的EXCEL
* Power: Mikkle
* Email:776329498@qq.com
* @param string $save_name
* @throws PHPExcel_Reader_Exception
*/
public function downloadExcel($save_name=""){
ob_start();
//最后通过浏览器输出
$save_name=$this->getExcelName();
$save_name = $save_name ? "$save_name.xls" : "导出信息.xls";
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header("Content-Disposition: attachment;filename=$save_name");
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->objPHPExcel, 'Excel5');
$objWriter->save('php://output');
ob_end_flush();//输出全部内容到浏览器
die();
}
/**
* 保存当前的EXCEL
* Power: Mikkle
* Email:776329498@qq.com
* @param string $save_path
* @throws PHPExcel_Reader_Exception
*/
public function saveExcel($save_path=""){
$objWriter = PHPExcel_IOFactory::createWriter($this->objPHPExcel, 'Excel5');
$save_name=$this->getExcelName();
$save_name = $save_name ? "$save_name.xls" : "demo.xls";
$save_path=$save_path?$save_path:ROOT_PATH.'runtime/excel/'.$save_name;
if(!is_dir(dirname ($save_path))){
mkdir(dirname ($save_path),0755,true);
}
$objWriter->save($save_path);
die();
}
/**
* 获取新的Sheet编号
* Power: Mikkle
* Email:776329498@qq.com
* @return int
*/
protected function getNewSheetNum(){
$sheet_num=$this->sheetNum;
$this->sheetNum=$sheet_num+1;
return $sheet_num;
}
/**
* 设置行宽 未设置时候默认为自动
* Power: Mikkle
* Email:776329498@qq.com
* @param $width
* @return $this
*/
public function setColumnWidth($width){
if(is_integer($width)||is_array($width)){
$this->columnWidth=$width;
}
return $this;
}
/**
* 设置默认行高
* Power: Mikkle
* Email:776329498@qq.com
* @param $height
* @return $this
*/
public function setRowHeight($height){
if(is_numeric($height)){
$this->rowHeight=$height;
}
return $this;
}
/**
* 魔术方法 有不存在的操作的时候执行
* @access public
* @param string $method 方法名
* @param array $args 参数
* @return mixed
*/
public function __call($method, $args)
{
call_user_func_array([$this->objPHPExcel, $method], $args);
}
}
第三步:使用
1.导出数据到表格$excel=new ExcelService();
$table_name="mk_material_list_edit";
$field=["id"=>"序号","guid"=>"项目代码","name"=>"项目名称"];
$map=["status"=>1];
$map2=["status"=>-1];
$excel->setExcelName("下载装修项目")
->createSheet("装修项目",$table_name,$field,$map)
->createSheet("已删除装修项目",$table_name,$field,$map2)
->downloadExcel();
2.上传文件后导入数据,并解析数据结果$getExcelObject=Excel::loadExcel("test.xls");
$sheetName=$getExcelObject->getSheetNames();
dump($sheetName);
$sheet = $getExcelObject->getSheetByName($sheetName[0])->toArray();
dump($sheet);
最后
以上就是包容铃铛为你收集整理的think php excel,在thinkphp6中使用Excel的全部内容,希望文章能够帮你解决think php excel,在thinkphp6中使用Excel所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复