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


相关推荐

  • leetcode 792_leetcode5421编码

    leetcode 792_leetcode5421编码给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。示例 1:输入:board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED”输出:true示例 2:输

    2022年8月9日
    6
  • validation怎么用_什么是确认validation

    validation怎么用_什么是确认validation引入文件环境在jQuery下,所有先要引入jQuery1<%–校验样式–%>2<linkrel=”stylesheet”href=”<%=basePath%>css/validationEngine.jquery.css”>3<%–校验及自定义规则规则–%>4<scripttype=”t…

    2022年10月4日
    7
  • Altium Designer——AD画PCB图步骤总结

    Altium Designer——AD画PCB图步骤总结AD画PCB图步骤总结本文总结一下AD画PCB的步骤,以防时间久了忘记一些小步骤。现在所用着的AD版本为AD17。电脑环境:AltiumDesigner17.1.5(build172)点这里下载,密码:rwsxAD画PCB图步骤:1、创建工程,新建“PrjPCB”文件。2、画原理图,新建“SchDOC”文件。画原理图时,如果没有的器件自己绘制原理…

    2022年7月15日
    22
  • 【Firebug入门指南】「建议收藏」

    据说,对于网页开发人员来说,Firebug是Firefox浏览器中最好的插件之一。…

    2022年1月18日
    54
  • 中文按字母排序_怎么按首字母顺序排

    中文按字母排序_怎么按首字母顺序排        项目中用到前端排序,自己写了一个实现,给大家分享一下。      存在的问题:很多时候是用汉字的首拼来比较,但汉字转拼音在前端实现是个问题,主要表现在两个地方1、字符库里的数据量没有覆盖所有汉字,2、多音字问题(注:这里都是说简体汉字)。      实现思路:1、从网上找了一个汉字转拼音的库,能转大部分汉字(多音字也没处理);2、写一个字符比较的函数;3、调用Array里的…

    2022年10月12日
    5
  • javascript 跳转_jquery页面跳转的方法

    javascript 跳转_jquery页面跳转的方法转自:微点阅读https://www.weidianyuedu.com第一种:<scriptlanguage=”javascript”type=”text/javascript”>window.location.href=”login.jsp?backurl=”+window.location.href;</script>第二种:<scriptlanguage=”javascript”>alert(“返回”);window.histor..

    2022年8月13日
    8

发表回复

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

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