【问题标题】:Convert (xls, xlsx) to CSV before Upload using PHP在使用 PHP 上传之前将 (xls, xlsx) 转换为 CSV
【发布时间】:2015-08-07 07:29:39
【问题描述】:

当用户单击更新按钮时,我正在尝试将 Excel 文件转换为 CSV。

当我在文件夹中上传一个 excel 文件时它的工作,然后我使用 PHPExcel 获取该文件并读取所有数据并将其转换为 CSV。

这里是代码。

<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <?php
    ini_set("display_errors", "1");
    ini_set("memory_limit", "-1");
    ini_set("max_execution_time", "-1");
    error_reporting(1);

    require_once "PHPExcel.php";
    $dir = "../excel2csv/";                 // Main Directory Name 
    $file_arr = array();
    $file_ext_arr = array('xls','xlsx');            // Valid Extensions of Excel File

    // From Directory get only Excel Files in Array
    if(is_dir($dir))
    {
        if($dh = opendir($dir))
        {
            while(($file = readdir($dh)) !== false)
            {
                $info = new SplFileInfo($file);
                $ext = $info->getExtension();           // Get Extension of Current File
                if(in_array($ext,$file_ext_arr))
                {
                    array_push($file_arr, $file);
                }
            }
            closedir($dh);
        }
    }

    // Make CSV File 
    $fp = fopen('file.csv', 'a');
    $list = array();

    foreach($file_arr as $val)
    {
        $arr_data = array();
        $objPHPExcel = PHPExcel_IOFactory::load($dir . $val);
        $cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
        foreach($cell_collection as $cell)
        {
            $column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
            $row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
            $data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
            //header will / should be in row 1 only. of course this can be modified to suit your need.
            // Skip Rows From Top if you have header in Excel then Change 0 to 1
            if($row == 0)
            {
                $header[$row][$column] = $data_value;
            }
            else
            {
                $arr_data[$row]['row'] = $row;
                $arr_data[$row][$column] = $data_value;
            }
        }
        $data = $arr_data;
        foreach($data as $val1)
        {
            $num_col = sizeof($val1) - 1;  // get number of columns in Excel 
            break;
        }
        $lwrcol=array();    
        foreach($data as $val2)
        {
            $alphaArr = range('A','Z');
            $colArr = range('A',$alphaArr[$num_col - 1]);

            foreach($colArr as $col)
            {
                $lwrcol[$col] = isset($val2[$col]) ? utf8_decode($val2[$col]) : "";
                fwrite($fp,$lwrcol[$col].",");
            }
            fwrite($fp,"\n");   
        }
        chmod(getcwd()."/file.csv", 0777);
    }
    fclose($fp);
    ?>
</html>

在上面的代码中,我首先从文件夹中找到所有 excel 文件并制作一个 file.csv 文件。

我想要做的是当用户在&lt;input type="file" name="upload"/&gt; 中选择任何文件并单击上传按钮,然后在后端第一个过程是将 Excel 文件转换为 CSV 之前它移动到上传文件夹。

【问题讨论】:

  • 所以我可以看到您的代码正在将Excel 转换为CSV,但您还没有编写代码先从input 上传。对吗?
  • 所有这些代码都在&lt;form action="file.php" method="post"&gt;

标签: php csv phpexcel


【解决方案1】:

我可以看到您的代码正在用于从CSV 保存Excel 的数据。但首先你想先从FORM 上传然后你想转换。所以你可以做的是:

首先你需要Form 的 HTML,如果有 POST 文件,则首先上传文件。看看下面的sn-p:

<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <?php
    ini_set("display_errors", "1");
    ini_set("memory_limit", "-1");
    ini_set("max_execution_time", "-1");
    error_reporting(1);


    require_once "PHPExcel.php";
    $dir = "../excel2csv/";                 // Main Directory Name 
    $file_arr = array();
    $file_ext_arr = array('xls','xlsx');            // Valid Extensions of Excel File



    if(!empty($_FILES)) {

        //uploading file first
        $info = pathinfo($_FILES['userFile']['name']);
        $ext = $info['extension']; // get the extension of the file
        $newname = "excelfile.".$ext; 

        $target = $dir.$newname;
        move_uploaded_file( $_FILES['userFile']['tmp_name'], $target);

        //upload finish  wiring csv file now
        writecsv();

    }

    function wirtecsv(){
        // From Directory get only Excel Files in Array
        if(is_dir($dir))
        {
            if($dh = opendir($dir))
            {
                while(($file = readdir($dh)) !== false)
                {
                    $info = new SplFileInfo($file);
                    $ext = $info->getExtension();           // Get Extension of Current File
                    if(in_array($ext,$file_ext_arr))
                    {
                        array_push($file_arr, $file);
                    }
                }
                closedir($dh);
            }
        }

        // Make CSV File 
        $fp = fopen('file.csv', 'a');
        $list = array();

        foreach($file_arr as $val)
        {
            $arr_data = array();
            $objPHPExcel = PHPExcel_IOFactory::load($dir . $val);
            $cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
            foreach($cell_collection as $cell)
            {
                $column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
                $row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
                $data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
                //header will / should be in row 1 only. of course this can be modified to suit your need.
                // Skip Rows From Top if you have header in Excel then Change 0 to 1
                if($row == 0)
                {
                    $header[$row][$column] = $data_value;
                }
                else
                {
                    $arr_data[$row]['row'] = $row;
                    $arr_data[$row][$column] = $data_value;
                }
            }
            $data = $arr_data;
            foreach($data as $val1)
            {
                $num_col = sizeof($val1) - 1;  // get number of columns in Excel 
                break;
            }
            $lwrcol=array();    
            foreach($data as $val2)
            {
                $alphaArr = range('A','Z');
                $colArr = range('A',$alphaArr[$num_col - 1]);

                foreach($colArr as $col)
                {
                    $lwrcol[$col] = isset($val2[$col]) ? utf8_decode($val2[$col]) : "";
                    fwrite($fp,$lwrcol[$col].",");
                }
                fwrite($fp,"\n");   
            }
            chmod(getcwd()."/file.csv", 0777);
        }
        fclose($fp);
    }
    ?>


    <form action='' method='POST' enctype='multipart/form-data'>
    <input type='file' name='userFile'><br>
    <input type='submit' name='upload_btn' value='upload'>
    </form>
</html>

【讨论】:

  • 我以前不想在任何文件夹中移动文件。我希望 Excel 文件在上传到服务器之前转换为 CSV。当用户选择任何 excel 并单击 UPLOAD 按钮时,第一个过程是在 CSV 文件中转换 Excel 文件,转换后该文件上传到 UPLOAD 文件夹以在另一个过程中使用。
  • 显示空白页
  • @SriP 我现在不知道空页的实际原因。为此,您必须逐步调试代码。
猜你喜欢
  • 1970-01-01
  • 2012-09-06
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-25
相关资源
最近更新 更多