【问题标题】:Export simple excel data into MySQL using PHP使用 PHP 将简单的 Excel 数据导出到 MySQL
【发布时间】:2012-10-25 06:08:18
【问题描述】:

我的客户得到了一个结构如下的 excel 文件

name     |     email
----------------------------
Name     |   email here
Name     |   email here
Name     |   email here
Name     |   email here
Name     |   email here
Name     |   email here

我想根据这个模式制作一个MySQL数据库表,并将数据保存到MySQL中。

我想知道如何做到这一点。 还需要一个选项

我们必须检查相应用户是否有正确的电子邮件地址,即@ 形式。

我们可以在导入时将数据作为循环检查吗?

还有如何将这些数据转换到 MySQL 中?

【问题讨论】:

  • 先转成CSV可行吗?
  • 如@Asad之前所说,先转换成CSV,然后用mysql将文件导入数据库。
  • 作为@Asad,我会先将excel表保存为csv,然后用PHP解析和验证它并从中进行一个多插入查询

标签: php mysql database excel


【解决方案1】:

有一个 名为 PHPExcel 的库。使用这个库,您可以轻松解析任何 excel 文件。或者,您可以将文件导出为 csv,这对您来说会更容易。 php 具有处理 csv 文件的本机函数。您可以使用fgetcsv()str_getcsv()

【讨论】:

    【解决方案2】:

    转到此链接并下载将读取 excel 文件并返回数组的 php 类。这个数组将保存所有写入excel文件的数据。

    php excel reader

    它是免费的...

    你也可以在那里看到演示。

    我已经在使用它了。绝对不错。

    您可以随意向我寻求任何其他帮助。

    【讨论】:

    • 文件大小有限制吗?
    • PHP内存限制除外
    【解决方案3】:

    将此 excel 文件保存为 csv 并运行以下代码并添加您的更改

    $source = fopen('email.csv', 'r') or die("Problem open file");
        while (($data = fgetcsv($source, 1000, ",")) !== FALSE)
        {
            $name = $data[0];
            $email = $data[1];
    
    
            mysql_query("INSERT INTO `table` (`name`,`email`) VALUES ('".$name."','".$email."') ");
    
    
        }
        fclose($source);
    

    【讨论】:

    • 使用此代码,我只能获取文件的第一行。
    【解决方案4】:
    <table>
                        <form enctype="multipart/form-data" action="" method="post">
                          <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
                                <tr>
                                <td><h5><b>Select Standared</b></h5></td>
                                <td><select name="chap_sel_std" id="chap_sel_std">
                                                            <option>Select Standared</option>
                                                    <?php
                                                        $exe_sel_std = mysql_query("SELECT * from s_standared");
                                                        while($r_sel_std = mysql_fetch_array($exe_sel_std)){
                                                            $sel_stdid = $r_sel_std['std_id'];
                                                            $sel_std = $r_sel_std['std'];?>
    
                                                            <option value="<?php echo $sel_stdid; ?>"><?php echo $sel_std;?></option>
                                                            <?php } ?>
                                    </select></td>
                                </tr>
                                <tr>
                                    <td><h5><b>Select Font</b></h5></td>
                                    <td><select name="sel_f_gn_que">
                                        <option>Select Font</option>
                                            <?php
                                                $xf = mysql_query("SELECT * from s_font");
                                                while($rquef = mysql_fetch_array($xf)){
                                                    $f_id = $rquef['f_id'];
                                                    $f_name = $rquef['f_name'];  ?>
                                        <option value="<?php echo $f_id; ?>"><?php echo $f_name; }?>  </option>
                                    </select></td>
                                </tr>
                                <tr>
                                    <td><h5><b>Upload Question<h5><b></td>
                                    <td>
                                        <input type="file" name="file" id="file" class="btn">
                                    </td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td colspan="2"><input type="submit" class="btn btn-green big" name="add_que" value="Add Questions"></td>
                                    <td><input type="submit" name="saveandexit" class="" value="Finish" onclick="close();"></td>
                                </tr>
                        </form>
                        </table>
                        </div>                   
    
        <?php
    
                $data = array();
    
        //$db =& DB::connect("mysql://root@localhost/names", array());
        //if (PEAR::isError($db)) { die($db->getMessage()); }
          //quetype    difficulty    standard    subject    chap    que    marks
    
        function add_person($quetype,$dif, $subject,$chap_name,$que,$marks)
        {
         global $data, $db;
    
         //$sth = $db->prepare( "INSERT INTO names VALUES( 0, ?, ?, ?, ? )" );
        // $db->execute( $sth, array( $first, $middle, $last, $email ) );
    
         $data []= array(
           'quetype' => $quetype, 
           'difficulty' => $dif,
           'subject' => $subject,
           'chap' => $chap_name,
           'que' => $que,
           //'ans' => $ans,
           'marks' => $marks
    
         );
        }
    
        if(!isset($_FILES['file']['tmp_name'])){
            echo "";
        }elseif($_FILES['file']['tmp_name'])
        {
         $dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
         $rows = $dom->getElementsByTagName( 'Row' );
         $first_row = true;
         foreach ($rows as $row)
         {
           if ( !$first_row )
           {
             $quetype = "";
             $dif = "";
             $subject = "";
             $chap_name = "";
             $que = "";
             //$ans = "";
             $marks = "";
    
             $index = 1;
             $cells = $row->getElementsByTagName( 'Cell' );
             foreach( $cells as $cell )
             {
               $ind = $cell->getAttribute( 'Index' );
               if ( $ind != null ) $index = $ind;
    
               if ( $index == 1 ) $quetype = $cell->nodeValue;
               if ( $index == 2 ) $dif = $cell->nodeValue;
               if ( $index == 4 ) $subject = $cell->nodeValue;
               if ( $index == 6 ) $chap_name = $cell->nodeValue;
               if ( $index == 8) $que = $cell->nodeValue;
               //if ( $index == 9) $ans = $cell->nodeValue;
               if ( $index == 9) $marks = $cell->nodeValue;
    
               $index += 1;
             }
             add_person($quetype,$dif, $subject,$chap_name,$que,$marks);
    
             if(isset($_POST['add_que'])){    
    
                     $chap_sel_std = $_POST['chap_sel_std'];
                     echo $simquefnt = $_POST['sel_f_gn_que'];
    
                        //que_id    quetype_id    chap_id    sub_id    std_id    que    dif_id    marks    que_cdate
                 //$chap_sel_std = $_POST['chap_sel_std']; //que_id    quetype_id    chap_id    sub_id    std_id    que    dif_id    marks    que_cdate
                 mysql_query("INSERT INTO 
                              s_question
                              VALUES (null,'$quetype','$chap_name','$subject','$chap_sel_std','$que','NO IMAGE','$dif','$marks','$simquefnt','$current')");                                                        
        //         header("location:../admin/quetionaris.php#tabs-que"); 
             echo "Successfully Added";
              }
           }
           $first_row = false;
         }
        }
        ?>
    

    【讨论】:

      猜你喜欢
      • 2013-09-09
      • 2013-03-19
      • 1970-01-01
      • 2011-01-12
      • 2011-12-04
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多