phpexcel

phpexcel

Class 'PHPExcel_Writer_Excel2013' not found

博客taotao 发表了文章 • 0 个评论 • 31 次浏览 • 2019-08-13 10:16 • 来自相关话题

phpExcel导入不同excel版本遇到的问题,报Class 'PHPExcel_Writer_Excel2013' not found怎么解决?   php excel在不同的版本,实例化类是不一样的。2003以前的excel读取实 ...查看全部
phpExcel导入不同excel版本遇到的问题,报Class 'PHPExcel_Writer_Excel2013' not found怎么解决?
 
php excel在不同的版本,实例化类是不一样的。2003以前的excel读取实例化excel5类,2003以后需要实例化excel2007类
 
 $objWriter=\PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel2007");//按照指定格式生成excel文件
 
 
 

thinkphp5.0 phpexcel导入导出excel文件

博客taotao 发表了文章 • 0 个评论 • 2257 次浏览 • 2016-08-22 15:15 • 来自相关话题

thinkphp5.0 phpexcel导入导出excel文件 一:下载phpexcel类库,并导入thinkphp5.0项目中 Vendor/PHPExcel 二:在common.php文件中加入公共的导入,导出方法 ...查看全部
thinkphp5.0 phpexcel导入导出excel文件
一:下载phpexcel类库,并导入thinkphp5.0项目中
Vendor/PHPExcel
二:在common.php文件中加入公共的导入,导出方法
1.导出excel方法
/**
+----------------------------------------------------------
* Import Excel | 2016.08.22
* Author:gallon <114746090@qq.com>
+----------------------------------------------------------
* @param $file upload file $_FILES
+----------------------------------------------------------
* @return array array("error","message")
+----------------------------------------------------------
*/
function exportExcel($expTitle,$expCellName,$expTableData){
$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
$fileName = $expTitle;//or $xlsTitle 文件名称可根据自己情况设定
$cellNum = count($expCellName);
$dataNum = count($expTableData);
vendor("PHPExcel.PHPExcel");
$objPHPExcel = new PHPExcel();
$cellName = array('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','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');

$objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' Export time:'.date('Y-m-d H:i:s'));
for($i=0;$i<$cellNum;$i++){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
}
// Miscellaneous glyphs, UTF-8
for($i=0;$i<$dataNum;$i++){
for($j=0;$j<$cellNum;$j++){
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
}
}

header('pragma:public');
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');
exit;
}

2.导入excel方法

/**
+----------------------------------------------------------
* Import Excel | 2016.08.22
* Author:gallon <114746090@qq.com>
+----------------------------------------------------------
* @param $file upload file $_FILES
+----------------------------------------------------------
* @return array array("error","message")
+----------------------------------------------------------
*/
function importExecl($file){
if(!file_exists($file)){
return array("error"=>0,'message'=>'file not found!');
}
Vendor("PHPExcel.PHPExcel.IOFactory");
$objReader = PHPExcel_IOFactory::createReader('Excel5');
try{
$PHPReader = $objReader->load($file);
}catch(Exception $e){}
if(!isset($PHPReader)) return array("error"=>0,'message'=>'read error!');
$allWorksheets = $PHPReader->getAllSheets();
$i = 0;
foreach($allWorksheets as $objWorksheet){
$sheetname=$objWorksheet->getTitle();
$allRow = $objWorksheet->getHighestRow();//how many rows
$highestColumn = $objWorksheet->getHighestColumn();//how many columns
$allColumn = PHPExcel_Cell::columnIndexFromString($highestColumn);
$array[$i]["Title"] = $sheetname;
$array[$i]["Cols"] = $allColumn;
$array[$i]["Rows"] = $allRow;
$arr = array();
$isMergeCell = array();
foreach ($objWorksheet->getMergeCells() as $cells) {//merge cells
foreach (PHPExcel_Cell::extractAllCellReferencesInRange($cells) as $cellReference) {
$isMergeCell[$cellReference] = true;
}
}
for($currentRow = 1 ;$currentRow<=$allRow;$currentRow++){
$row = array();
for($currentColumn=0;$currentColumn<$allColumn;$currentColumn++){;
$cell =$objWorksheet->getCellByColumnAndRow($currentColumn, $currentRow);
$afCol = PHPExcel_Cell::stringFromColumnIndex($currentColumn+1);
$bfCol = PHPExcel_Cell::stringFromColumnIndex($currentColumn-1);
$col = PHPExcel_Cell::stringFromColumnIndex($currentColumn);
$address = $col.$currentRow;
$value = $objWorksheet->getCell($address)->getValue();
if(substr($value,0,1)=='='){
return array("error"=>0,'message'=>'can not use the formula!');
exit;
}
if($cell->getDataType()==PHPExcel_Cell_DataType::TYPE_NUMERIC){
$cellstyleformat=$cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat();
$formatcode=$cellstyleformat->getFormatCode();
if (preg_match('/^([$[A-Z]*-[0-9A-F]*])*[hmsdy]/i', $formatcode)) {
$value=gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value));
}else{
$value=PHPExcel_Style_NumberFormat::toFormattedString($value,$formatcode);
}
}
if($isMergeCell[$col.$currentRow]&&$isMergeCell[$afCol.$currentRow]&&!empty($value)){
$temp = $value;
}elseif($isMergeCell[$col.$currentRow]&&$isMergeCell[$col.($currentRow-1)]&&empty($value)){
$value=$arr[$currentRow-1][$currentColumn];
}elseif($isMergeCell[$col.$currentRow]&&$isMergeCell[$bfCol.$currentRow]&&empty($value)){
$value=$temp;
}
$row[$currentColumn] = $value;
}
$arr[$currentRow] = $row;
}
$array[$i]["Content"] = $arr;
$i++;
}
spl_autoload_register(array('Think','autoload'));//must, resolve ThinkPHP and PHPExcel conflicts
unset($objWorksheet);
unset($PHPReader);
unset($PHPExcel);
unlink($file);
return array("error"=>1,"data"=>$array);
}
三:在应用层加入触发代码
    //导出用户
