当前位置:网站首页>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');
}边栏推荐
- Day6 merge two ordered arrays
- Global and Chinese market of micro positioning technology 2022-2028: Research Report on technology, participants, trends, market size and share
- Part 27 supplement (27) buttons of QML basic elements
- AST (Abstract Syntax Tree)
- Meso tetra [P - (p-n-carbazole benzylidene imino)] phenylporphyrin (tcipp) /eu (tcipp) [pc( α- 2-oc8h17) 4] and euh (tcipp) [pc (a-2-oc8h17) 4] supplied by Qiyue
- FPGA 学习笔记:Vivado 2019.1 工程创建
- PR notes:
- FAQs for datawhale learning!
- 【leetcode】1027. Longest arithmetic sequence (dynamic programming)
- Chapter 1: find the algebraic sum of odd factors, find the same decimal sum s (D, n), simplify the same code decimal sum s (D, n), expand the same code decimal sum s (D, n)
猜你喜欢

Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520

2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)

Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system

Phpstudy set LAN access
![CesiumJS 2022^ 源码解读[7] - 3DTiles 的请求、加载处理流程解析](/img/70/6fd00146418e5d481e951d51428990.png)
CesiumJS 2022^ 源码解读[7] - 3DTiles 的请求、加载处理流程解析

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

Explore the internal mechanism of modern browsers (I) (original translation)

How to improve data security by renting servers in Hong Kong

Nerfplusplus parameter format sorting

FPGA learning notes: vivado 2019.1 project creation
随机推荐
FPGA 学习笔记:Vivado 2019.1 工程创建
44. Concurrent programming theory
2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
PR FAQ: how to set PR vertical screen sequence?
Assign the CMD command execution result to a variable
IPv6 experiment
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
【c】 Digital bomb
Pat grade B 1009 is ironic (20 points)
Fingerprint password lock based on Hal Library
MySQL learning notes - single table query
Acquisition and transmission of parameters in automatic testing of JMeter interface
Global and Chinese market of cyanuric acid 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
Day10 -- forced login, token refresh and JWT disable
Test changes in Devops mode -- learning and thinking
Today's work summary and plan: February 14, 2022
The 15 year old interviewer will teach you four unique skills that you must pass the interview
Oak-d raspberry pie cloud project [with detailed code]
NFT without IPFs and completely on the chain?