laravelµ¼ÈëexcelÖÐÎIJ»ÏÔʾ
ÔÚlaravelÖУ¬Ê¹ÓÃmaatwebsite/laravel-excelÀ´´¦ÀíexcelÎļþÊÇÒ»ÖÖºÜÊÇÀû±ãµÄÒªÁ졣Ȼ¶ø£¬ÓÐʱµ±µ¼ÈëµÄexcelÎļþ°üÀ¨ÖÐÎÄʱ£¬laravelÎÞ·¨×¼È·µØ½«ÖÐÎÄ×Ö·ûÏÔʾÔÚÊý¾Ý¿âÖС£±¾ÎĽ«Ì½ÌÖÕâ¸öÎÊÌ⣬²¢ÌṩһЩ½â¾ö¼Æ»®¡£
ÎÊÌâÐÎò
µ±Ê¹ÓÃMaatwebsite/Laravel-Excel½«±í¸ñµ¼ÈëLaravelÓ¦ÓóÌÐòʱ£¬ÓÐʱÖÐÎÄ×Ö·û»á·ºÆðÂÒÂë»òÏÔʾ¹ýʧµÄÇéÐΡ£Õâ¸öÎÊÌâͨ³£·ºÆðÔÚÒÔÏÂÇéÐÎÖУº
Êý¾Ý¿â±àÂ벻ƥÅ䣺ÈôÊÇÊý¾Ý¿âµÄ±àÂë·½·¨ÓëExcelÎļþÖеıàÂë·½·¨·×ÆçÖ£¬¾Í»áµ¼ÖÂÖÐÎÄ×Ö·û±»ÆÆËð¡£
ExcelÎļþ±àÂë¹ýʧ£ºÈôÊÇExcelÎļþÖеıàÂë·½·¨ÓëÏÖʵµÄ±àÂë·½·¨·×ÆçÖ£¬Ò²»áµ¼ÖÂÖÐÎÄ×Ö·û±»ÆÆËð¡£
½â¾ö¼Æ»®
¼ì²éÊý¾Ý¿â±àÂë
ÔÚLaravelÖУ¬Êý¾Ý¿â±àÂëÊÇÓëAppServiceProvider.phpÎļþÏà¹ØµÄ¡£Äã¿ÉÒÔÔÚ¸ÃÎļþÖÐÉèÖÃÊý¾Ý¿â±àÂë¡£ÈôÊÇÄãʹÓõÄÊÇMySQLÊý¾Ý¿â£¬¿ÉÒÔÔÚ¸ÃÎļþÖÐÉèÖãº
use IlluminateSupportServiceProvider;
use IlluminateSupportFacadesSchema;
class AppServiceProvider extends ServiceProvider
{
public function boot() { Schema::defaultStringLength(191); DB::statement('SET NAMES utf8mb4'); DB::statement('SET CHARACTER SET utf8mb4'); }
µÇ¼ºó¸´ÖÆ
}
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÒѽ«Êý¾Ý¿â±àÂëÉèÖÃΪutf8mb4£¬½«¸ÃÎļþÖеıàÂëÉèÖÃΪÓëExcelÎļþÏàͬ¼´¿É½â¾öÎÊÌâ¡£
¼ì²éExcelÎļþ±àÂë
ÈôÊÇÄãµÄExcelÎļþÖеıàÂë·½·¨ÓëÏÖʵµÄ±àÂë·½·¨·×ÆçÖ£¬ÄÇôµ¼ÈëµÄʱ¼ä¾Í»á·ºÆðÖÐÎÄÂÒÂëµÄÎÊÌâ¡£Òò´Ë£¬ÔÚµ¼ÈëExcelÎļþ֮ǰ£¬ÎÒÃÇÐèҪȷ±£ExcelÎļþµÄ±àÂë·½·¨ÊÇ׼ȷµÄ¡£Ò»ÖÖ¼òÆÓµÄÒªÁìÊÇ£º
ÔÚ·¿ªExcelÎļþ֮ǰ£¬ÏȽ«ÆäÁí´æΪTXTÎļþ£¬È»ºóÔÙµ¼ÈëTXTÎļþ£¬ÕâÑù¿ÉÒÔÈ·±£±àÂë׼ȷ¡£
ÀýÈ磺
$reader = MaatwebsiteExcelExcel::load(‘excel.xls’)->toCsv(‘excel.csv’);
$csvData = file_get_contents(‘excel.csv’);
$csvData = mb_convert_encoding($csvData, ‘UTF-8’, ‘UTF-8’);
$csvFile = fopen(‘excel.csv’, ‘w’);
fwrite($csvFile, $csvData);
fclose($csvFile);
$reader = MaatwebsiteExcelExcel::load(‘excel.csv’)->get();
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ«ExcelÎļþת»»ÎªCSVÎļþ£¬È»ºó½«Æäת»»ÎªUTF-8±àÂë¡£Çë×¢ÖØ£¬ÕâÖÖÒªÁì²¢²»ÊÇ×îºÃµÄÒªÁ죬ΪÁËÈ·±£Êý¾ÝµÄ׼ȷÐÔ£¬Äã¿ÉÄÜÐèÒª×ö¸ü¶àµÄµ÷½â¡£
½áÂÛ
´¦ÀíÖÐÎÄ×Ö·ûµÄÎÊÌâ²¢²»ÊÇÒ»¼þÈÝÒ×µÄÊÂÇé¡£¿ÉÊÇ£¬¾Óɱ¾ÎĵÄÌÖÂÛ£¬ÎÒÃÇÒѾÏàʶÁË»ù±¾µÄ½â¾ö¼Æ»®¡£ÈôÊÇÄãÈÔÈ»Óöµ½ÁËÖÐÎÄ×Ö·ûµ¼Èë¹ýʧµÄÎÊÌ⣬Çë¼á³Ö¼ì²éÄãµÄÊý¾Ý¿â±àÂëºÍExcelÎļþ±àÂ룬²¢¾ÙÐÐÐëÒªµÄµ÷½â¡£
ÒÔÉϾÍÊÇlaravelµ¼ÈëexcelÖÐÎIJ»ÏÔʾµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