<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                準備工作 1、excel表文件名為test20170919.xls,存放于當前項目目錄下的.\upload\excel文件夾中; 2、利用PHPExcel組件讀取excel表文件內容。 PS:PHPExcel組件使用詳細內容可參考《Yii2-手動安裝、引用第三方插件PHPExcel實例詳解,Excel導出》,http://www.yiichina.com/topic/7142 3、寫入的數據庫表名為country。 PS:一次性添加多條數據至數據庫詳細說明可參考《使用createCommand()函數向數據庫添加1條或多條數據》,http://www.yiichina.com/topic/7253 實例代碼如下: <?php namespace app\controllers; use Yii; use yii\web\Controller; use app\models\Country; use PHPExcel; class ImportController extends Controller { public function actionImport() { $basePath=Yii::$app->basePath; $file_url=$basePath.'\upload\excel\test20170919.xls'; $filename=$file_url; $objPHPExcelnew=new PHPExcel(); $objReader= \PHPExcel_IOFactory::createReader('Excel5'); $objPHPExcel=$objReader->load($filename); $sheet=$objPHPExcel->getActiveSheet(); $highestRow=$sheet->getHighestRow(); $highestColumn=$sheet->getHighestColumn(); $highestColumnIndex=\PHPExcel_Cell::columnIndexFromString($highestColumn); $excelData=array(); for($row=2;$row<=$highestRow;$row++) { for($col=0;$col<$highestColumnIndex;$col++) { $excelData[$row][]=(string)$sheet->getCellByColumnAndRow($col,$row)->getValue(); } } Yii::$app->db->createCommand()->batchInsert('country', ['code', 'name','population'],$excelData)->execute(); echo 'insert success.'; } } wwwwwwwwww # #### # Yii2-手動安裝、引用第三方插件PHPExcel實例詳解,Excel導出 0、下載PHPExcel 下載phpexcel地址:https://github.com/PHPOffice/PHPExcel/archive/1.8.1.zip 下載完成后,解壓PHPExcel-1.8.1.zip文件,進入Classes文件夾目錄,復制里面的子文件及子文件夾內容(即類文件)。 ![](https://box.kancloud.cn/0251d03b9a4d6f9a6b6c2ea4f2863213_1101x640.jpg) 1、將PHPExcel類文件夾放入libs 在yii2根目錄下,新建libs文件夾,并將下載好的PHPExcel類文件夾放入libs文件夾下 1-1.png 1-2.png 2、配置web.php,加入PHPExcel.php類 在D:\wamp\Apache2.2\htdocs\myyii2\config文件夾下,打開web.php文件 添加如下代碼: Yii::$classMap['PHPExcel']='@app/libs/PHPExcel.php'; 2-1.png 2-2.png 3、創建PHPExcel導出控制器 在D:\wamp\Apache2.2\htdocs\myyii2\controllers文件夾下創建ExportController.php控制器文件。 3-1.png 添加以下代碼: <?php namespace app\controllers; use Yii; use yii\web\Controller; use app\models\Country; use PHPExcel; class ExportController extends Controller { public function actionExport(){ $data=Country::find()->asArray()->all(); if(!$data){ return $this->redirect(['Country/index']); }else{ $objPHPExcel=new PHPExcel(); $objPHPExcel->getProperties()->setCreator("zhangsan") ->setLastModifiedBy("lisi") ->setTitle("my title") ->setSubject("my subject") ->setDescription("my description") ->setKeywords("my keywords") ->setCategory("my category"); // 設置列寬 $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20); // 設置表頭 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', '代碼') ->setCellValue('B1', '國家') ->setCellValue('C1', '地區'); $n=2; // 設置內容 foreach($data as $v){ $objPHPExcel->getActiveSheet()->setCellValue('A'.$n,$v['code']); $objPHPExcel->getActiveSheet()->setCellValue('B'.$n,$v['name']); $objPHPExcel->getActiveSheet()->setCellValue('C'.$n,$v['population']); $n=$n+1; } // 重命名 $objPHPExcel->getActiveSheet()->setTitle('test-sheet'); $objPHPExcel->setActiveSheetIndex(0); // 輸出 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'); } } } 4、執行控制器中的export動作 在瀏覽器中輸入網址:http://localhost/myyii2/web/index.php?r=export/export,回車執行excel導出動作。 4-1.png 雙擊打開excel文件核對excel內容是否與代碼設定的內容一致 4-2.png 右鍵test.xls文件,點擊屬性-->詳細信息,發現屬性設置與代碼中設置的表格屬性一致。 4-3.png # 使用createCommand()函數向數據庫添加1條或多條數據 使用createCommand()函數向數據庫添加數據 添加1條數據 Yii::$app->db->createCommand()->insert(表名, ['字段1'=>'字段1內容', '字段2'=>'字段2內容','字段3'=>'字段3內容'])->execute(); public function actionAddOne() { Yii::$app->db->createCommand()->insert('country',['code'=>'A1','name'=>'A111111','population'=>'123456789'])->execute(); } 添加多條數據 Yii::$app->db->createCommand()->batchInsert(表名, ['字段1', '字段2','字段3'], [['字段1內容', '字段2內容','字段3內容'], ['字段1內容', '字段2內容','字段3內容'], ['字段1內容', '字段2內容','字段3內容'], ])->execute(); public function actionAddAll() { Yii::$app->db->createCommand()->batchInsert('country', ['code', 'name','population'], [ ['AA', 'Aaaaaa','123456789'], ['BB', 'Bbbbbb','123456789'], ['CC', 'Cccccc','123456789'], ])->execute(); } //[刪除]數據 public function actionDeleteOne() { Yii::$app->db->createCommand()->delete('country',['code'=>'A1'])->execute(); } //[修改]數據 public function actionUpdateOne() { Yii::$app->db->createCommand()->update('country',['name'=>'A16666','population'=>'233456'],['code'=>'A1'])->execute(); } 更多補充 //獲取自增ID $id=Yii::$app->db->getLastInsertID(); //[查詢]數據 // 查詢返回多行: $command = Yii::$app->db->createCommand('SELECT * FROM post'); $posts = $command->queryAll(); // 返回單行 $command = Yii::$app->db->createCommand('SELECT * FROM post WHERE id=1'); $post = $command->queryOne(); // 查詢多行單值: $command = Yii::$app->db->createCommand('SELECT title FROM post'); $titles = $command->queryColumn(); // 查詢標量值/計算值: $command = Yii::$app->db->createCommand('SELECT COUNT(*) FROM post'); $postCount = $command->queryScalar();
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看