Kohana Import and Export

Source: Internet
Author: User
Tags kohana

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.