laravel processing excel file using third-party packages maatwebsite / excel in

Excel file has been processed using python, php used in the project maatwebsite / excel excel files to process, found to be particularly strong, recording use.
Because the new version 3.1 revision large, instead using 2.1, the latter will be adjusted as needed

1. Install a third-party package needs some other packages, requirements are as follows:
PHP version >= 5.3.7
Laravel >= 4.1
PHPOffice PHPExcel >= 1.8.0 (included by composer.json)
PHP extension php_zip enabled (required if you need PHPExcel to handle .xlsx .ods or .gnumeric files)
PHP extension php_xml enabled
PHP extension php_gd2 enabled (optional, but required for exact column width autocalculation)
2. Install third-party packages
composer require "maatwebsite/excel": "~2.1.0"
3. Fill the facade to the configuration file

app/config/app.config

config/app.php
//Package Service Providers...
'providers' => [
    Maatwebsite\Excel\ExcelServiceProvider::class,
]
'aliases' => [
    'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
4. Publish Profiles
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
5. load the file
Excel::load('file.xls', function($reader) {

    // reader methods

});

Note that the file may be stored in a file, or a file transfer by the client through the client file delivery client transfer complete logic and a basic example of the detection of the file is given below:

function checkFileUploadTrue(){
    // UPLOAD_ERR_OK => '文件上传成功。'
            // 下面的错误对应的整数位1-》1
    $error = [
        UPLOAD_ERR_INI_SIZE => '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。',//1
        UPLOAD_ERR_FORM_SIZE => '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。',//2
        UPLOAD_ERR_PARTIAL => '文件只有部分被上传。',//3
        UPLOAD_ERR_NO_FILE => '没有文件被上传。',//4
        UPLOAD_ERR_NO_TMP_DIR => '找不到临时文件夹。',//6
        UPLOAD_ERR_CANT_WRITE => '文件写入失败。'//7
    ];
    // 检测error是否为0,其他的任务不成功
    if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
        return ['flag'=> False,'msg' => $error[$_FILES['file']['error']]];
    }
    // 检测是否为上传文件
    if (!is_uploaded_file($_FILES["file"]["tmp_name"])) {
       return ['flag' => False, 'msg' => '不是上传的文件!'];
    }
    // 检测是否为约定的文件类型
    $file_type = ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.ms-excel'];
    if (!in_array($_FILES['file']['type'],$file_type)) {
        return ['flag' => False, 'msg' => '只能上传excel类型文件'];
    }
    // 目录
    $storage_file_dir = '/data/file/tmp/';
    //转存文件
    $tmp_filename = $_FILES['file']['tmp_name'];
    $dest_filename = $storage_file_dir.$_FILES['file']['name'];
    if(!move_uploaded_file($tmp_filename,$dest_filename)){
        return ['flag' => False, 'msg' => '文件转存失败,确认之后再转存!'];
    }
    return ['flag'=> True,'msg' => ''];
}
6. read the contents of a file, you can use two ways
Excel::load('local_store.xls', function($reader) {

})->get();

//或者

Excel::load('local_store.xls', function($reader) {

    // 获取所有的结果集
    $results = $reader->get();

    //或者使用all
    //$results = $reader->all();

});

Guess you like

Origin www.cnblogs.com/linnenn/p/11290534.html