/**标签:1024,Convert,Log,excel,file,debug,csv,xls From: https://www.cnblogs.com/haoxuanchen2014/p/11165606.html
Convert excel file to csv
*/
public function excel_to_csv()
{
ini_set('max_execution_time','0');
ini_set('memory_limit', '1G');
$xls_file = storage_path('excel_file') . '\\' . '1w.xls';
$file_info = pathinfo($xls_file);
Log::debug('文件路径:' . $xls_file);
if (!file_exists($xls_file)) {
Log::debug('文件不存在!');
}
$file_size = (filesize($xls_file) / 1024 / 1024) . "MB";
if ($file_size) {
Log::debug('Excel file size: '.$file_size);
}
// valide the file type
if (strtolower($file_info['extension']) == 'xls') {
$reader = new Xls();
}else{
$reader = new Xlsx();
}
$memonry = '【开始执行内存:'.(memory_get_usage()/1024/1024)."MB, ";
$memonry .= '峰值内存:'.(memory_get_peak_usage()/1024/1024)."MB】\r\n";
Log::debug($memonry);
$spreadsheet = $reader->load($xls_file);
$loadedSheetNames = $spreadsheet->getSheetNames();
$writer = new Csv($spreadsheet);
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
Log::debug($sheetIndex);
$writer->setSheetIndex($sheetIndex);
$csv_path = storage_path('new_csv') . '\\' . $file_info['filename'] . '_' . $loadedSheetName . '.csv';
// $writer->save($loadedSheetName.'.csv');
$writer->save($csv_path);
}
$memonry = '【结束执行内存:'.(memory_get_usage()/1024/1024)."MB, ";
$memonry .= '峰值内存:'.(memory_get_peak_usage()/1024/1024)."MB】\r\n";
Log::debug($memonry);
Log::debug('All End!');
}