ExcelFileParser处理excel获得数据 可作批量导入到数据库

ExcelFileParser处理excel获得数据 可作批量导入到数据库

提交表单

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Excel数据获取演示</title>
<meta name="Keywords" content="TODO" />
<meta name="Description" content="TODO"/>
</head>
<body>
    <div>
      <div>Excel数据获取演示</div>
      <div>
        <form method="POST" action="/Index/parse" enctype="multipart/form-data">
            <input type="file" name="excel" value="" />
            <input type="submit" name="submit" value="提交" />
        </form>
      </div>
    </div>
</body>
</html>

提交处理
[php]
<?php
/**
* CopyRight (c) 2009,
* All rights reserved.
* 文件名:
* 摘  要:
*
* @author 星期八 ixqbar@hotmail.com
* @version
*/

class IndexAction extends Action
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * 默认索引页
     */
    public function index()
    {
        $this->display();
    }
    /**
     * 提交处理
     */
    public function parse()
    {
       /**
        * $_FILES数组说明
        * array(n) {
        *   ["表单文件框名称"] => array(5) {
        *       ["name"]        => 提交文件名称
        *       ["type"]        => 提交文件类型 Excel为"application/vnd.ms-excel"
        *       ["tmp_name"]    => 临时文件名称
        *       ["error"]       => 错误(0成功1文件太大超过upload_max_filesize2文件太大超过MAX_FILE3上传不完整4没有上传文件)
        *       ["size"]        => 文件大小(单位:KB)
        *   }
        * }
        */
        $return=array(0,'');
        /**
         * 判断是否提交
         * is_uploaded_file(文件名称)用于确定指定的文件是否使用POST方法上传,防止非法提交,通常和move_upload_file一起使用保存上传文件到指定的路径
         */
        if(!isset($_FILES) || !is_uploaded_file($_FILES['excel']['tmp_name']))
        {
            $return=array(1,'提交不合法');
        }
        //处理
        if(0 == $return[0])
        {
            import('@.Util.ExcelParser');
            $excel=new ExcelParser($_FILES['excel']['tmp_name']);
            $return=$excel->main();
        }
        //输出处理
        print_r($return);
    }
}
?>
[/php]

处理类
[php]
<?php
/**
* CopyRight (c) 2009,
* All rights reserved.
* 文件名:excel数据获取
* 摘  要:
*
* @author 星期八 ixqbar@hotmail.com
* @version 0.1
*/
class ExcelParser
{
    private $_data=array(0,'');
    private $_excel_handle;
    private $_excel=array();
    /**
     * 构造函数
     * @param <string> $filename 上传文件临时文件名称
     */
    public function __construct($filename)
    {
        /**
         * 引入excelparser类
         * 普通方法为
         * requires 路径.'excelparser.php';
         * import为ThinkPHP自带导入类方法
         */
        import('@.Util.PHPExcelParser.excelparser','','.php');
        $this->_excel_handle=new ExcelFileParser();
        //错误获取
        $this->checkErrors($filename);
    }
    /**
     * 错误校验
     */
    private function checkErrors($filename)
    {
        /**
         * 方法一
         */
        $error_code=$this->_excel_handle->ParseFromFile($filename);
        /**
         * 方法二
         * $file_handle = fopen($this->_filename,'rb');
         * $content = fread($file_handle,filesize($this->_filename));
         * fclose($file_handle);
         * $error_code = $this->_excel->ParseFromString($content);
         * unset($content,$file_handle);
         */
        switch($error_code)
        {
            case 0:
                //无错误不处理
                break;
            case 1:
                $this->_data=array(1,'文件读取错误(Linux注意读写权限)');
                break;
            case 2:
                $this->_data=array(1,'文件太小');
                break;
            case 3:
                $this->_data=array(1,'读取Excel表头失败');
                break;
            case 4:
                $this->_data=array(1,'文件读取错误');
                break;
            case 5:
                $this->_data=array(1,'文件可能为空');
                break;
            case 6:
                $this->_data=array(1,'文件不完整');
                break;
            case 7:
                $this->_data=array(1,'读取数据错误');
                break;
            case 8:
                $this->_data=array(1,'版本错误');
                break;
        }
        unset($error_code);
    }
    /**
     * Excel信息获取
     */
    private function getExcelInfo()
    {
        if(1==$this->_data[0])return;
        /**
         * 获得sheet数量
         * 获得sheet单元对应的行和列
         */
        $this->_excel['sheet_number']=count($this->_excel_handle->worksheet['name']);
        for($i=0;$i<$this->_excel['sheet_number'];$i++)
        {
            /**
             * 行于列
             * 注意:从0开始计数
             */
            $row=$this->_excel_handle->worksheet['data'][$i]['max_row'];
            $col=$this->_excel_handle->worksheet['data'][$i]['max_col'];
            $this->_excel['row_number'][$i]=($row==NULL)?0:++$row;
            $this->_excel['col_number'][$i]=($col==NULL)?0:++$col;
            unset($row,$col);
        }
    }
    /**
     * 中文处理函数
     * @return <string>
     */
    private function uc2html($str)
    {
        $ret = '';
        for( $i=0; $i<strlen($str)/2; $i++ )
        {
            $charcode = ord($str[$i*2])+256*ord($str[$i*2+1]);
            $ret .= '&#'.$charcode.';';
        }
        return mb_convert_encoding($ret,'UTF-8','HTML-ENTITIES');
    }
    /**
     * Excel数据获取
     */
    private function getExcelData()
    {
        if(1==$this->_data[0])return;

        //修改标记
        $this->_data[0]=1;
        //获取数据
        for($i=0;$i<$this->_excel['sheet_number'];$i++)
        {
            /**
             * 对行循环
             */
            for($j=0;$j<$this->_excel['row_number'][$i];$j++)
            {
                /**
                 * 对列循环
                 */
                for($k=0;$k<$this->_excel['col_number'][$i];$k++)
                {
                    /**
                     * array(4) {
                     *   ["type"]   => 类型 [0字符类型1整数2浮点数3日期]
                     *   ["font"]   => 字体
                     *   ["data"]   => 数据
                     *   ...
                     * }
                     */
                    $data=$this->_excel_handle->worksheet['data'][$i]['cell'][$j][$k];
                    switch($data['type'])
                    {
                        case 0:
                            //字符类型
                            if($this->_excel_handle->sst['unicode'][$data['data']])
                            {
                                //中文处理
                                $data['data'] = $this->uc2html($this->_excel_handle->sst['data'][$data['data']]);
                            }
                            else
                            {
                                $data['data'] = $this->_excel_handle->sst['data'][$data['data']];
                            }
                            break;
                        case 1:
                            //整数
                            //TODO
                            break;
                        case 2:
                            //浮点数
                            //TODO
                            break;
                        case 3:
                            //日期
                            //TODO
                            break;
                    }
                    $this->_data[1][$i][$j][$k]=$data['data'];
                    unset($data);
                }
            }
        }
    }
    /**
     * 主函数
     * @return <array> array(标识符,内容s)
     */
    public function main()
    {
        //Excel信息获取
        $this->getExcelInfo();
        //Excel数据获取
        $this->getExcelData();
        return $this->_data;
    }
}

