One. Import
Ini_set (' Memory_limit ', ' 512M ');
Require_once (Kohana::find_file (' Vendor ', ' phpexcel/phpexcel/iofactory '));
$xls = $_files[' xls ' [' tmp_name '];
$error _msgs = Array ();
if ($xls) {
$type = pathinfo ($_files[' xls ' [' name '], pathinfo_extension);
if ($type = = ' xlsx ' | | $type = = ' xls ') {
$objPHPExcel = Phpexcel_iofactory::load ($xls);
} else if ($type = = ' csv ') {
$objReader = Phpexcel_iofactory::createreader (' CSV ')
->setdelimiter (', ')
->setinputencoding (' GBK ')//not set will result in Chinese column content returning Boolean (false) or garbled
->setenclosure (' "')
->setlineending ("\ r \ n")
->setsheetindex (0);
$objPHPExcel = $objReader->load ($xls);
}
$objWorkSheet = $objPHPExcel->getactivesheet ();
$maxRow = $objPHPExcel->getactivesheet ()
->gethighestrow ();
$maxCol = $objPHPExcel->getactivesheet ()
->gethighestcolumn ();
if ($maxCol = = ' J ') {
for ($row = 2; $row <= $maxRow; $row + +) {
for ($col = 0; $col < $col + +) {
$val = $objWorkSheet->getcellbycolumnandrow ($col, $row)
->getvalue ();
$data [$row] [$col] = Trim ($val);
}
}
}
Two, export
Download templates
Public Function action_download_temp ()
{
Require_once (Kohana::find_file (' Vendor ', ' phpexcel/phpexcel/iofactory '));
$this->objreader = Phpexcel_iofactory::createreader (' Excel5 ');
$objPHPExcel = new Phpexcel ();
$objPHPExcel->getproperties ()
->setcreator ("Newerp")
->setlastmodifiedby ("Newerp")
->settitle ("Office 2003 XLSX Test Document")
->setsubject ("Office 2003 XLSX Test Document")
->setdescription ("Test document for Office 2003 XLSX, generated using PHP classes.")
->setkeywords ("Office 2003 OPENXML PHP")
->setcategory ("Test result file");
$objActSheet = $objPHPExcel->setactivesheetindex (0);
$objActSheet->setcellvalue (' A1 ', ' name ');
$objActSheet->setcellvalue (' B1 ', ' contacts ');
$objActSheet->setcellvalue (' C1 ', ' contact phone ');
$objActSheet->setcellvalue (' D1 ', ' QQ ');
$objActSheet->setcellvalue (' E1 ', ' other Im ');
$objActSheet->setcellvalue (' F1 ', ' Email ');
$objActSheet->setcellvalue (' G1 ', ' address ');
$objActSheet->setcellvalue (' H1 ', ' zip ');
$objActSheet->setcellvalue (' I1 ', ' Fax ');
$objActSheet->setcellvalue (' J1 ', ' url ');
$objActSheet->setcellvalue (' K1 ', ' business scope ');
$objActSheet->setcellvalue (' L1 ', ' deposit ');
$objActSheet->setcellvalue (' M1 ', ' remarks ');
$objActSheet->setcellvalue (' N1 ', ' sort ');
$objActSheet->setcellvalue (' O1 ', ' mnemonic Code ');
$objPHPExcel->getactivesheet ()
->settitle (' Load ');
$objPHPExcel->setactivesheetindex (0);
$day = Date ("m-d");
Header ("Content-type:application/vnd.ms-excel; Charset=utf-8 ");
Header (' Content-disposition:attachment;filename= '. $day. ' Platform summary table. xls ');
Header (' cache-control:max-age=0 ');
$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 ');
$objWriter->save (' php://output ');
Exit
}
Kohana Import and Export