2017-02-14 84 views
3

我想將xlsx文件中的數據導入到我的數據庫中,因爲我使用phpexcel庫將xls或xlsx文件轉換爲csv,然後讀取數據。 此代碼正常工作,將xls文件轉換爲本地主機和服務器上的csv,但xlsx到csv轉換隻在本地主機上完成。 這裏是我的代碼:將xlsx轉換爲csv無法在服務器上工作

function convertXLStoCSV($infile,$outfile) 
{ 

$fileType = PHPExcel_IOFactory::identify($infile); 

$objReader = PHPExcel_IOFactory::createReader($fileType); 

$objReader->setReadDataOnly(true); 
$objPHPExcel = $objReader->load($infile);  

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV'); 
$objWriter->save($outfile); 


} 

,並使用調用這個函數:

if($ext == 'xls' or $ext == 'xlsx') 
{ 

    $new_doc_name = time() . "." .$ext; 

    $target_path = "../../uploaded_files/"; 
    $target_path = $target_path . $new_doc_name ; 

     if ($_FILES["CSV_file"]["type"] == "application/xls" or $_FILES["CSV_file"]["type"] == "application/xlsx" or $_FILES["CSV_file"]["type"] == "application/vnd.ms-excel" or $_FILES["CSV_file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"){ 

     if(move_uploaded_file($_FILES['CSV_file']['tmp_name'], $target_path)) { 


     convertXLStoCSV($target_path,'output.csv'); 
     if(($handle = fopen("output.csv" , "r")) !== FALSE) 

請幫助

+0

因此,檢查服務器日誌;檢查服務器上的PHP版本是否與本地主機上運行的版本匹配,並且所有必需的擴展 –

回答

2

試試這個

$objReader = PHPExcel_IOFactory::createReader('Excel2007'); 
    $objPHPExcel = $objReader->load($uploadFIle); 

    $objWorksheet = $objPHPExcel->setActiveSheetIndex(0); 
    $highestRow = $objWorksheet->getHighestRow(); 
    $highestColumn = $objWorksheet->getHighestColumn(); 

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');  
$index = 0; 
    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) { 

     $objPHPExcel->setActiveSheetIndex($index); 

     // write out each worksheet to it's name with CSV extension 
     $outFile = str_replace(array("-"," "), "_", $worksheet->getTitle()) .".csv"; 

     $objWriter->setSheetIndex($index); 
     $objWriter->save($outFile); 

     $index++; 
    } 
+0

沒有它的不工作。也沒有錯誤。我的代碼在本地主機上正常工作,但不在服務器上工作。 –

相關問題