public function excel_action()
{
$xlsName = "User";
$xlsCell = array(
array('id','账号序列'),
array('user_login','登录账户'),
array('create_time','注册时间')
);
$xlsData=userModel::all();
$xlsData =db("users")->where("user_status",1)->select();
exportExcel($xlsName,$xlsCell,$xlsData);
}

Class 'PHPExcel_Writer_Excel2013' not found

博客taotao 发表了文章 • 0 个评论 • 31 次浏览 • 2019-08-13 10:16 • 来自相关话题

phpExcel导入不同excel版本遇到的问题,报Class 'PHPExcel_Writer_Excel2013' not found怎么解决?   php excel在不同的版本,实例化类是不一样的。2003以前的excel读取实 ...查看全部
phpExcel导入不同excel版本遇到的问题,报Class 'PHPExcel_Writer_Excel2013' not found怎么解决?
 
php excel在不同的版本,实例化类是不一样的。2003以前的excel读取实例化excel5类,2003以后需要实例化excel2007类
 
 $objWriter=\PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel2007");//按照指定格式生成excel文件
 
 
 

thinkphp5.0 phpexcel导入导出excel文件

博客taotao 发表了文章 • 0 个评论 • 2257 次浏览 • 2016-08-22 15:15 • 来自相关话题

thinkphp5.0 phpexcel导入导出excel文件 一:下载phpexcel类库,并导入thinkphp5.0项目中 Vendor/PHPExcel 二:在common.php文件中加入公共的导入,导出方法 ...查看全部
thinkphp5.0 phpexcel导入导出excel文件
一:下载phpexcel类库,并导入thinkphp5.0项目中
Vendor/PHPExcel
二:在common.php文件中加入公共的导入,导出方法
1.导出excel方法
/**
+----------------------------------------------------------
* Import Excel | 2016.08.22
* Author:gallon <114746090@qq.com>
+----------------------------------------------------------
* @param $file upload file $_FILES
+----------------------------------------------------------
* @return array array("error","message")
+----------------------------------------------------------
*/
function exportExcel($expTitle,$expCellName,$expTableData){
$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
$fileName = $expTitle;//or $xlsTitle 文件名称可根据自己情况设定
$cellNum = count($expCellName);
$dataNum = count($expTableData);
vendor("PHPExcel.PHPExcel");
$objPHPExcel = new PHPExcel();
$cellName = array('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','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');

$objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' Export time:'.date('Y-m-d H:i:s'));
for($i=0;$i<$cellNum;$i++){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
}
// Miscellaneous glyphs, UTF-8
for($i=0;$i<$dataNum;$i++){
for($j=0;$j<$cellNum;$j++){
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
}
}

