kohana导入和导出

一.导入

 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')//不设置将导致中文列内容返回boolean(false)或乱码
->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 < 10; $col++) {
$val = $objWorkSheet->getCellByColumnAndRow($col, $row)
->getValue();
$data[$row][$col] = trim($val);
}
}
}


二,导出
//下载模板
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', '名称');
$objActSheet->setCellValue('B1', '联系人');
$objActSheet->setCellValue('C1', '联系电话');
$objActSheet->setCellValue('D1', 'QQ');
$objActSheet->setCellValue('E1', '其他IM');
$objActSheet->setCellValue('F1', 'Email');
$objActSheet->setCellValue('G1', '地址');
$objActSheet->setCellValue('H1', '邮编');
$objActSheet->setCellValue('I1', '传真');
$objActSheet->setCellValue('J1', '网址');
$objActSheet->setCellValue('K1', '经营范围');
$objActSheet->setCellValue('L1', '押金');
$objActSheet->setCellValue('M1', '备注');
$objActSheet->setCellValue('N1', '排序');
$objActSheet->setCellValue('O1', '助记码');


$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 . '平台汇总表.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
posted @ 2015-04-08 11:54  KOBE-Jason  阅读(549)  评论(0编辑  收藏  举报