1.安装 phalapi2.x 框架,直接下载一个空框架
2.安装composer 官网入门介绍里 有个exe文件直接下载 安装其他比较麻烦, 安装完成后运行cmd 输入 composer -v 成功的话会显示composer信息
3.导入第三方插件 的2方法 (phpexcel 已经被抛弃 phpoffice/phpspreadsheet可用)
一。cmd里面直接cd到项目路径 然后运行 composer require phpoffice/phpspreadsheet
二。phpstorm 里打开Terminal 在对应项目路径 运行 composer require phpoffice/phpspreadsheet
4.引用
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
5.导入导出部分代码
public function getRules() {
return array(
'index' => array(
'username' => array('name' => 'username', 'desc' => '用户名字'),
),
'importExcel' => array(
'excel' => array(
'name' => 'upfile',
'type' => 'file',
)
),
);
}
/**
* 导入excel接口
* @desc 把excel文件导入到数据库
* @return int result 导入结果
*/
public function importExcel() {
$model = new ModelCard();
$filename = $this->excel['tmp_name'];
$excel = IOFactory::load($filename);
$sheet = $excel->getSheet(0);
$highestRow = $sheet->getHighestRow();
//$import_data = array();
//循环导入
$count = 0;
for($i = 2; $i <= $highestRow; $i++) {
$data = array(
'card_code' => $sheet->getCellByColumnAndRow(1, $i)->getValue(),
'card_pwd' => (string)$sheet->getCellByColumnAndRow(2, $i)->getValue(),
'gift_id' => $sheet->getCellByColumnAndRow(3, $i)->getValue(),
);
$result = $model->insert($data);