php轻量级excel生成方法

编写原因

有个开发任务,是我这边要提供一个分类信息模板给合作方公司作为数据对接用的,分类数据是我数据库里面的。这个模板实际上就是一个excel文件。那我就要用php代码生成一份文件了。但是 我 不想使用phpexcel那个插件,因为他太重量了。所以我github上面找到一个轻量级的类库,用来生成。

类库代码

<?php

/**
 * Excel_XML
 */

/**
 * Class Excel_XML
 * 
 * A simple export library for dumping array data into an excel
 * readable format. Supports OpenOffice Calc as well.
 * 
 * @author    Oliver Schwarz <oliver.schwarz@gmail.com>
 */
class Excel_XML
{

        /**
         * MicrosoftXML Header for Excel
         * @var string
         */
        const sHeader = "<?xml version=\"1.0\" encoding=\"%s\"?\>\n<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">";

        /**
         * MicrosoftXML Footer for Excel
         * @var string
         */
        const sFooter = "</Workbook>";

        /**
         * Worksheet & Data
         * @var array
         */
        private $aWorksheetData;

        /**
         * Output string
         * @var string
         */
        private $sOutput;

        /**
         * Encoding to be used
         * @var string
         */
        private $sEncoding;

        /**
         * Constructor
         *
         * Instanciates the class allowing a user-defined encoding.
         *
         * @param string $sEncoding Charset encoding to be used
         */
        public function __construct($sEncoding = 'UTF-8')
        {
                $this->sEncoding = $sEncoding;
                $this->sOutput = '';
        }

        /**
         * Add a worksheet
         *
         * Creates a new worksheet and adds the given data to it.
         * @param string $title Title of worksheet
         * @param array $data 2-dimensional array of data
         */
        public function addWorksheet($title, $data)
        {
                $this->aWorksheetData[] = array(
                        'title' => $this->getWorksheetTitle($title),
                        'data'  => $data
                );
        }

