宅男在线永久免费观看网直播,亚洲欧洲日产国码无码久久99,野花社区在线观看视频,亚洲人交乣女bbw,一本一本久久a久久精品综合不卡

全部
常見問題
產(chǎn)品動(dòng)態(tài)
精選推薦

ThinkPHP6輕松搞定Excel導(dǎo)入導(dǎo)出,讓你的工作效率翻倍!

管理 管理 編輯 刪除

隨著互聯(lián)網(wǎng)的快速發(fā)展,Excel已經(jīng)成為公司和個(gè)人日常辦公中重要的工具之一。因此,Excel導(dǎo)入導(dǎo)出的功能已經(jīng)成為許多應(yīng)用程序的必要組成部分。如何使用ThinkPHP6實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出呢?下面,本文將為您詳細(xì)介紹。

一、ThinkPHP6系統(tǒng)環(huán)境驗(yàn)證

使用ThinkPHP6實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出,首先需要滿足系統(tǒng)環(huán)境要求。在ThinkPHP6中,可以使用composer安裝phpoffice/phpspreadsheet庫(kù),實(shí)現(xiàn)Excel處理功能。在命令行中執(zhí)行以下命令進(jìn)行安裝:

composer require phpoffice/phpspreadsheet

安裝完畢后,在controller層中引入PhpOfficePhpSpreadsheetSpreadsheet和PhpOfficePhpSpreadsheetWriterXlsx類庫(kù),這些類庫(kù)將在我們的后面代碼中使用。

二、Excel導(dǎo)出

1. 導(dǎo)出Excel數(shù)據(jù)

使用ThinkPHP6實(shí)現(xiàn)Excel導(dǎo)出,首先需要將數(shù)據(jù)導(dǎo)入到Excel中。我們可以根據(jù)需要,在controller層中編寫相應(yīng)的代碼。例如,在導(dǎo)出學(xué)生信息時(shí),可以通過查詢數(shù)據(jù)庫(kù)獲取相關(guān)信息。

use appcommonmodelStudent;

public function export()
{
    $students = Student::all();
}

2. 設(shè)置Excel表格的頭部

Excel的表格頭部是非常重要的一部分。為了使Excel表格的頭部清晰明了,我們可以編寫代碼來(lái)生成表格的頭部信息。在下面的代碼中,我們使用了循環(huán)來(lái)實(shí)現(xiàn)。

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('學(xué)生信息表');

$sheet->setCellValue('A1', '序號(hào)');
$sheet->setCellValue('B1', '學(xué)號(hào)');
$sheet->setCellValue('C1', '姓名');
$sheet->setCellValue('D1', '性別');
$sheet->setCellValue('E1', '日期');

3. 將數(shù)據(jù)寫入Excel表格

接下來(lái),將獲取的數(shù)據(jù)寫入Excel表格中。循環(huán)遍歷數(shù)據(jù),將每個(gè)學(xué)生的信息依次寫入表格中。

foreach ($students as $key => $item) {
    $num = $key + 2;
    $sheet->setCellValue('A' . $num, $num - 1);
    $sheet->setCellValue('B' . $num, $item->number);
    $sheet->setCellValue('C' . $num, $item->name);
    $sheet->setCellValue('D' . $num, $item->sex);
    $sheet->setCellValue('E' . $num, $item->date);
}

4. 將Excel表格輸出并保存到本地

最后,將生成的Excel表格輸出并保存到本地。在下面的代碼中,我們使用writer來(lái)將Excel表格保存為.xlsx格式并輸出到瀏覽器中。

$writer = new Xlsx($spreadsheet);
$filename = "學(xué)生信息表.xlsx";
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=' . $filename);
header('Cache-Control: max-age=0');
$writer->save('php://output');
exit();

三、Excel導(dǎo)入

除了導(dǎo)出Excel,ThinkPHP6還可以實(shí)現(xiàn)Excel的導(dǎo)入功能。接下來(lái),我們將為您介紹ThinkPHP6中實(shí)現(xiàn)Excel導(dǎo)入的方法。

1. 上傳Excel文件并讀取數(shù)據(jù)

在導(dǎo)入Excel之前,我們需要先上傳Excel文件。在這里,我們使用了ThinkPHP6自帶的文件上傳類庫(kù)。上傳成功之后,使用PhpOfficePhpSpreadsheetIOFactory類庫(kù)中的方法,讀取上傳的Excel文件中的數(shù)據(jù)。

use thinkacadeFilesystem;

