zoukankan      html  css  js  c++  java
  • php读取excel文件

    <form id="form1" name="form1" method="post" action="" enctype="multipart/form-data">
        <label>
            <input name="file1" type="file" id="file1" />
            <input type="submit" name="Submit1" value="提交" />
        </label>
    </form>
    <p>
    <?php
    /*
    到 http://sourceforge.net/projects/phpexcelreader/ 下载 phpExcelReader
    压缩包里的 Excel 文件夹里有两个文件 oleread.inc reader.php
    将 oleread.inc 重命名为 OLERead.php
    将 reader.php 文件中 require_once 'Spreadsheet/Excel/Reader/OLERead.php'; 替换成 require_once 'OLERead.php';
    将 reader.php 文件中 $this->_ole =& new OLERead(); 替换成 $this->_ole = new OLERead();
    */
    
    
    require_once 'Excel/reader.php';//需要读取 excel 的页面引用 reader.php 文件
    $excel_data = new Spreadsheet_Excel_Reader ();//实例化一个读取excel对象
    $excel_data->setOutputEncoding ( 'utf-8' );//赋编码格式
    
    //连接数据库
    $conn = mysql_connect ( 'localhost', 'root', 'root' ) or die ( "Can not connect to database." );
    mysql_query ( "set names 'utf-8'" );
    mysql_select_db ( 'test' );
    
    
    if ($_POST ['Submit1']) {
        //先把上传文件转存到访问页面所在目录下
        move_uploaded_file($_FILES["file1"]["tmp_name"], "./" . $_FILES["file1"]["name"]);
        $excel_data->read ( $_FILES["file1"]["name"] );//为读取excel对象赋刚转存的文件
        //$excel_data->read ( "aaa.xls" );//如非提交文件也可直接赋文件路径
        for($i = 1; $i <= $excel_data->sheets [0] ['numRows']; $i ++) {
            //$id = $excel_data->sheets [0] ['cells'] [$i] [1];//sheets [0] 是excel文件中第一页,$i 第几行,1第一列,页索引从0开始,行,列索引都从1开始
            $title = $excel_data->sheets [0] ['cells'] [$i] [2];//第一页,第$i行,第2列
            $content = $excel_data->sheets [0] ['cells'] [$i] [3];
            $ins_message = "INSERT INTO message( title , content ) VALUES( '$title' , '$content' )";
            $que_message = mysql_query ( $ins_message );
        }
    }
    /*
    excel中 工具->选项->常规 选中 R1C1引用样式 可使 列名由字母变为数字
    */
    ?>
  • 相关阅读:
    DotnetBrowser入门教程-(2)启动简单的Web服务
    DotnetBrowser入门教程-(1)浏览器控件使用
    Delphi初始化与结束化
    用友二次开发之用友备份专家[1.01]
    用友账套恢复工具
    用友二次开发之总账自定义结转
    用友二次开发之登陆界面
    用友二次开发之U810.1销售预订单导入
    表格控件表头栏目(Column)与数据表头步
    Delphi开发的IP地址修改工具
  • 原文地址:https://www.cnblogs.com/dreamhome/p/2990077.html
Copyright © 2011-2022 走看看