Using PHPExcel to import and export in YII - CSDN Blog

不言
Release: 2023-03-28 16:52:01
Original
1671 people have browsed it

1. Unzip phpexcel into the protected/vendor directory. The directory structure is vendor/PHPExcel/PHPExcel.php

2. Modify the index.php file

require_once($yii); $app=Yii::createWebApplication($config);//->run(); // adding PHPExcel autoloader Yii::import('application.vendor.*'); require_once "PHPExcel/PHPExcel.php"; require_once "PHPExcel/PHPExcel/Autoloader.php"; Yii::registerAutoloader(array('PHPExcel_Autoloader','Load'), true); $app->run();
Copy after login

3. Export the Excel file

public function actionExcel(){ $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'Hello') ->setCellValue('B2', 'world!') ->setCellValue('C1', 'Hello') ->setCellValue('D2', 'world!'); $objPHPExcel->getActiveSheet()->setTitle('Simple'); $objPHPExcel->setActiveSheetIndex(0); ob_end_clean(); ob_start(); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="test.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); }
Copy after login

4. Import the Excel file of.

$file = CUploadedFile::getInstance($model,'brand_model_file'); var_dump($file->getType()); if(in_array($file->getType(),array('application/vnd.ms-excel','application/excel','application/msexcel','application/kset')) ) { $excelFile = $file->getTempName(); //$phpexcel = new PHPExcel; $excelReader = PHPExcel_IOFactory::createReader('Excel5'); $phpexcel = $excelReader->load($excelFile)->getSheet(0); $total_line = $phpexcel->getHighestRow(); $total_column = $phpexcel->getHighestColumn(); $allData = array(); for ($row = 1; $row <= $total_line; $row++) { $data = array(); for ($column = 'A'; $column <= $total_column; $column++) { $data[] = trim($phpexcel->getCell($column.$row) -> getValue()); } array_push($allData, $data); } print_r($allData);
Copy after login

The above is the entire content of this article, thank you for reading.

Related recommendations:

PHP uses PHPExcel to implement batch upload to the database

How to solve the problem of file name when IE browser uses PHPExcel to export files Problem with Chinese garbled characters


#

The above is the detailed content of Using PHPExcel to import and export in YII - CSDN Blog. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!