javck
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: laravel,套件 --- # Laravel Excel處理解決方案 [官方網站](https://docs.laravel-excel.com/3.1/getting-started/) ## 安裝套件 開啟Terminal,輸入以下指令 `composer require maatwebsite/excel` ## 匯入 ### 5分鐘簡易匯入Excel #### Step 1.生成Import類別 可透過以下指令來生成此類別,生成的類別位在app/Imports資料夾內 `php artisan make:import UsersImport --model=User` . ├── app │ ├── Imports │ │ ├── UsersImport.php │ └── composer.json 或者你可以自己撰寫,格式如下: ```php <?php namespace App\Imports; use App\User; use Illuminate\Support\Facades\Hash; use Maatwebsite\Excel\Concerns\ToModel; class UsersImport implements ToModel { /** * @param array $row * * @return User|null */ public function model(array $row) { //可設定如果沒有某欄位,該行就不要匯入 if(!isset($row[0])){ return null; } return new User([ //設定欄位對應,第一欄對應name欄位,第二欄對應email欄位 'name' => $row[0], 'email' => $row[1], 'password' => Hash::make($row[2]), ]); } } ``` #### Step 2.在控制器使用Import類別來進行載入 ```php use App\Imports\UsersImport; use Maatwebsite\Excel\Facades\Excel; use App\Http\Controllers\Controller; class UsersController extends Controller { public function import() { Excel::import(new UsersImport, 'users.xlsx'); return redirect('/')->with('success', 'All good!'); } } ``` ### 如果有多個Sheet,只需要匯入某個Sheet... Step 1.在Import類別導入類別並實作 ```php <?php namespace App\Imports; ... use Maatwebsite\Excel\Concerns\WithMultipleSheets; ... class UserImport implements ToModel,WithMultipleSheets ``` Step 2.在Import類別加入以下函式 下列程式表示只匯入第一個sheet ```php public function sheets(): array { return [ 0 => $this, ]; } ``` ### 從Sheet的第幾行開始讀 Step 1.在Import類別導入類別並實作 ```php <?php namespace App\Imports; ... use Maatwebsite\Excel\Concerns\WithStartRow; ... class UserImport implements ToModel,WithStartRow ``` Step 2.在Import類別加入以下程式碼 ```php public function startRow(): int { return 4; //從第4行開始讀 } ``` ### 匯入時進行欄位驗證 Step 1.在Import類別導入類別並實作 ```php <?php namespace App\Imports; ... use Maatwebsite\Excel\Concerns\WithValidation; ... class UserImport implements ToModel,WithValidation ``` Step 2.在Import類別加入以下程式碼 ```php //匯入時的驗證規則 public function rules(): array { return [ '0' => 'required|string', '1' => 'required|email', ]; } ``` ```php //驗證訊息使用的欄位名稱 public function customValidationAttributes() { return [ '0' => '姓名', '1' => 'Email', ]; } ``` ## 匯出 ### 5分鐘簡易匯出Excel #### Step 1. 建立輸出類別 你能夠利用以下指令建立一個位於 app/Exports 資料夾內的輸出類別 `php artisan make:export UsersExport --model=User` 輸出內容如下: ```php= <?php namespace App\Exports; use App\User; use Maatwebsite\Excel\Concerns\FromCollection; class UsersExport implements FromCollection { public function collection() { return User::all(); } } ``` #### Step 2.撰寫輸出方法 在控制器裡頭建立一個輸出方法 ```php= <?php namespace App\Http\Controllers; use App\Exports\UsersExport; use Maatwebsite\Excel\Facades\Excel; class UsersController extends Controller { public function export() { return Excel::download(new UsersExport, 'users.xlsx'); } } ``` #### Step 3.建立一個用來輸出Excel的路由規則 `Route::get('users/export/', 'UsersController@export');` ### 為Excel檔案加上標頭 #### Step 1.導入名為 WithHeadings 的 Trait ```php ... use Maatwebsite\Excel\Concerns\WithHeadings; class ServiceExport implements FromCollection, WithHeadings { ... } ``` #### Step 2.在輸出類別內加入 headings() 陣列內的字串將依序作為Excel的標頭 ```php public function headings(): array { return [ 'Code', 'Description', 'Pos', 'Mod A', 'Mod B', 'Charge', ]; } ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully