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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • hey,你的CommonJS规范

    hey,你的CommonJS规范

    2021年6月13日
    113
  • Spring中ApplicationContext对Beanfactory扩展[通俗易懂]

    Spring中ApplicationContext对Beanfactory扩展[通俗易懂]ApplicationContext比BeanFactory扩展了高级特性,除了集成了ListableBeanFactory和HierarchicalBeanFactory以外,实现了如下附加功能:

    2022年6月24日
    30
  • iptable 理解

    iptable 理解这个当初我理解不了,主要是没把netfilter理解清楚。Netfilter是集成在内核中的,用来定义存储各种规则的。Iptalbe是修改这些规则的工具,修改后存在netfilter里面。数据包进入LINUX服务器时,先进入服务器的netfilter模块中进行判断处理。 Netfilter包含有三种表,三种表下共包含有五种链,链下面包含各种规则。即表包含若干链,链包含若干规则。 …

    2022年5月28日
    97
  • Java中xml转义字符和gt,gte,lt,lte缩写

    Java中xml转义字符和gt,gte,lt,lte缩写javamybatisXML文件中不允许出现”>”、”<“之类的符号。需要转义”=”是可以正常使用的 字段 符号 说明 &lt; < 小于号 &gt; > 大于号 …

    2022年7月20日
    18
  • Drupal 安装过程「建议收藏」

    Drupal 安装过程「建议收藏」php.ini文件https://drupal.stackexchange.com/questions/164172/problem-installing-in-local-the-translation-server-is-offlinecurl.cainfo=”C:\wamp64\keys\cacert.pem”转载于:https://www.cnblogs.com/A…

    2022年7月20日
    15
  • Burpsuite Professional安装及使用教程(抓包)

    Burpsuite Professional安装及使用教程(抓包)先从吾爱破解论坛下载工具:https://down.52pojie.cn/Tools/Network_Analyzer/Burp_Suite_Pro_v1.7.37_Loader_Keygen.zip工具运行需要Java环境,请自行安装,此处不赘述。解压完后双击keygen填一下LicenseText(随意),然后点击Run即可启动BurpSuite的主程序将keygen中得到的License复制粘贴到这里然后点击Next点击这个后出现下一个界面点击Copyreques

    2022年6月5日
    65

发表回复

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

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