PHPExcel没有正确写入文件

时间:2014-08-01 15:39:52

标签: php phpexcel html-form

我的网站允许用户上传.xls.xlsx.csv个文件。如果文件不是.csv,则将其转换为1并保存到服务器。问题是我所拥有的代码并没有保存文件。相反,它只保存一个空白的单元格。我的代码出了什么问题,如何编辑它以完成我寻找的任务?

require_once('Classes/PHPExcel/IOFactory.php'); 
require_once("mcl_Oci.php");
$filename = $_FILES['uploaded']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);

    if ($_FILES['uploaded']['error'] !== UPLOAD_ERR_OK) {
        die("Upload failed with error code #" . $_FILES['uploaded']['error']);
    }

    if($ext == 'xls') {

        $inputFileType = 'Excel5';
        $inputFileName = $_FILES['uploaded']['tmp_name'];
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcelReader = $objReader->load($inputFileName);
        $loadedSheetNames = $objPHPExcelReader->getSheetNames();

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');

            $objWriter->setSheetIndex($sheetIndex);
            $objWriter->save('abc.csv'); //THIS IS WHERE IT ERRORS//


        $files = fopen('abc.csv', 'r'); 

            while (($line = fgetcsv($files)) !== FALSE) {
                $csv_array[] = array_combine(range(1, count($line)), array_values($line));
        }       
        echo 'Your .xls file was uploaded successfully. Have a nice day.';


    } elseif($ext == 'csv'){
        $inputFileType = 'CSV';
        $inputFileName = $_FILES['uploaded']['tmp_name'];
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcelReader = $objReader->load($inputFileName);
        $loadedSheetNames = $objPHPExcelReader->getSheetNames();
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');

            $objWriter->save('abc.csv');

        $files = fopen('abc.csv', 'r');
        while (($line = fgetcsv($files)) !== FALSE) {
                $csv_array[] = array_combine(range(1, count($line)), array_values($line));
        }   
        echo 'Your .csv file was uploaded successfully. Have a nice day.';

    } elseif($ext == 'xlsx') {
        $inputFileType = 'Excel2007';

        $inputFileName = $_FILES['uploaded']['tmp_name'];

        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcelReader = $objReader->load($inputFileName);

        $loadedSheetNames = $objPHPExcelReader->getSheetNames();

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');

        $objWriter->save(str_replace('.xlsx', '.csv', $inputFileName));

            $objWriter->save('abc.csv');

        $files = fopen('abc.csv', 'r');

        while (($line = fgetcsv($files)) !== FALSE) {
                $csv_array[] = array_combine(range(1, count($line)), array_values($line));
        }   
        echo 'Your .xlsx file was uploaded successfully. Have a nice day.';     
    }

    else {
        echo "This is not an accepted file type. Please save as either '*.csv' or '*.xls' and re-upload.";
    }       

更新

我的代码完全适用于.csv个文件。但是,它不适用于.xls.xlsx

2 个答案:

答案 0 :(得分:1)

if($ext='xls') {

正在进行分配,将所有文件设为xls。它应该是

if($ext == 'xls') {
        ^^---note the doubled "=" signs.

您在所有if()测试中都有完全相同的错误。

同样,您似乎无需费心检查文件上传是否成功,这是一件非常糟糕的事情。从不假设成功。总是假设失败,并将成功视为一个惊喜:

if ($_FILES['uploaded']['error'] !== UPLOAD_ERR_OK) {
   die("Upload failed with error code #" . $_FILES['uploaded']['error']);
}

答案 1 :(得分:0)

以下代码最终允许我上传单页.xlsx.xls.csv文件。上传后,如OP中所述,我将.xlsx.xls转换为.csv并将其保存到目录中。

    require_once('Classes/PHPExcel/IOFactory.php'); 
    require_once("mcl_Oci.php");
    $filename = $_FILES['uploaded']['name'];
    $ext = pathinfo($filename, PATHINFO_EXTENSION);

    if ($_FILES['uploaded']['error'] !== UPLOAD_ERR_OK) {
        die("Upload failed with error code #" . $_FILES['uploaded']['error']);
    }

    if($ext == 'xls') {

        $inputFileType = 'Excel5';
        $inputFileName = $_FILES['uploaded']['tmp_name'];
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcelReader = $objReader->load($inputFileName);
        $loadedSheetNames = $objPHPExcelReader->getSheetNames();

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');



        $objWriter->save(str_replace('.xls', '.csv', $inputFileName));

            $objWriter->save('abc.csv');


        $files = fopen('abc.csv', 'r'); 

            while (($line = fgetcsv($files)) !== FALSE) {
                $csv_array[] = array_combine(range(1, count($line)), array_values($line));
        }       
        echo 'Your .xls file was uploaded successfully. Have a nice day.';


    } elseif($ext == 'csv'){
        $inputFileType = 'CSV';
        $inputFileName = $_FILES['uploaded']['tmp_name'];
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcelReader = $objReader->load($inputFileName);
        $loadedSheetNames = $objPHPExcelReader->getSheetNames();
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');

            $objWriter->save('abc.csv');

        $files = fopen('abc.csv', 'r');
        while (($line = fgetcsv($files)) !== FALSE) {
                $csv_array[] = array_combine(range(1, count($line)), array_values($line));
        }   
        echo 'Your .csv file was uploaded successfully. Have a nice day.';

    } elseif($ext == 'xlsx') {
        $inputFileType = 'Excel2007';

        $inputFileName = $_FILES['uploaded']['tmp_name'];

        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcelReader = $objReader->load($inputFileName);

        $loadedSheetNames = $objPHPExcelReader->getSheetNames();

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');

        $objWriter->save(str_replace('.xlsx', '.csv', $inputFileName));

            $objWriter->save('abc.csv');

        $files = fopen('abc.csv', 'r');

        while (($line = fgetcsv($files)) !== FALSE) {
                $csv_array[] = array_combine(range(1, count($line)), array_values($line));
        }   
        echo 'Your .xlsx file was uploaded successfully. Have a nice day.';     
    }

    else {
        echo "This is not an accepted file type. Please save as either '*.csv' or '*.xls' and re-upload.";
    }