• phpexcel如何读和写大于26列的excel


    主要运用到PHPExcel_Cell类的两个方法

    1读取excel大于26列时.

    [plain] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. PHPExcel_Cell::columnIndexFromString($highestColumm)://由列名转化为列索引数字 'A->0,Z->25'  
    1>计算出总列数
    [plain] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. $highestColumm = $sheet->getHighestColumn(); // 获得最多的列数,返回列名的大写字母  如 'AB','F'等  
    2>计算出列对应的索引
    [plain] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. $end_index     = PHPExcel_Cell::columnIndexFromString($highestColumm);//由列名转为列数('AB'->28)  
    [plain] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. $PHPExcel      = $reader->load($file_path_root . '/' . $file_name);//文件路径加载文件名  
    2.         $sheet         = $PHPExcel->getSheet(0);  
    3.         $highestRow    = $sheet->getHighestRow(); // 取得总行数  
    4.         $highestColumm = $sheet->getHighestColumn(); // 取得总列数  
    5.         $end_index     = PHPExcel_Cell::columnIndexFromString($highestColumm);//由列名转为列数('AB'->28)  
    6.   
    7.         $titles  = array();  
    8.         $content = array();  
    9.         /** 循环读取每个单元格的数据 */  
    10.         for ($row = 1; $row <= $highestRow; $row++) {  
    11.             for ($column = 0; $column < $end_index; $column++) {  
    12.                 $col_name = PHPExcel_Cell::stringFromColumnIndex($column);//由列数反转列名(0->'A')  
    13.                 $value    = mb_convert_encoding($sheet->getCell($col_name . $row)->getValue(), 'gbk', 'utf8');//转码  
    14.                 if ($row == 1) {  
    15.                     //获取表头  
    16.                     $titles[] = preg_replace('/s/', '', $value);  
    17.                 } else {  
    18.                     if ($sheet->getCell($col_name . $row)->getDataType() == PHPExcel_Cell_DataType::TYPE_NUMERIC) {  
    19.                         //数字类型时,三位精度标准来比较该数是否在0,1之间,转换为百分数,保留一位小数  
    20.                         if (bccomp(floatval($value), floatval('1'), 3) == -1 && bccomp(floatval($value), floatval('0'), 3) == 1) {  
    21.                             $value = sprintf("%01.1f", $value * 100) . '%';  
    22.                         }  
    23.                     }  
    24.                     $content[$row][$column] = preg_replace('/s/', '', $value);  
    25.                 }  
    26.             }  
    27.         }  
    ps:
     
    上传excel时,如果没有读写权限,可以写到tmp目录下,这个目录下一般都是有读写权限的.

    2写大于26列的excel

    [php] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. $title_col = PHPExcel_Cell::stringFromColumnIndex($k);//标题行列名,由列索引反转列名(0->'A')  
    [php] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. </pre><pre name="code" class="php"><pre name="code" class="php">foreach($titles as $k =>$t)  
    2. {  
    3.        $title_col = PHPExcel_Cell::stringFromColumnIndex($k);//标题行列名,由列索引反转列名(0->'A')  
    4.        $this->_objPHPExcel->getActiveSheet()->getColumnDimension($title_col)->setWidth(12);  
    5.        $this->_objPHPExcel->setActiveSheetIndex()->getStyle($title_col."1")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);  
    6.        $this->_objPHPExcel->getActiveSheet()->setCellValue($title_col."1",iconv('GBK','UTF-8//IGNORE',$t));//标题行  
    7. }  

    3读取excel百分号时自动转化为了小数

    处理单元格数据时,如果有百分号小数会自动转为小数,处理时需要判断单元格数据类型,因为初步认定只有百分数是小数,其余各列不存在小数.故处理结果:
    判断数据类型为数字,其余几种类型都在 PHPExcel_Cell_DataType类中:
     
    [php] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. /* Data types */  
    2.     const TYPE_STRING2  = 'str';  
    3.     const TYPE_STRING   = 's';  
    4.     const TYPE_FORMULA  = 'f';  
    5.     const TYPE_NUMERIC  = 'n';  
    6.     const TYPE_BOOL     = 'b';  
    7.     const TYPE_NULL     = 'null';  
    8.     const TYPE_INLINE   = 'inlineStr';  
    9.     const TYPE_ERROR    = 'e';  
    [php] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. if ($sheet->getCell($col_name . $row)->getDataType() == PHPExcel_Cell_DataType::TYPE_NUMERIC) {  
    2.                         //数字类型时,三位精度标准来比较该数是否在0,1之间,转换为百分数,保留一位小数  
    3.                         if (bccomp(floatval($value), floatval('1'), 3) == -1 && bccomp(floatval($value), floatval('0'), 3) == 1) {  
    4.                             $value = sprintf("%01.1f", $value * 100) . '%';  
    5.                         }  
    6. }  

    判断浮点数是否在0-1之间(三位精度),然后组合成百分数即可:
    [php] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. $value = sprintf("%01.1f", $value * 100) . '%';  
  • 相关阅读:
    MVC @Url.Action 小示例
    Eclipse快捷键
    MVC视频下载/文件上传
    MySQL数据库备份/导出
    C#文件下载
    C#正则表达式匹配字符串中的数字
    常用的LINQ to SQL 用法
    C# 实现抓取网页内容(一)
    C# 繁体字和简体字之间的相互转换
    我到底会什么??
  • 原文地址:https://www.cnblogs.com/bit5566/p/6735137.html
Copyright © 2020-2023  润新知