Yêu cầu thg 9 5, 2019 1:32 CH 3076 0 3
  • 3076 0 3
+1

Lỗi không đọc được tiếng Nhật khi import CSV khi dùng maatwebsite/excel trong Laravel

Chia sẻ
  • 3076 0 3

Chào cả nhà, e đang sử dụng maatwebsite/excel để làm chức năng import csv, mà k hiểu sao nó cột nào tiếng anh thì nó đọc ok
Còn tiếng Nhật thì nó đều trả về value của column là null như bên dưới.
Cả nhà ai gặp vấn đề này rồi cho em xin giải pháp với. E đang dùng version mới nhất https://docs.laravel-excel.com/3.1/imports/
Thanks cả nhà 😄

array:63 [1 => false
  2 => "Mitsuhiro"
  3 => "60:03"
  4 => 3.0
  5 => "DLMF-3917_01"
  6 => 1.0
  19 => "NULL"
  20 => null
  21 => null
  22 => null
  23 => null
  ....

======================================

Hum trước mình đã giải quyết xong vấn đề này.
Lý do ・do composer chưa clear hoàn toàn package cũ khi mình upgrade từ version 2.1 lên v_3.
Cách khắc phục là composer remove maatwebsite/excel, remove config/excel.php Sau đó cài lại v3 từ đầu. ・do input_encoding trong config/excel.php
Khắc phục: Sửa input_encoding từ UTF-8 đến Shift-JIS

Note là chỉ cần config encoding trong excel.php Không dùng thêm mb_convert_encoding($row, 'UTF-8', 'SJIS');
Vì khi dùngm trường hợp của mình nó đã bị lỗi sang 1 thứ format kì lạ khác.

3 CÂU TRẢ LỜI


Đã trả lời thg 9 5, 2019 2:35 CH
+2

Bạn thử text tiếng việt liệu có vấn đề không

Chia sẻ
Avatar morikun @thanhnguyen
thg 9 5, 2019 2:46 CH

Của mình nó đang gặp vấn đề với UTF-8 và SJIS đó Hưng. Việt Nhật đều bó tay.
Không rõ là có phải do version mới có vấn đề ko. Ở version 2.1 mình thấy ko bị sao.
Cơ mà muốn dùng version cao nó có nhiều cái hay ho hơn 😄

Avatar WheeBoo @quanghung97
thg 9 6, 2019 4:04 SA

@thanhnguyen có vẻ file config của bạn là phiên bản cũ không tương thích với 3.1 hay sao, rất có khả năng vậy https://github.com/Maatwebsite/Laravel-Excel/blob/3.1/config/excel.php

Avatar morikun @thanhnguyen
thg 9 6, 2019 6:35 SA

@quanghung97 Cái này mình cũng thử rồi Hưng ơi.
Mình đang dùng cách khác rồi 😄
Tại file csv lớn quá nên muốn dùng thằng này cho nhàn hạ mà ko đc 😄

Đã trả lời thg 9 6, 2019 2:21 SA
+2

Bạn thử convert nó xem

$data =  mb_convert_encoding($data, 'UTF-8', 'SJIS');
Chia sẻ
Avatar morikun @thanhnguyen
thg 9 6, 2019 2:41 SA

Mình cũng thử cách này rồi Trung ơi. Vấn đề nó bị từ lúc read line rồi. Nếu là bị lỗi font thì dùng mb_convert_encoding() sẽ có tác dụng. Ở đây mình dd($row) nó đã bị null hết rùi 😄 huhu

public function model(array $row)
    {
        dd($row);
thg 9 6, 2019 3:39 SA

@thanhnguyen dữ liệu từ request vẫn bình thường đúng không nhỉ. bạn thử sửa 'to_ascii' => false xem đc ko.

thg 9 6, 2019 3:46 SA

@thanhnguyen Bạn nên debug trong code nó xem có đọc được file ở các column đó không, check line_ending trong file và setting của bạn nữa. Mình nghĩ nó vẫn đọc được nhưng khả năng sai delimiter, enclosure hoặc bước mapping lỗi Nếu ko đc quay lại dùng phpexcel thôi, thằng này wrap lại nó nhưng phpexcel chưa thấy issue gì liên quan đến japanese như này.

Avatar morikun @thanhnguyen
thg 9 6, 2019 6:36 SA

@Plumpboy
Mình cũng đọc issue này và thử rồi mà ko đc 😄
Đành phải dùng cái khác thui 😄

Đã trả lời thg 9 5, 2019 1:57 CH
+1

Bạn có thể show cái config của nó không ?? config/excel.php

Chia sẻ
Avatar morikun @thanhnguyen
thg 9 5, 2019 2:15 CH

Đây bạn. Mình cũng đọc 1 số issue trên github,

  • Sửa use_bom
  • Sửa encoding
  • Sửa 1 vài thứ khác mà có vẻ chưa đúng chỗ ngứa nên chưa ổn lắm. config/excel.php
<?php

return array(

    'cache'      => [

        /*
        |--------------------------------------------------------------------------
        | Enable/Disable cell caching
        |--------------------------------------------------------------------------
        */
        'enable'   => true,

        /*
        |--------------------------------------------------------------------------
        | Caching driver
        |--------------------------------------------------------------------------
        |
        | Set the caching driver
        |
        | Available methods:
        | memory|gzip|serialized|igbinary|discISAM|apc|memcache|temp|wincache|sqlite|sqlite3
        |
        */
        'driver'   => 'memory',

        /*
        |--------------------------------------------------------------------------
        | Cache settings
        |--------------------------------------------------------------------------
        */
        'settings' => [

            'memoryCacheSize' => '32MB',
            'cacheTime'       => 600

        ],

        /*
        |--------------------------------------------------------------------------
        | Memcache settings
        |--------------------------------------------------------------------------
        */
        'memcache' => [

            'host' => 'localhost',
            'port' => 11211,

        ],

        /*
        |--------------------------------------------------------------------------
        | Cache dir (for discISAM)
        |--------------------------------------------------------------------------
        */

        'dir'      => storage_path('cache')
    ],

    'properties' => [
        'creator'        => 'Maatwebsite',
        'lastModifiedBy' => 'Maatwebsite',
        'title'          => 'Spreadsheet',
        'description'    => 'Default spreadsheet export',
        'subject'        => 'Spreadsheet export',
        'keywords'       => 'maatwebsite, excel, export',
        'category'       => 'Excel',
        'manager'        => 'Maatwebsite',
        'company'        => 'Maatwebsite',
    ],

    /*
    |--------------------------------------------------------------------------
    | Sheets settings
    |--------------------------------------------------------------------------
    */
    'sheets'     => [

        /*
        |--------------------------------------------------------------------------
        | Default page setup
        |--------------------------------------------------------------------------
        */
        'pageSetup' => [
            'orientation'           => 'portrait',
            'paperSize'             => '9',
            'scale'                 => '100',
            'fitToPage'             => false,
            'fitToHeight'           => true,
            'fitToWidth'            => true,
            'columnsToRepeatAtLeft' => ['', ''],
            'rowsToRepeatAtTop'     => [0, 0],
            'horizontalCentered'    => false,
            'verticalCentered'      => false,
            'printArea'             => null,
            'firstPageNumber'       => null,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Creator
    |--------------------------------------------------------------------------
    |
    | The default creator of a new Excel file
    |
    */

    'creator'    => 'Maatwebsite',

    'csv'        => [
        /*
       |--------------------------------------------------------------------------
       | Delimiter
       |--------------------------------------------------------------------------
       |
       | The default delimiter which will be used to read out a CSV file
       |
       */

        'delimiter'   => ',',

        /*
        |--------------------------------------------------------------------------
        | Enclosure
        |--------------------------------------------------------------------------
        */

        'enclosure'   => '"',

        /*
        |--------------------------------------------------------------------------
        | Line endings
        |--------------------------------------------------------------------------
        */

        'line_ending' => "\r\n",

        /*
        |--------------------------------------------------------------------------
        | setUseBom
        |--------------------------------------------------------------------------
        */

        'use_bom' => true
    ],

    'export'     => [

        /*
        |--------------------------------------------------------------------------
        | Autosize columns
        |--------------------------------------------------------------------------
        |
        | Disable/enable column autosize or set the autosizing for
        | an array of columns ( array('A', 'B') )
        |
        */
        'autosize'                    => true,

        /*
        |--------------------------------------------------------------------------
        | Autosize method
        |--------------------------------------------------------------------------
        |
        | --> PHPExcel_Shared_Font::AUTOSIZE_METHOD_APPROX
        | The default is based on an estimate, which does its calculation based
        | on the number of characters in the cell value (applying any calculation
        | and format mask, and allowing for wordwrap and rotation) and with an
        | "arbitrary" adjustment based on the font (Arial, Calibri or Verdana,
        | defaulting to Calibri if any other font is used) and a proportional
        | adjustment for the font size.
        |
        | --> PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT
        | The second method is more accurate, based on actual style formatting as
        | well (bold, italic, etc), and is calculated by generating a gd2 imagettf
        | bounding box and using its dimensions to determine the size; but this
        | method is significantly slower, and its accuracy is still dependent on
        | having the appropriate fonts installed.
        |
        */
        'autosize-method'             => PHPExcel_Shared_Font::AUTOSIZE_METHOD_APPROX,

        /*
        |--------------------------------------------------------------------------
        | Auto generate table heading
        |--------------------------------------------------------------------------
        |
        | If set to true, the array indices (or model attribute names)
        | will automatically be used as first row (table heading)
        |
        */
        'generate_heading_by_indices' => true,

        /*
        |--------------------------------------------------------------------------
        | Auto set alignment on merged cells
        |--------------------------------------------------------------------------
        */
        'merged_cell_alignment'       => 'left',

        /*
        |--------------------------------------------------------------------------
        | Pre-calculate formulas during export
        |--------------------------------------------------------------------------
        */
        'calculate'                   => false,

        /*
        |--------------------------------------------------------------------------
        | Include Charts during export
        |--------------------------------------------------------------------------
        */
        'includeCharts'               => false,

        /*
        |--------------------------------------------------------------------------
        | Default sheet settings
        |--------------------------------------------------------------------------
        */
        'sheets'                      => [

            /*
            |--------------------------------------------------------------------------
            | Default page margin
            |--------------------------------------------------------------------------
            |
            | 1) When set to false, default margins will be used
            | 2) It's possible to enter a single margin which will
            |    be used for all margins.
            | 3) Alternatively you can pass an array with 4 margins
            |    Default order: array(top, right, bottom, left)
            |
            */
            'page_margin'          => false,

            /*
            |--------------------------------------------------------------------------
            | Value in source array that stands for blank cell
            |--------------------------------------------------------------------------
            */
            'nullValue'            => null,

            /*
            |--------------------------------------------------------------------------
            | Insert array starting from this cell address as the top left coordinate
            |--------------------------------------------------------------------------
            */
            'startCell'            => 'A1',

            /*
            |--------------------------------------------------------------------------
            | Apply strict comparison when testing for null values in the array
            |--------------------------------------------------------------------------
            */
            'strictNullComparison' => false
        ],

        /*
        |--------------------------------------------------------------------------
        | Store settings
        |--------------------------------------------------------------------------
        */

        'store'                       => [

            /*
            |--------------------------------------------------------------------------
            | Path
            |--------------------------------------------------------------------------
            |
            | The path we want to save excel file to
            |
            */
            'path'       => storage_path('exports'),

            /*
            |--------------------------------------------------------------------------
            | Return info
            |--------------------------------------------------------------------------
            |
            | Whether we want to return information about the stored file or not
            |
            */
            'returnInfo' => false

        ],

        /*
        |--------------------------------------------------------------------------
        | PDF Settings
        |--------------------------------------------------------------------------
        */
        'pdf'                         => [

            /*
            |--------------------------------------------------------------------------
            | PDF Drivers
            |--------------------------------------------------------------------------
            | Supported: DomPDF, tcPDF, mPDF
            */
            'driver'  => 'DomPDF',

            /*
            |--------------------------------------------------------------------------
            | PDF Driver settings
            |--------------------------------------------------------------------------
            */
            'drivers' => [

                /*
                |--------------------------------------------------------------------------
                | DomPDF settings
                |--------------------------------------------------------------------------
                */
                'DomPDF' => [
                    'path' => base_path('vendor/dompdf/dompdf/')
                ],

                /*
                |--------------------------------------------------------------------------
                | tcPDF settings
                |--------------------------------------------------------------------------
                */
                'tcPDF'  => [
                    'path' => base_path('vendor/tecnick.com/tcpdf/')
                ],

                /*
                |--------------------------------------------------------------------------
                | mPDF settings
                |--------------------------------------------------------------------------
                */
                'mPDF'   => [
                    'path' => base_path('vendor/mpdf/mpdf/')
                ],
            ]
        ]
    ],

    'filters'    => [
        /*
        |--------------------------------------------------------------------------
        | Register read filters
        |--------------------------------------------------------------------------
        */

        'registered' => [
            'chunk' => 'Maatwebsite\Excel\Filters\ChunkReadFilter'
        ],

        /*
        |--------------------------------------------------------------------------
        | Enable certain filters for every file read
        |--------------------------------------------------------------------------
        */

        'enabled'    => []
    ],

    'import'     => [

        /*
        |--------------------------------------------------------------------------
        | Has heading
        |--------------------------------------------------------------------------
        |
        | The sheet has a heading (first) row which we can use as attribute names
        |
        | Options: true|false|slugged|slugged_with_count|ascii|numeric|hashed|trans|original
        |
        */

        'heading'                 => 'original',

        /*
        |--------------------------------------------------------------------------
        | First Row with data or heading of data
        |--------------------------------------------------------------------------
        |
        | If the heading row is not the first row, or the data doesn't start
        | on the first row, here you can change the start row.
        |
        */

        'startRow'                => 1,

        /*
        |--------------------------------------------------------------------------
        | Cell name word separator
        |--------------------------------------------------------------------------
        |
        | The default separator which is used for the cell names
        | Note: only applies to 'heading' settings 'true' && 'slugged'
        |
        */

        'separator'               => '_',

        /*
        |--------------------------------------------------------------------------
        | Slug whitelisting
        |--------------------------------------------------------------------------
        |
        | Here you can whitelist certain characters in the slug.
        | E.g. user.last_name will not remove . and _
        | Note: only applies to 'heading' settings 'true' && 'slugged'
        |
        */

        'slug_whitelist'       => '._',

        /*
        |--------------------------------------------------------------------------
        | Include Charts during import
        |--------------------------------------------------------------------------
        */

        'includeCharts'           => false,

        /*
        |--------------------------------------------------------------------------
        | Sheet heading conversion
        |--------------------------------------------------------------------------
        |
        | Convert headings to ASCII
        | Note: only applies to 'heading' settings 'true' && 'slugged'
        |
        */

        'to_ascii'                => true,

        /*
        |--------------------------------------------------------------------------
        | Import encoding
        |--------------------------------------------------------------------------
        */

        'encoding'                => [

            'input'  => 'UTF-8',
            'output' => 'UTF-8'

        ],

        /*
        |--------------------------------------------------------------------------
        | Calculate
        |--------------------------------------------------------------------------
        |
        | By default cells with formulas will be calculated.
        |
        */

        'calculate'               => true,

        /*
        |--------------------------------------------------------------------------
        | Ignore empty cells
        |--------------------------------------------------------------------------
        |
        | By default empty cells are not ignored
        |
        */

        'ignoreEmpty'             => false,

        /*
        |--------------------------------------------------------------------------
        | Force sheet collection
        |--------------------------------------------------------------------------
        |
        | For a sheet collection even when there is only 1 sheets.
        | When set to false and only 1 sheet found, the parsed file will return
        | a row collection instead of a sheet collection.
        | When set to true, it will return a sheet collection instead.
        |
        */
        'force_sheets_collection' => false,

        /*
        |--------------------------------------------------------------------------
        | Date format
        |--------------------------------------------------------------------------
        |
        | The format dates will be parsed to
        |
        */

        'dates'                   => [

            /*
            |--------------------------------------------------------------------------
            | Enable/disable date formatting
            |--------------------------------------------------------------------------
            */
            'enabled' => true,

            /*
            |--------------------------------------------------------------------------
            | Default date format
            |--------------------------------------------------------------------------
            |
            | If set to false, a carbon object will return
            |
            */
            'format'  => false,

            /*
            |--------------------------------------------------------------------------
            | Date columns
            |--------------------------------------------------------------------------
            */
            'columns' => []
        ],

        /*
        |--------------------------------------------------------------------------
        | Import sheets by config
        |--------------------------------------------------------------------------
        */
        'sheets'                  => [

            /*
            |--------------------------------------------------------------------------
            | Example sheet
            |--------------------------------------------------------------------------
            |
            | Example sheet "test" will grab the firstname at cell A2
            |
            */

            'test' => [

                'firstname' => 'A2'

            ]

        ]
    ],

    'views'      => [

        /*
        |--------------------------------------------------------------------------
        | Styles
        |--------------------------------------------------------------------------
        |
        | The default styles which will be used when parsing a view
        |
        */

        'styles' => [

            /*
            |--------------------------------------------------------------------------
            | Table headings
            |--------------------------------------------------------------------------
            */
            'th'     => [
                'font' => [
                    'bold' => true,
                    'size' => 12,
                ]
            ],

            /*
            |--------------------------------------------------------------------------
            | Strong tags
            |--------------------------------------------------------------------------
            */
            'strong' => [
                'font' => [
                    'bold' => true,
                    'size' => 12,
                ]
            ],

            /*
            |--------------------------------------------------------------------------
            | Bold tags
            |--------------------------------------------------------------------------
            */
            'b'      => [
                'font' => [
                    'bold' => true,
                    'size' => 12,
                ]
            ],

            /*
            |--------------------------------------------------------------------------
            | Italic tags
            |--------------------------------------------------------------------------
            */
            'i'      => [
                'font' => [
                    'italic' => true,
                    'size'   => 12,
                ]
            ],

            /*
            |--------------------------------------------------------------------------
            | Heading 1
            |--------------------------------------------------------------------------
            */
            'h1'     => [
                'font' => [
                    'bold' => true,
                    'size' => 24,
                ]
            ],

            /*
            |--------------------------------------------------------------------------
            | Heading 2
            |--------------------------------------------------------------------------
            */
            'h2'     => [
                'font' => [
                    'bold' => true,
                    'size' => 18,
                ]
            ],

            /*
            |--------------------------------------------------------------------------
            | Heading 2
            |--------------------------------------------------------------------------
            */
            'h3'     => [
                'font' => [
                    'bold' => true,
                    'size' => 13.5,
                ]
            ],

            /*
             |--------------------------------------------------------------------------
             | Heading 4
             |--------------------------------------------------------------------------
             */
            'h4'     => [
                'font' => [
                    'bold' => true,
                    'size' => 12,
                ]
            ],

            /*
             |--------------------------------------------------------------------------
             | Heading 5
             |--------------------------------------------------------------------------
             */
            'h5'     => [
                'font' => [
                    'bold' => true,
                    'size' => 10,
                ]
            ],

            /*
             |--------------------------------------------------------------------------
             | Heading 6
             |--------------------------------------------------------------------------
             */
            'h6'     => [
                'font' => [
                    'bold' => true,
                    'size' => 7.5,
                ]
            ],

            /*
             |--------------------------------------------------------------------------
             | Hyperlinks
             |--------------------------------------------------------------------------
             */
            'a'      => [
                'font' => [
                    'underline' => true,
                    'color'     => ['argb' => 'FF0000FF'],
                ]
            ],

            /*
             |--------------------------------------------------------------------------
             | Horizontal rules
             |--------------------------------------------------------------------------
             */
            'hr'     => [
                'borders' => [
                    'bottom' => [
                        'style' => 'thin',
                        'color' => ['FF000000']
                    ],
                ]
            ]
        ]

    ]

);

thg 9 5, 2019 2:21 CH

@thanhnguyen phần đọc file csv bạn đang viết như nào nhỉ ??

Avatar morikun @thanhnguyen
thg 9 5, 2019 2:28 CH

@HuyDQ
Bên controller

(new AlbumImport)->import($request->file('import_file'), null, \Maatwebsite\Excel\Excel::CSV);

Còn đây là import model

<?php

namespace App\Imports;

use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Illuminate\Contracts\Queue\ShouldQueue;

use App\Models\TmpAlbum;

// We can use WithHeadingRow if all of column has unique name
// use Maatwebsite\Excel\Concerns\WithHeadingRow;

class AlbumImport implements ToModel, WithBatchInserts,WithChunkReading, ShouldQueue,WithStartRow
{
    use Importable;

    public function model(array $row)
    {
        // echo "<pre>";
        // $i = 0;
        // // echo ++$i . ". " . $row[0];
        // print_r($row);die;
        $album = [
            'id' => $row[0],
            'title' => $row[2],
            'text' => $row[3],
            'image_url' => "http://xxx",
        ];
        dd($row);
        if ($row[0]) {
	        return new TmpAlbum($album);
        }
    }

    public function startRow(): int
    {
        return 2;
    }

    public function batchSize(): int
    {
        return 1000;
    }

    public function chunkSize(): int
    {
        return 1000;
    }
    
}
thg 9 5, 2019 2:41 CH

@thanhnguyen mình tìm thấy 1 issue có vẻ khá giống case của bạn. Bạn thử sửa cái chỗ:

'to_ascii' => false

Link Issue: https://github.com/Maatwebsite/Laravel-Excel/issues/1171

Avatar morikun @thanhnguyen
thg 9 5, 2019 2:42 CH

@HuyDQ Thanks Huy, cái này mình thử trước khi tạo question rùi 😄, mà ko đc. Ahuhu

Avatar Phan Cường @Phancuong997
thg 12 10, 2019 1:22 SA

@thanhnguyen bạn khắc phục được chưa ạ.mình cũng như bạn nhưng là output ra excel =(. shift jis thì bị =false() hết

Avatar morikun @thanhnguyen
thg 12 10, 2019 1:33 SA

@Phancuong997 Mình khắc phục được rồi bạn. Mình cũng đã mô tả vấn đề thực sự và cách mình giải quyết ở dưới câu hỏi rồi đó bạn.
Nếu bạn áp dụng cách đó vẫn không được thì thử new 1 project mới toanh, rồi dùng chính code bạn đang sử dụng để export thử nhé. Đôi khi vấn đề k nằm ở cái bạn đang làm, mà nó bị ảnh hưởng bởi những cái cũ 😄

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí