PHP Excel processing xlsx

1: After obtaining the uploaded file content, import the database array

function importExecl($file='', $sheet=0,$all=false,$columnH=''){
$file = iconv("utf-8", "gb2312", $file); //Transcoding
if(empty($file) OR !file_exists($file)) {
die('file not exists!');
}

if(!class_exists('PHPExcel_Reader_Excel2007')) {
require_once('./PHPExcel-1.8/Classes/PHPExcel.php'); //Introduce the PHP EXCEL class
}
$objRead = new PHPExcel_Reader_Excel2007(); //Create reader object
if(!$objRead->canRead($file)){
$objRead = new PHPExcel_Reader_Excel5();
if(!$objRead->canRead($file)){
die('No Excel!');
}
}

$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');

$obj = $objRead->load($file); //Create excel object
$currSheet = $obj->getSheet($sheet); //Get the specified sheet
if(!$columnH) {
$columnH = $currSheet->getHighestColumn(); //Get the largest column number
if(strlen($columnH) > 2) {
$columnH = 'AZ';
}
}
$columnCnt = array_search($columnH, $cellName);

$rowCnt = $currSheet->getHighestRow(); //Get the total number of rows

$data = array();
for($_row=1; $_row<=$rowCnt; $_row + + ){ //Read content
for($_column=0; $_column<=$columnCnt; $_column + + ){
$cellId = $cellName[$_column].$_row;
//$cellValue = $currSheet->getCell($cellId)->getValue(); //Get the formula itself
$cellValue = $currSheet->getCell($cellId)->getFormattedValue(); //The value obtained is the value calculated by the formula
            //$cellValue = $currSheet->getCell($cellId)->getCalculatedValue(); #There will be objects
if($cellValue instanceof PHPExcel_RichText){ //Rich text conversion string
$cellValue = $cellValue->__toString();
}
$data[$_row][$cellName[$_column]] = $cellValue;
}
}

$data_all = [];
if($all) {
$sheet = $obj->getSheetCount();
for($i=1;$i<$sheet;$i + + ) {
$data_all[] = importExecl($file,$i);
}
return $data_all;
}
return $data;
}

2: Export data to xlsx

function exportExcel($title=array(), $data=array(), $fileName='', $savePath='./', $isDown=false){
require_once('./PHPExcel-1.8/Classes/PHPExcel.php');
$obj = new PHPExcel();

//horizontal cell identifier
$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');

$obj->getActiveSheet(0)->setTitle('Yunshang'); //Set the sheet name
$_row = 1; //Set vertical cell identifier
if($title){
$_cnt = count($title);
$obj->getActiveSheet(0)->mergeCells('A'.$_row.':'.$cellName[$_cnt-1].$_row); //Merge cells
$obj->setActiveSheetIndex(0)->setCellValue('A'.$_row, 'Data export:'.date('Y-m-d H:i:s')); //Set the merged cell content
$_row + + ;
$i = 0;
foreach($title AS $v){ //Set column title
$obj->setActiveSheetIndex(0)->setCellValue($cellName[$i].$_row, $v);
$i + + ;
}
$_row + + ;
}

//Fill in data
if($data){
$i = 0;
foreach($data AS $_v){
$j = 0;
foreach($_v AS $_cell){
$obj->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + $_row), $_cell);
if(preg_match('/[\x{4e00}-\x{9fa5}]/u', $_cell) === 1){
$length = intval(strlen($_cell));
//Contains Chinese
$obj->getActiveSheet()->getColumnDimension($cellName[$j])->setWidth($length);
}else {
$obj->getActiveSheet()->getColumnDimension($cellName[$j])->setAutoSize(true);
}
$j + + ;
}
$i + + ;
}
}
//File name processing
if(!$fileName){
        $fileName = uniqid(time(),true);
    }

$objWrite = PHPExcel_IOFactory::createWriter($obj, 'Excel2007');

if($isDown){ //Webpage download
header('pragma:public');
header("Content-Disposition:attachment;filename=$fileName.xls");
        $objWrite->save('php://output');exit;
}

$_fileName = iconv("utf-8", "gb2312", $fileName); //Transcoding
$_savePath = $savePath.$_fileName.'.xlsx';
$objWrite->save($_savePath);

return $savePath.$fileName.'.xlsx';
}

3: Print multiple sheets

/** Print multiple sheet pages */
function exportExcel2($sheet_obj, $fileName='', $isDown=false, $savePath='./'){
require_once('./PHPExcel-1.8/Classes/PHPExcel.php');
$obj = new PHPExcel();

//horizontal cell identifier
$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');

foreach($sheet_obj as $key => $val) {
if($key != 0) {
$obj->createSheet();
}else {
$obj->getActiveSheet($key)->setTitle($val['sheet_name']); //Set the sheet name
}

$_row = 1; //Set vertical cell identifier
if($val['title']){
$_cnt = count($val['title']);
$obj->getActiveSheet($key)->mergeCells('A'.$_row.':'.$cellName[$_cnt-1].$_row); //Merge cells
$obj->setActiveSheetIndex($key)->setCellValue('A'.$_row, 'Department Classification'.$val['sheet_name'].'--Data export: '.date ('Y-m-d H:i:s')); //Set the merged cell content
$_row + + ;
$i = 0;
foreach($val['title'] AS $v){ //Set column title
$obj->setActiveSheetIndex($key)->setCellValue($cellName[$i].$_row, $v);
$i + + ;
}
$_row + + ;
}

//Fill in data
if($val['data']){
$i = 0;
foreach($val['data'] AS $_v){
$j = 0;
foreach($_v AS $_cell){
$obj->getActiveSheet($key)->setCellValue($cellName[$j] . ($i + $_row), $_cell);
$j + + ;
}
$i + + ;
}
}
}

//File name processing
if(!$fileName){
        $fileName = uniqid(time(),true);
    }

$objWrite = PHPExcel_IOFactory::createWriter($obj, 'Excel2007');

if($isDown){ //Webpage download
header('pragma:public');
header("Content-Disposition:attachment;filename=$fileName.xls");
        $objWrite->save('php://output');exit;
}

$_fileName = iconv("utf-8", "gb2312", $fileName); //Transcoding
$_savePath = $savePath.$_fileName.'.xlsx';
$objWrite->save($_savePath);

return $savePath.$fileName.'.xlsx';
}