当前位置:网站首页>Phpexcel import export
Phpexcel import export
2022-07-03 20:11:00 【Xiao Yu***】
Import :
public function import(){
$oldData=[];
$info=$this->fileUplad();
if($info['code']!=200){
return false;
}
// Get all the fields of the corresponding table and delete the first one id Elements
$title_to_field=array(
' The student's name ' => 'name',
' Gender ' => 'sex',
' ID number ' => 'cardID',
' nation ' => 'nation',
' Political affiliation ' => 'politics_id',
' Religious beliefs ' => 'faith',
' Native place ' => 'native_place',
' Graduation school ' => 'graduation_school',
' major ' => 'major',
' Education ' => 'education',
' Only child or not ' => 'only_child',
' Telephone ' => 'tel',
' Home address ' => 'address',
' label ' => 'tag_id',
' remarks ' => 'remarks',
);
vendor("PHPExcel.PHPExcel.PHPExcel");
vendor("PHPExcel.PHPExcel.IOFactory");
if ($info['exts'] == 'xls') {
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
} else if ($info['exts'] == 'xlsx') {
$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
}else if($info['exts'] == 'csv'){
$objReader = \PHPExcel_IOFactory::createReader('CSV')
->setDelimiter(',')
->setInputEncoding('GBK')
->setEnclosure('"')
->setSheetIndex(0);
}
// load file
$objPHPExcel = $objReader->load($info['path'],$encode='utf-8');
// Get the first worksheet in the table , If you want to get a second , hold 0 Change it to 1, By analogy
$objWorksheet=$objPHPExcel->getSheet(0);
// Get the total number of rows
$highestRow = $objWorksheet->getHighestRow();
// Get total number of columns
$allColumn = $objWorksheet->getHighestColumn();
$allColumn = \PHPExcel_Cell::columnIndexFromString($allColumn);
// Get the header information array
for ($currentColumn=0;$currentColumn<=$allColumn;$currentColumn++){
$cell =$objWorksheet->getCellByColumnAndRow($currentColumn,1);
$value=$cell->getCalculatedValue();
if($value){
$head_list[] = $value;
}
}
// insert data
for ($row=2;$row<=$highestRow;++$row) {
$result=[];
for($i=0;$i<count($head_list);$i++){
$cell =$objWorksheet->getCellByColumnAndRow($i,$row);
$value=$cell->getCalculatedValue();
if (is_object($value)) {
$value = $value->__toString();
}
if(strpos($value,'=')){
$value = "'".$value;
}
if(isset($title_to_field[$head_list[$i]])){
$key=$title_to_field[$head_list[$i]];
$result[$key]=$value;
}
}
$res = self::insert($result);
if(! $res){
continue;
}
}
return true;
}
export :
public static function export()
{
// Query database information
try {
$xlsData = StudentModel::select()->append(['tag_name']);
} catch (\Exception $e) {
return $e->getMessage();
}
Vendor('PHPExcel.PHPExcel');// Call the class library , The path is based on vendor The folder
vendor("PHPExcel.PHPExcel.IOFactory");
// Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
// Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
// Instantiation
$objExcel = new \PHPExcel();
// Set document properties
$objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
// Set contents
$objActSheet = $objExcel->getActiveSheet();
$key = ord("A");
$letter = explode(',', "A,B,C,D,E,F,G,H,I,J,K,L,N,M,O,P");
$arrHeader = array(' The student's name ', ' Gender ', ' ID number ', ' nation ', ' Political affiliation ', ' Religious beliefs ', ' Native place ', ' Graduation school ', ' Education ', ' Is it an only child ', ' Telephone ', ' major ', ' Home phone ', ' Home address ', ' label ', ' remarks ');
// Fill in the header information
$lenth = count($arrHeader);
for ($i = 0; $i < $lenth; $i++) {
$objActSheet->setCellValue("$letter[$i]1", "$arrHeader[$i]");
};
// Fill in the form information
foreach ($xlsData as $k => $v) {
$k += 2;
// Form and content
$objActSheet->setCellValue('A' . $k, $v['name']);
$objActSheet->setCellValue('B' . $k, ($v['sex'] == 1) ? ' male ' : ' Woman ');
$objActSheet->setCellValue('C' . $k, ' '.$v['cardID']);
$objActSheet->setCellValue('D' . $k, $v['nation']);
$objActSheet->setCellValue('E' . $k, config('politics')[$v['politics_id']]);
$objActSheet->setCellValue('F' . $k, $v['faith']);
$objActSheet->setCellValue('G' . $k, $v['native_place']);
$objActSheet->setCellValue('H' . $k, $v['graduation_school']);
$objActSheet->setCellValue('I' . $k, $v['education']);
$objActSheet->setCellValue('J' . $k, ($v['only_child'] == 1) ? ' yes ' : ' no ');
$objActSheet->setCellValue('K' . $k, ' '.$v['tel']);
$objActSheet->setCellValue('L' . $k, $v['major']);
$objActSheet->setCellValue('M' . $k, $v['family_tel']);
$objActSheet->setCellValue('N' . $k, $v['address']);
$objActSheet->setCellValue('O' . $k, $v['tag_name']);
$objActSheet->setCellValue('P' . $k, $v['remarks']);
// Image generation
//$objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
//$objDrawing[$k]->setPath(ROOT_PATH."public/static/image/playbtn.png");
// Set the width and height
//$objDrawing[$k]->setHeight(40);// The height of the picture
//$objDrawing[$k]->setWidth(40); // Photo width
// Set the cell to insert the picture into
//$objDrawing[$k]->setCoordinates('C' . $k);
// Image offset distance
//$objDrawing[$k]->setOffsetX(30);
//$objDrawing[$k]->setOffsetY(12);
//$objDrawing[$k]->setWorksheet($objExcel->getActiveSheet());
// Table height
$objActSheet->getRowDimension($k)->setRowHeight(20);
}
$width = array(20, 20, 15, 10, 10, 30, 10, 15);
// Set the width of the table
$objActSheet->getColumnDimension('A')->setWidth($width[5]);
$objActSheet->getColumnDimension('B')->setWidth($width[1]);
$objActSheet->getColumnDimension('C')->setWidth($width[0]);
$objActSheet->getColumnDimension('D')->setWidth($width[5]);
$objActSheet->getColumnDimension('E')->setWidth($width[5]);
$outfile = md5(" Personnel list " . time()) . ".xlsx";
ob_end_clean();
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="' . $outfile . '"');
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$objWriter->save('php://output');
}
边栏推荐
- 4. Data splitting of Flink real-time project
- Bright purple crystal meso tetra (4-aminophenyl) porphyrin tapp/tapppt/tappco/tappcd/tappzn/tapppd/tappcu/tappni/tappfe/tappmn metal complex - supplied by Qiyue
- 【leetcode】1027. Longest arithmetic sequence (dynamic programming)
- CMD implements the language conversion of locale non Unicode programs
- 2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]
- Parental delegation mechanism
- Promethus
- Micro service knowledge sorting - three pieces of micro Service Technology
- Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
- About unregistered transfer login page
猜你喜欢
2.3 other data types
2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
Oak-d raspberry pie cloud project [with detailed code]
IP address is such an important knowledge that it's useless to listen to a younger student?
FPGA learning notes: vivado 2019.1 project creation
2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers
Xctf attack and defense world crypto advanced area best_ rsa
5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes
[effective Objective-C] - block and grand central distribution
kubernetes集群搭建efk日志收集平台
随机推荐
Find a line in a file and remove it
Today's work summary and plan: February 14, 2022
2022 - 06 - 30 networker Advanced (XIV) Routing Policy Matching Tool [ACL, IP prefix list] and policy tool [Filter Policy]
Microservice framework - frequently asked questions
unittest框架基本使用
Global and Chinese market of speed limiter 2022-2028: Research Report on technology, participants, trends, market size and share
Chapter 1: find all factorial sums, Grand Prix site unified programming, three factorial sums, graphic point scanning, recursive factorial n of n!, Find the factorial n of n!, King Shehan miscalculate
2022-06-28 advanced network engineering (XIII) IS-IS route filtering, route summary, authentication, factors affecting the establishment of Isis neighbor relations, other commands and characteristics
App compliance
Oak-d raspberry pie cloud project [with detailed code]
2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
BOC protected alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC supplied by Qiyu
Use of aggregate functions
原生表格-滚动-合并功能
2.5 conversion of different data types (2)
kubernetes集群搭建efk日志收集平台
Global and Chinese markets of active matrix LCD 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of two in one notebook computers 2022-2028: Research Report on technology, participants, trends, market size and share
P5.js development - setting
Rad+xray vulnerability scanning tool