        /**
         * Send workbook to browser
         *
         * Sends the finished workbook to the browser using PHP's header
         * directive.
         *
         * @param string $filename Filename to use for sending the workbook
         */
        public function sendWorkbook($filename)
        {
                if (!preg_match('/\.(xml|xls)$/', $filename)):
                        throw new Exception('Filename mimetype must be .xml or .xls');
                endif;
                $filename = $this->getWorkbookTitle($filename);
                $this->generateWorkbook();
                if (preg_match('/\.xls$/', $filename)):
                        header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding);
                        header("Content-Disposition: inline; filename=\"" . $filename . "\"");
                else:
                        header("Content-Type: application/xml; charset=" . $this->sEncoding);
                        header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
                endif;
                echo $this->sOutput;
        }

        /**
         * Write workbook to file
         *
         * Writes the workbook into the file/path given as a parameters.
         * The method checks whether the directory is writable and the
         * file is not existing and writes the file.
         *
         * @param string $filename Filename to use for writing (must contain mimetype)
         * @param string $path Path to use for writing [optional]
         */
        public function writeWorkbook($filename, $path = '')
        {
                $this->generateWorkbook();
                $filename = $this->getWorkbookTitle($filename);
                if (!$handle = @fopen($path . $filename, 'w+')):
                        throw new Exception(sprintf("Not allowed to write to file %s", $path . $filename));
                endif;
                if (@fwrite($handle, $this->sOutput) === false):
                        throw new Exception(sprintf("Error writing to file %s", $path . $filename));
                endif;
                @fclose($handle);
                return sprintf("File %s written", $path . $filename);
        }

        /**
         * Get workbook output
         *
         * Just returns the generated workbook content.
         *
         * @return string Output generated by the class
         */
        public function getWorkbook()
        {
                $this->generateWorkbook();
                return $this->sOutput;
        }

        /**
         * Compatibility: Add an array
         *
         * This method implements compatibility to version 1.1. Though using
         * self::addWorksheet surely is an improvement, nobody should be
         * forced to rewrite his code.
         *
         * @param array $data Data to be added
         */
        public function addArray($data)
        {
                $this->addWorksheet('Table1', $data);
        }

        /**
         * Compatibility: Generate XML
         *
         * This method implements compatibility to version 1.1 and generates
         * the excel file.
         *
         * @param string $filename Filename (without mimetype)
         */
        public function generateXML($filename)
        {
                $filename = $this->getWorkbookTitle($filename);
                $filename .= '.xls';
                $this->sendWorkbook($filename);
        }

        /**
         * Workbook title correction
         *
         * Corrects filename (if necessary) stripping out non-allowed
         * characters.
         *
         * @param string $filename Desired filename
         * @return string Corrected filename
         */
        private function getWorkbookTitle($filename)
        {
                return preg_replace('/[^aA-zZ0-9\_\-\.]/', '', $filename);
        }

        /**
         * Worksheet title correction
         *
         * Corrects the worksheet title (given by the user) by the allowed
         * characters by Excel.
         *
         * @param string $title Desired worksheet title
         * @return string Corrected worksheet title
         */
        private function getWorksheetTitle($title)
        {
                $title = preg_replace ("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title);
                return substr ($title, 0, 31);
        }

        /**
         * Generate the workbook
         *
         * This is the main wrapper to generate the workbook.
         * It will invoke the creation of worksheets, rows and
         * columns.
         */
        private function generateWorkbook()
        {
                $this->sOutput .= stripslashes(sprintf(self::sHeader, $this->sEncoding)) . "\n";
                foreach ($this->aWorksheetData as $item):
                        $this->generateWorksheet($item);
                endforeach;
                $this->sOutput .= self::sFooter;
        }

        /**
         * Generate the Worksheet
         *
         * The second wrapper generates the worksheet. When the worksheet
         * data seems to be more than the excel allowed maximum lines, the
         * array is sliced.
         *
         * @param array $item Worksheet data
         * @todo Add a security check to testify whether this is an array
         */
        private function generateWorksheet($item)
        {
                $this->sOutput .= sprintf("<Worksheet ss:Name=\"%s\">\n    <Table>\n", $item['title']);
                if (count($item['data']))
                        $item['data'] = array_slice($item['data'], 0, 65536);
                foreach ($item['data'] as $k => $v):
                        $this->generateRow($v);
                endforeach;
                $this->sOutput .= "    </Table>\n</Worksheet>\n";
        }

        /**
         * Generate the single row
         * @param array Item with row data
         */
        private function generateRow($item)
        {
                $this->sOutput .= "        <Row>\n";
                foreach ($item as $k => $v):
                        $this->generateCell($v);
                endforeach;
                $this->sOutput .= "        </Row>\n";
        }

        /**
         * Generate the single cell
         * @param string $item Cell data
         */
        private function generateCell($item)
        {
                $type = 'String';
                if (is_numeric($item)):
                        $type = 'Number';
                        if ($item{0} == '0' && strlen($item) > 1 && $item{1} != '.'):
                                $type = 'String';
                        endif;
                endif;
                $item = str_replace('&#039;', '&apos;', htmlspecialchars($item, ENT_QUOTES));
                $this->sOutput .= sprintf("            <Cell><Data ss:Type=\"%s\">%s</Data></Cell>\n", $type, $item);
        }

        /**
         * Deconstructor
         * Resets the main variables/objects
         */
        public function __destruct()
        {
                unset($this->aWorksheetData);
                unset($this->sOutput);
        }

}

?>

演示代码

<?php
// 引入类库文件
require "php-excel.class.php";
header("Content-type: text/html; charset=utf-8");
// 文件头部
$array[] = ['一级分类编码','一级分类名称','二级分类编码','二级分类名称','三级分类编码','三级分类名称'];

// 文件数据
$array[] = ['101','aaa','102','bbb','103','ccc'];