header('pragma:public');
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');
exit;
}

2.导入excel方法

/**
+----------------------------------------------------------
* Import Excel | 2016.08.22
* Author:gallon <114746090@qq.com>
+----------------------------------------------------------
* @param $file upload file $_FILES
+----------------------------------------------------------
* @return array array("error","message")
+----------------------------------------------------------
*/
function importExecl($file){
if(!file_exists($file)){
return array("error"=>0,'message'=>'file not found!');
}
Vendor("PHPExcel.PHPExcel.IOFactory");
$objReader = PHPExcel_IOFactory::createReader('Excel5');
try{
$PHPReader = $objReader->load($file);
}catch(Exception $e){}
if(!isset($PHPReader)) return array("error"=>0,'message'=>'read error!');
$allWorksheets = $PHPReader->getAllSheets();
$i = 0;
foreach($allWorksheets as $objWorksheet){
$sheetname=$objWorksheet->getTitle();
$allRow = $objWorksheet->getHighestRow();//how many rows
$highestColumn = $objWorksheet->getHighestColumn();//how many columns
$allColumn = PHPExcel_Cell::columnIndexFromString($highestColumn);
$array[$i]["Title"] = $sheetname;
$array[$i]["Cols"] = $allColumn;
$array[$i]["Rows"] = $allRow;
$arr = array();
$isMergeCell = array();
foreach ($objWorksheet->getMergeCells() as $cells) {//merge cells
foreach (PHPExcel_Cell::extractAllCellReferencesInRange($cells) as $cellReference) {
$isMergeCell[$cellReference] = true;
}
}
for($currentRow = 1 ;$currentRow<=$allRow;$currentRow++){
$row = array();
for($currentColumn=0;$currentColumn<$allColumn;$currentColumn++){;
$cell =$objWorksheet->getCellByColumnAndRow($currentColumn, $currentRow);
$afCol = PHPExcel_Cell::stringFromColumnIndex($currentColumn+1);
$bfCol = PHPExcel_Cell::stringFromColumnIndex($currentColumn-1);
$col = PHPExcel_Cell::stringFromColumnIndex($currentColumn);
$address = $col.$currentRow;
$value = $objWorksheet->getCell($address)->getValue();
if(substr($value,0,1)=='='){
return array("error"=>0,'message'=>'can not use the formula!');
exit;
}
if($cell->getDataType()==PHPExcel_Cell_DataType::TYPE_NUMERIC){
$cellstyleformat=$cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat();
$formatcode=$cellstyleformat->getFormatCode();
if (preg_match('/^([$[A-Z]*-[0-9A-F]*])*[hmsdy]/i', $formatcode)) {
$value=gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value));
}else{
$value=PHPExcel_Style_NumberFormat::toFormattedString($value,$formatcode);
}
}
if($isMergeCell[$col.$currentRow]&&$isMergeCell[$afCol.$currentRow]&&!empty($value)){
$temp = $value;
}elseif($isMergeCell[$col.$currentRow]&&$isMergeCell[$col.($currentRow-1)]&&empty($value)){
$value=$arr[$currentRow-1][$currentColumn];
}elseif($isMergeCell[$col.$currentRow]&&$isMergeCell[$bfCol.$currentRow]&&empty($value)){
$value=$temp;
}
$row[$currentColumn] = $value;
}
$arr[$currentRow] = $row;
}
$array[$i]["Content"] = $arr;
$i++;
}
spl_autoload_register(array('Think','autoload'));//must, resolve ThinkPHP and PHPExcel conflicts
unset($objWorksheet);
unset($PHPReader);
unset($PHPExcel);
unlink($file);
return array("error"=>1,"data"=>$array);
}
三:在应用层加入触发代码
    //导出用户
public function excel_action()
{
$xlsName = "User";
$xlsCell = array(
array('id','账号序列'),
array('user_login','登录账户'),
array('create_time','注册时间')
);
$xlsData=userModel::all();
$xlsData =db("users")->where("user_status",1)->select();
exportExcel($xlsName,$xlsCell,$xlsData);
}