phpspreadsheet使用实例_php获取html中文本框内容

phpspreadsheet使用实例_php获取html中文本框内容目录安装引用导入Excel获取日期格式安装composerrequirephpoffice/phpspreadsheet引用usePhpOffice\PhpSpreadsheet\Reader\Xlsx;usePhpOffice\PhpSpreadsheet\Reader\Xls;usePhpOffice\PhpSpreadsheet\IOFactory;usePhpOffice\PhpSpreadsheet\Cell\Coordinate;usePhpOffice\PhpS

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

安装

composer require phpoffice/phpspreadsheet

引用

use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

导入Excel

public function importExcel(string $file = '', int $sheet = 0, int $columnCnt = 0, &$options = [])
    {
        try {
            /* 转码 */
            $file = iconv("utf-8", "gb2312", $file);

            if (empty($file) OR !file_exists($file)) {
                throw new \Exception('文件不存在!');
            }

            /** @var Xlsx $objRead */
            $objRead = IOFactory::createReader('Xlsx');

            if (!$objRead->canRead($file)) {
                /** @var Xls $objRead */
                $objRead = IOFactory::createReader('Xls');

                if (!$objRead->canRead($file)) {
                    throw new \Exception('只支持导入Excel文件!');
                }
            }

            /* 如果不需要获取特殊操作,则只读内容,可以大幅度提升读取Excel效率 */
            empty($options) && $objRead->setReadDataOnly(true);
            /* 建立excel对象 */
            $obj = $objRead->load($file);
            /* 获取指定的sheet表 */
            $currSheet = $obj->getSheet($sheet);

            if (isset($options['mergeCells'])) {
                /* 读取合并行列 */
                $options['mergeCells'] = $currSheet->getMergeCells();
            }

            if (0 == $columnCnt) {
                /* 取得最大的列号 */
                $columnH = $currSheet->getHighestColumn();
                /* 兼容原逻辑,循环时使用的是小于等于 */
                $columnCnt = Coordinate::columnIndexFromString($columnH);
            }

            /* 获取总行数 */
            $rowCnt = $currSheet->getHighestRow();
            $data   = [];

            /* 读取内容 */
            for ($_row = 1; $_row <= $rowCnt; $_row++) {
                $isNull = true;

                for ($_column = 1; $_column <= $columnCnt; $_column++) {
                    $cellName = Coordinate::stringFromColumnIndex($_column);
                    $cellId   = $cellName . $_row;
                    $cell     = $currSheet->getCell($cellId);

                    if (isset($options['format'])) {
                        /* 获取格式 */
                        $format = $cell->getStyle()->getNumberFormat()->getFormatCode();
                        /* 记录格式 */
                        $options['format'][$_row][$cellName] = $format;
                    }

                    if (isset($options['formula'])) {
                        /* 获取公式,公式均为=号开头数据 */
                        $formula = $currSheet->getCell($cellId)->getValue();

                        if (0 === strpos($formula, '=')) {
                            $options['formula'][$cellName . $_row] = $formula;
                        }
                    }

                    if (isset($format) && 'm/d/yyyy' == $format) {
                        /* 日期格式翻转处理 */
                        $cell->getStyle()->getNumberFormat()->setFormatCode('yyyy/mm/dd');
                    }

                    $data[$_row][$cellName] = trim($currSheet->getCell($cellId)->getFormattedValue());

                    if (!empty($data[$_row][$cellName])) {
                        $isNull = false;
                    }
                }

                /* 判断是否整行数据为空,是的话删除该行数据 */
                if ($isNull) {
                    unset($data[$_row]);
                }
            }

            return $data;
        } catch (\Exception $e) {
            throw $e;
        }
    }

获取日期格式

默认获取的是日期的值(日期数字42380表示从1900-1-1开始的第42380天,即2016-1-11)跟PHP中的时间戳不一致

// 默认
$value = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value);

导出数据

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('工作表一');
$sheet->setCellValue('A1', '11');
$sheet->setCellValue('B1', '22');
$writer = new Xlsx($spreadsheet);
$filename = date('YmdHis',time());
$writer->save($filename.'.xlsx');
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/192443.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux文件的创建与扫描,Linux系统quotacheck命令:扫描文件系统并建立Quota记录文件…

    linux文件的创建与扫描,Linux系统quotacheck命令:扫描文件系统并建立Quota记录文件…其实,磁盘配额(Quota)就是通过分析整个文件系统中每个用户和群组拥有的文件总数和总容量,再将这些数据记录在文件系统中的最顶层目录中,然后在此记录文件中使用各个用户和群组的配额限制值去规范磁盘使用量的。因此,建立Quota的记录文件是非常有必要的。扫描文件系统(必须含有挂载参数usrquota和grpquota)并建立Quota记录文件,可以使用quotacheck命令。此命令…

    2025年7月24日
    1
  • 香农编码简单例题_单链表逆序输出c语言

    香农编码简单例题_单链表逆序输出c语言常量介绍:si:信源符号p(si):该项概率pi:前i-1项累加概率p(s1)+p(s2)+…+p(s(i-1))li:码长,计算公式[-log(p(si))]+1;(下取整)求解步骤:1、将信源符号按从大到小的顺序排列2、求码长li3、求累加概率pi4、将累加概率pi转换为二进制小数,并根据码长li取小数点后li为作为码字介绍:本程序是将原始数据存放到记事本Data.txt中,记事本应…

    2022年9月3日
    5
  • Spring cloud和Dubbo区别

    Spring cloud和Dubbo区别Springboot是创建产品级的Spring应用和服务,简化了配置文件,使用嵌入式web服务器,有很多开箱即用微服务功能,可以和springcloud联合部署。springcloud是微服务工具包,为开发者提供了在分布式系统的配置管理、服务发现、断路器、智能路由、微代理、控制总线等开发工具包。zookeeperDubbo默认不支持rest因此基于它扩展出了自己的DubboX(当当扩展)…

    2022年6月12日
    33
  • css两端对齐IOS不适用 样式冲突

    css两端对齐IOS不适用 样式冲突问题 textclass explain v html info goodsExplain explain white space pre wrap 用来换行 display inline block text align justify 用来两端对齐 text align last left word break break word 文本带有换行符 没有带标签 textclass explain v html info goodsExplain

    2025年7月27日
    0
  • 为什么pycharm找不到模块_pycharm project interpreter

    为什么pycharm找不到模块_pycharm project interpreter如果要连接服务器的话,是需要在deployment里进行操作的,但是有时候不管怎么找,在Pycharm中都找不到。(PS:我就遇到了这样的问题)其实原因很简单,你装的Pycharm可能是社区版,不具有远程连接服务器的功能,只需要下载一个专业版就行。用学生账号免费使用专业版的方法可参照这个:https://blog.csdn.net/weixin_45459911/article/details…

    2022年8月26日
    5
  • C#多线程锁「建议收藏」

    C#多线程锁「建议收藏」c#多线程操作队列

    2022年6月21日
    29

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号