?>

时间: 2024-07-31 12:55:24

ExcelFileParser处理excel获得数据 可作批量导入到数据库的相关文章

php中使用ExcelFileParser处理excel获得数据(可作批量导入到数据库使用)_php技巧

复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&qu

poi导入excel到数据库,excel中的空白行也会导入到数据库

问题描述 poi导入excel到数据库,excel中的空白行也会导入到数据库 if条件判断也没起作用,如何才能正确判断 解决方案 将数据库中的内容导入到excel 解决方案二: 应该判断的是 该行内容的空白,而不是 row 对应的 null 值. row 对应不会为 null 的,只是行内数据的 空白 . 解决方案三: 判断 row == null这个地方不对,需要判断行里的内容为null才可以 你可以单步调试一下,看看 解决方案四: 判断方法不对,看下执行到的步骤

文本数据批量导入导出数据库功能~~急~~

问题描述 在asp.net中怎么实现txt和Iso文件批量导入跟导出,导入的数据内容作成报表打印哪个能这好给我个例子 解决方案 解决方案二:关键在于你自定义的文件格式

请问,可以将excel中的地址,批量导入地图中吗?

问题描述 想找这样一种地图,可以将excel中的地址批量导入到电子地图中.快递公司希望把客户地址标注在地图上,并计算出某个范围内的业务量.请问,可以导入地图吗?可以实现计算一定范围的业务量吗?谢谢.有兴趣的请联系QQ:1819259627 解决方案 解决方案二:地址是gps坐标么?解决方案三:不是,就是中文地址,无坐标.百度地图搜索每次只能搜索一个地址,如果能批量搜索,也就可以用了.所以想自己做一个.解决方案四:没有坐标的话你全靠地址匹配是不可靠的

php 把excel文件批量导入到数据库代码

我们是利用了ExcelFileParser这个扩展来处理的哦, <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> &l

php 把excel批量导入到数据库代码

我们是利用了ExcelFileParser这个扩展来处理的哦, <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> &l

数据批量导入Oracle数据库

SQL*LOADER是大型数据 仓库选择使用的加载方法,因为它提供了最快速的途径(DIRECT,PARALLEL).现在,我们抛开其理论不谈,用实例来使 您快速掌握SQL*LOADER的使用方法. 首先,我们认识一下SQL*LOADER. 在NT下,SQL*LOADER的命令为SQLLDR,在UNIX下一般为sqlldr/sqlload. 如执行:d:oracle>sqlldr SQL*Loader: Release 8.1.6.0.0 - Production on 星期二 1月 8 11:0

c++将txt数据批量导入mysql数据库的例子

////包括多线程的使用,数据库的操作和打开文件对话框的使用. #include "stdafx.h" #include <windows.h> #include <windowsx.h> #include "resource.h" #include "MainDlg.h" #include <SQL.H>//连接库:odbc32.lib odbccp32.lib #include <SQLEXT.H&g

python批量导入MongoDB数据库

由于源文件不是MongoDB支持的JSON和BSON 格式的数据, 所以只能将源数据转变格式后, 用脚本将其导入,所测数据为中科院信工所提供的数据.(这也是在信工所接触的第一个写程序的活.)源码如下: #!/usr/bin/env python #encoding:utf-8 ################################### # function: analysis the log in the 'die' to JSON , and #output the data to