【PHPOffice/PhpSpreadsheet的基本使用】
PHPOffice/PhpSpreadsheet:?[https://github.com/PHPOffice/PhpSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet)
document:?[https://phpspreadsheet.readthedocs.io/en/latest/](https://phpspreadsheet.readthedocs.io/en/latest/)
```
composer require phpoffice/phpspreadsheet
```
1.1.Hello World
```
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
```
1.2.上傳excel輸出整段php array
form.php
```
<form method="post" enctype="multipart/form-data" action="readfile.php">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" class="form-control" id="exampleInputFile">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
```
readfile.php
```
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
$file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
if(isset($_FILES['file']['name']) && in_array($_FILES['file']['type'], $file_mimes)) {
$arr_file = explode('.', $_FILES['file']['name']);
$extension = end($arr_file);
if('csv' == $extension) {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
} else {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
}
$spreadsheet = $reader->load($_FILES['file']['tmp_name']);
// start customize
$sheetData = $spreadsheet->getActiveSheet()->ToArray();
echo '<pre>';
print_r($sheetData);
// /end customize
}
```
遍歷所有內容:
```
// start customize
$worksheet = $spreadsheet->getActiveSheet();
echo '<table>' . PHP_EOL;
foreach ($worksheet->getRowIterator() as $row) {
echo '<tr>' . PHP_EOL;
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells,
// even if a cell value is not set.
// By default, only cells that have a value
// set will be iterated.
foreach ($cellIterator as $cell) {
echo '<td>' .
$cell->getValue() .
'</td>' . PHP_EOL;
}
echo '</tr>' . PHP_EOL;
}
echo '</table>' . PHP_EOL;
// /end customize
```
指定位置顯示內容:
```
// start customize
$sheetData = $spreadsheet->getActiveSheet()->rangeToArray(
'C3:E5', // The worksheet range that we want to retrieve
NULL, // Value that should be returned for empty cells
TRUE, // Should formulas be calculated (the equivalent of getCalculatedValue() for each cell)
TRUE, // Should values be formatted (the equivalent of getFormattedValue() for each cell)
TRUE // Should the array be indexed by cell row and cell column
);
// /end customize
```
指定Excel列放入database:
```
// start customize
$sheetData = $spreadsheet->getActiveSheet()->ToArray();
$connect = mysqli_connect("localhost", "root", "root", "test");
if(is_array($sheetData))
{
foreach($sheetData as $row => $value)
{
$data1 = mysqli_real_escape_string($connect, $value[0]);
$data2 = mysqli_real_escape_string($connect, $value[1]);
$data3 = mysqli_real_escape_string($connect, $value[3]);
$sql = "INSERT INTO demo(list1, list2, list3) VALUES ('".$data1."', '".$data2."', '".$data3."')";
mysqli_query($connect, $sql);
}
echo 'success';
}
// /end customize
```
將Excel指定內容用json格式輸出:
- 高級工程師或以上級別的行為習慣
- Laravel封裝
- 公用類
- 查詢
- 項目1
- Laravel CMS
- 無限分級列表
- Laravel
- Laravel簡介
- Laravel下載和安裝
- 微服務
- Laravel身份認證
- Laravel Passport
- 后臺身份驗證
- Passport實現前后端登錄認證
- Laravel Sanctum
- Laravel輔助
- Unitest
- Laravel常用命令行
- Laravel數據庫配置
- Laravel連接數據庫模板
- Laravel數據庫操作
- Laravel路由
- Laravel操作
- Laravel Blade模板引擎
- Laravel CRUD (create, read, update, delete)
- Laravel實例
- Laravel Eloquent
- belongsTo和belongsToMany案例
- Laravel Package
- Laravel File Manager
- Laravel language
- Laravel查詢
- Laravel合并查詢
- Laravel統計
- Laravel Voyager Blog System
- Laravel Excel
- Laravel AsgardCms后臺
- Laravel基礎集合
- Laravel 備份和更新
- Laravel和Codeigniter對比
- Laravel事務處理
- Laravel Redis
- Laravel在Nginx服務器運行時遇到404錯誤的修正
- Laravel安全
- Laravel JWT
- Laravel Passport
- Laravel加密和解密
- Laravel sent email
- laravel 5.4 eloquent-relationships
- 加入變量文件
- Laravel migrations
- Laravel隊列
- Laravel+RabbitMQ
- Laravel OSS
- CMS設計
- 角色權限設計
- Lumen
- Lumen API
- lumen email
- 服務器
- Cloud9使用
- 配置本地服務器多域名
- PHP運行環境部署方式
- 同時運行PHP、Java、Python
- Linux操作
- Composer
- 支付開發
- 微信支付
- 支付寶API
- PayPal開發
- PHP基礎
- JavaScript相對PHP開發側重後臺功能的劣勢
- PHP foreach 和 for循環
- PHP檢測數組有哪里連續的字符串,以及有多少個
- Session
- 域名判斷顯示
- 檢測登錄設備
- PHP foreach循環, 則只循環相同數據一次
- PHP計算搜索過程所用的時間
- PHP文件處理
- Session和cookie案例:登錄時的記住我功能
- PHP array判斷
- PHP剔除數組中相同子元素的值
- PHP分頁
- PHP "三元運算符"簡寫
- 防CSRF跨站請求偽造攻擊
- PHP Json system
- crul
- PHP爬蟲
- 參考
- PHP一和二維數組
- 格式轉換
- PHP抓取網頁指定內容
- PHP Simple HTML DOM Parser
- PHP提取多維數組指定一列的方法大全
- PHP將第一個字符轉為指定內容
- OpenSSL加密解密
- 圖片+文字
- PHP理論
- Opcache
- Git
- 安裝Git
- Git操作
- Git參考
- 參考筆記
- 程序員接單平臺
- 后端PHP面試
- 基礎面試
- MySQL訓練題
- 經典算法題
- 框架原理
- 高并發
- 微服務架構
- 安全性
- 分布式設計
- 服務器集群
- 模擬題目
- 模擬面試1
- 模擬面試2
- 網絡協議
- 英文面試
- 高端PHP面試取勝點
- MVC設計
- 模板引擎
- 算法高效解題
- 算法概述
- leetcode算法題
- 排序
- SQL
- MySQL
- MySQL連接
- Join案例
- MySQL參考文章
- mysql_pdo_select
- mysql_pdo_insert
- MySQL索引
- MySQL事務
- MySQL將數據表字段不重復的值合成一個數組
- MySQL常用語句
- 備份
- MySQL other
- MsSQL
- MsSQL連接
- MySQL PDO連接
- MsSQL安裝
- mssql_pdo_select
- mssql_pdo_insert
- MySQL past
- MySql常用操作
- MySQL基礎練習
- MySQL設計
- MySQL案例
- php array轉換放入數據表
- PHP Mysql連接數據庫并輸出數據
- MySQL常用命令
- MySQL導出導入
- PDO
- Excel
- Excel在Drupal的應用
- Excel在Laravel的應用
- PHPOffice/PhpSpreadsheet的基本使用
- 用PHP Simple HTML DOM Parser獲取指定列的內容
- PHPOffice/PhpSpreadsheet的案例
- Sublime Text
- elasticsearch
- 后端基礎核心:數據的輸入和輸出
- 發送郵件功能
- PHPMailer發送郵件功能源碼
- PHPMailer發送郵件功能源碼(詳細案例)
- 常用cnd鏈接
- API
- API ajax+php demo
- PHP Postman API
- API1
- demo-api
- Google API
- google sheet data insert into database
- Laravel API
- Google Analytics
- 其他工具
- 經驗
- 編程注意事項
- 開發語言的差別
- 軟件產品
- 經驗之談
- test
- Bootstrap 4
- 1.demo page
- CMS設計
- 編輯器
- Other