$uploadFile = request()->file('file');
$saveName = Filesystem::disk('public')->putFile('excel', $uploadFile);
$filePath = Filesystem::disk('public')->path($saveName);

$spreadsheet = PhpOfficePhpSpreadsheetIOFactory::load($filePath);
$sheet = $spreadsheet->getActiveSheet();
$highestRow = $sheet->getHighestRow();
$data = [];

2. 遍歷Excel表格并得到數(shù)據(jù)

獲取到Excel表格數(shù)據(jù)后,我們需要將數(shù)據(jù)遍歷一遍,使用數(shù)組保存Excel表格的每一行數(shù)據(jù)。

for($i = 2; $i <= $highestRow; $i++){
    $item['number'] = $sheet->getCellByColumnAndRow(1, $i)->getValue();
    $item['name'] = $sheet->getCellByColumnAndRow(2, $i)->getValue();
    $item['sex'] = $sheet->getCellByColumnAndRow(3, $i)->getValue();
    $item['date'] = date('Y-m-d H:i:s',$sheet->getCellByColumnAndRow(4, $i)->getValue() - 25569)*86400;
    $data[] = $item;
}

3. 將Excel表格導(dǎo)入到數(shù)據(jù)庫(kù)中

最后,我們將Excel表格中的數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中。在這里,我們使用ThinkPHP6自帶的ORM操作,將數(shù)據(jù)保存到數(shù)據(jù)庫(kù)表中。

use appcommonmodelStudent;

Db::startTrans();
try {
    Student::insertAll($data);
    Db::commit();
} catch (Exception $e) {
    Db::rollback();
    return json(['code' => '500', 'msg' => '操作失敗']);
}
return json(['code' => '200', 'msg' => '操作成功']);

總結(jié)

本文詳細(xì)介紹了如何使用ThinkPHP6實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出功能。通過使用PhpOfficePhpSpreadsheet類庫(kù),我們可以輕松地完成Excel相關(guān)的操作。Excel導(dǎo)入導(dǎo)出功能在企業(yè)管理系統(tǒng)中用到非常廣泛,理解和掌握相關(guān)技能將會(huì)對(duì)開發(fā)者有很大的幫助。

請(qǐng)登錄后查看

CRMEB-慕白寒窗雪 最后編輯于2024-01-10 16:17:02

快捷回復(fù)
回復(fù)
回復(fù)
回復(fù)({{post_count}}) {{!is_user ? '我的回復(fù)' :'全部回復(fù)'}}
排序 默認(rèn)正序 回復(fù)倒序 點(diǎn)贊倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level || item.bbs_level }}

作者 管理員 企業(yè)

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
{{item.is_suggest == 1? '取消推薦': '推薦'}}
沙發(fā) 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暫無(wú)簡(jiǎn)介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
打賞
已打賞¥{{item.reward_price}}
{{item.like_count}}
{{item.showReply ? '取消回復(fù)' : '回復(fù)'}}
刪除
回復(fù)
回復(fù)

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回復(fù) {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
打賞
已打賞¥{{itemc.reward_price}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回復(fù)' : '回復(fù)'}}
刪除
回復(fù)
回復(fù)
查看更多
打賞
已打賞¥{{reward_price}}
4127
{{like_count}}
{{collect_count}}
添加回復(fù) ({{post_count}})

相關(guān)推薦

快速安全登錄

使用微信掃碼登錄
{{item.label}} 加精
{{item.label}} {{item.label}} 板塊推薦 常見問題 產(chǎn)品動(dòng)態(tài) 精選推薦 首頁(yè)頭條 首頁(yè)動(dòng)態(tài) 首頁(yè)推薦
取 消 確 定
回復(fù)
回復(fù)
問題:
問題自動(dòng)獲取的帖子內(nèi)容,不準(zhǔn)確時(shí)需要手動(dòng)修改. [獲取答案]
答案:
提交
bug 需求 取 消 確 定
打賞金額
當(dāng)前余額:¥{{rewardUserInfo.reward_price}}
{{item.price}}元
請(qǐng)輸入 0.1-{{reward_max_price}} 范圍內(nèi)的數(shù)值
打賞成功
¥{{price}}
完成 確認(rèn)打賞

微信登錄/注冊(cè)

切換手機(jī)號(hào)登錄

{{ bind_phone ? '綁定手機(jī)' : '手機(jī)登錄'}}

{{codeText}}
切換微信登錄/注冊(cè)
暫不綁定
CRMEB客服

CRMEB咨詢熱線 咨詢熱線

400-8888-794

微信掃碼咨詢

CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
返回頂部 返回頂部
CRMEB客服