$xls = new \Excel_XML();
$xls->addWorksheet('Names', $array);
$xls->sendWorkbook('pmp.xls');




效果展示

 

是不是很简单。代码量很少。所以个人感觉很方便,大家多多学习一起进步

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DouPHP轻量级商城管理系统,基于DouPHP核心开发,使用PHP+MYSQL架构的,可以使用它快速搭建一个商城系统。操作简单后台简约明了,从使用者而不是开发者的角度出发设计后台功能布局,完全不需要使用手册就可以轻松进行日常内容编辑工作。功能简单系统核心功能只有简单的单页面、博客、文章等模块,基于实际使用甚至可以将文章卸载。因此它可以应用于十分基础的建站要求,实际上很多企业网站所需的功能就很基础。扩展性强与传统的网站系统不同,DouPHP并不内置模块生成工具,因为生成工具往往使得系统十分臃肿。我们将功能模块开发好(实际上这样功能模块会有更大的开发空间),然后放在DouPHP自带的在线模块扩展功能里,操作时只需要点击安装,即可实现将功能模块下载并自动完成安装,最重要的是这些模块都是完全独立了,模块安装程序只负责下载、解压、数据库导入工作。模块插件功能性模块:投票模块、自定义表单模块、工单模块等、会员模块、订单模块、视频模块、下载模块、图片模块等;系统基础模块:公众号模块、小程序模块、内容碎片、数据导出EXCEL模块;插件扩展:支付宝、微信支付、PAYPAL、QQ登录、微信登录、微博登录;放心使用系统免费开源,任何人可下载使用DouPHP,包括企业,我们并不限制将DouPHP用于商业用途。关于定制开发,不会因为是DouPHP官方就收取更高的费用,我们采取低价策略提供专业技术服务,统一策略应用于收费模板和模块。系统定位致力于中小企业搭建官网建设,但又不限于企业网站,基于现有框架通过模块扩展提供个人博客、网上商城、投票系统、企业在线办公等需求的轻量级解决方案。开发愿景我们希望开发一款面向使用者的网站管理系统
DouPHP轻量级商城管理系统,基于DouPHP核心开发,使用PHP+MYSQL架构的,可以使用它快速搭建一个商城系统。 DouPHP功能特色(模块全部免费,一键安装) 功能性模块:售后服务模块、防伪查询模块、投票模块、自定义表单模块、工单模块等、会员模块、订单模块等; 系统基础模块:公众号模块、数据导出EXCEL模块; 插件扩展:离线支付、支付宝、微信支付、PAYPAL、QQ登录、微信登录、微博登录; 操作简单 后台简约明了,从使用者而不是开发者的角度出发设计后台功能布局,完全不需要使用手册就可以轻松进行日常内容编辑工作。 功能简单 系统核心功能只有简单的单页面、博客、文章等模块,基于实际使用甚至可以将文章卸载。因此它可以应用于十分基础的建站要求,实际上很多企业网站所需的功能就很基础。 扩展性强 与传统的网站系统不同,DouPHP并不内置模块生成工具,因为生成工具往往使得系统十分臃肿。我们将功能模块开发好(实际上这样功能模块会有更大的开发空间),然后放在DouPHP自带的在线模块扩展功能里,操作时只需要点击安装,即可实现将功能模块下载并自动完成安装,最重要的是这些模块都是完全独立了,模块安装程序只负责下载、解压、数据库导入工作。 放心使用 系统免费开源,任何人可下载使用DouPHP,包括企业,我们并不限制将DouPHP用于商业用途。关于定制开发,不会因为是DouPHP官方就收取更高的费用,我们采取低价策略提供专业技术服务,统一策略应用于收费模板和模块。 系统定位 致力于中小企业搭建官网建设,但又不限于企业网站,基于现有框架通过模块扩展提供个人博客、网上商城、投票系统、企业在线办公等需求的轻量级解决方案。 开发愿景 我们希望开发一款面向使用者的网站管理系统

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值