当前位置:网站首页>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');
}边栏推荐
- Pat grade B 1009 is ironic (20 points)
- Day6 merge two ordered arrays
- kubernetes集群搭建efk日志收集平台
- 2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
- 2.1 use of variables
- 2.4 conversion of different data types
- Ae/pr/fcpx super visual effects plug-in package fxfactory
- The simplicity of laravel
- CesiumJS 2022^ 源码解读[7] - 3DTiles 的请求、加载处理流程解析
- WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
猜你喜欢
![2022 - 06 - 30 networker Advanced (XIV) Routing Policy Matching Tool [ACL, IP prefix list] and policy tool [Filter Policy]](/img/b6/5d6b946d8001e2d73c2cadbdce72fc.png)
2022 - 06 - 30 networker Advanced (XIV) Routing Policy Matching Tool [ACL, IP prefix list] and policy tool [Filter Policy]

BOC protected phenylalanine zinc porphyrin (Zn · TAPP Phe BOC) / iron porphyrin (Fe · TAPP Phe BOC) / nickel porphyrin (Ni · TAPP Phe BOC) / manganese porphyrin (Mn · TAPP Phe BOC) Qiyue Keke
![2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]](/img/b6/5d6b946d8001e2d73c2cadbdce72fc.png)
2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]

2.2 integer

How to do Taobao full screen rotation code? Taobao rotation tmall full screen rotation code

How can the outside world get values when using nodejs to link MySQL
![[effective Objective-C] - block and grand central distribution](/img/09/22b979b97ea13d649b4b904637b79f.jpg)
[effective Objective-C] - block and grand central distribution

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

Ae/pr/fcpx super visual effects plug-in package fxfactory

2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
随机推荐
Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
Find a line in a file and remove it
6006. Take out the minimum number of magic beans
P5.js development - setting
Global and Chinese markets of cast iron diaphragm valves 2022-2028: Research Report on technology, participants, trends, market size and share
BOC protected tryptophan zinc porphyrin (Zn · TAPP Trp BOC) / copper porphyrin (Cu · TAPP Trp BOC) / cobalt porphyrin (cobalt · TAPP Trp BOC) / iron porphyrin (Fe · TAPP Trp BOC) / Qiyue supply
4. Data binding
7. Data broker presentation
Professional interpretation | how to become an SQL developer
2.3 other data types
MySQL learning notes - single table query
2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
QT tutorial: signal and slot mechanism
3. Data binding
About callback function and hook function
The 15 year old interviewer will teach you four unique skills that you must pass the interview
How can the outside world get values when using nodejs to link MySQL
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
Global and Chinese market of micro positioning technology 2022-2028: Research Report on technology, participants, trends, market size and share
Geek Daily: the system of monitoring employees' turnover intention has been deeply convinced off the shelves; The meta universe app of wechat and QQ was actively removed from the shelves; IntelliJ pla