广告位联系
返回顶部
分享到

PHP中创建和编辑Excel表格的教程

php 来源:互联网搜集 作者:酷站 发布时间:2018-09-15 08:28:45 人浏览
摘要

今天小编给大家带来PHP中创建和编辑Excel表格的教程 要使用纯PHP创建或编辑Excel电子表格,我们将使用PHPExcel库,它可以读写许多电子表格格式,包括xls,xlsx,ods和csv。在我们继续之前,仔细检查您的服务器上是否有PHP 5.2或更高版本以及安装了以下PHP扩

今天小编给大家带来PHP中创建和编辑Excel表格的教程

要使用纯PHP创建或编辑Excel电子表格,我们将使用PHPExcel库,它可以读写许多电子表格格式,包括xls,xlsx,ods和csv。在我们继续之前,仔细检查您的服务器上是否有PHP 5.2或更高版本以及安装了以下PHP扩展:php_zip,php_xml和php_gd2。

创建电子表格

创建电子表格是PHP应用程序中最常见的用例之一,用于将数据导出到Excel电子表格。查看以下代码,了解如何使用PHPExcel创建示例Excel电子表格:

// Include PHPExcel library and create its object
require('PHPExcel.php');
 
$phpExcel = new PHPExcel;
 
// Set default font to Arial
$phpExcel->getDefaultStyle()->getFont()->setName('Arial');
 
// Set default font size to 12
$phpExcel->getDefaultStyle()->getFont()->setSize(12);
 
// Set spreadsheet properties – title, creator and description
$phpExcel ->getProperties()->setTitle("Product list");
$phpExcel ->getProperties()->setCreator("Voja Janjic");
$phpExcel ->getProperties()->setDescription("PHP Excel spreadsheet testing.");
 
// Create the PHPExcel spreadsheet writer object
// We will create xlsx file (Excel 2007 and above)
$writer = PHPExcel_IOFactory::createWriter($phpExcel, "Excel2007");
 
// When creating the writer object, the first sheet is also created
// We will get the already created sheet
$sheet = $phpExcel ->getActiveSheet();
 
// Set sheet title
$sheet->setTitle('My product list');
 
// Create spreadsheet header
$sheet ->getCell('A1')->setValue('Product');
$sheet ->getCell('B1')->setValue('Quanity');
$sheet ->getCell('C1')->setValue('Price');
 
// Make the header text bold and larger
$sheet->getStyle('A1:D1')->getFont()->setBold(true)->setSize(14);
 
// Insert product data
 
 
// Autosize the columns
$sheet->getColumnDimension('A')->setAutoSize(true);
$sheet->getColumnDimension('B')->setAutoSize(true);
$sheet->getColumnDimension('C')->setAutoSize(true);
 
// Save the spreadsheet
$writer->save('products.xlsx');

如果要下载电子表格而不是将其保存到服务器,请执行以下操作:

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="file.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');


编辑现有电子表格

在PHP中编辑电子表格与创建电子表格类似:
 

// Include PHPExcel library and create its object
require('PHPExcel.php');
 
// Load an existing spreadsheet
$phpExcel = PHPExcel_IOFactory::load('products.xlsx');
 
// Get the first sheet
$sheet = $phpExcel ->getActiveSheet();
 
// Remove 2 rows starting from the row 2
$sheet ->removeRow(2,2);
 
// Insert one new row before row 2
$sheet->insertNewRowBefore(2, 1);
 
// Create the PHPExcel spreadsheet writer object
// We will create xlsx file (Excel 2007 and above)
$writer = PHPExcel_IOFactory::createWriter($phpExcel, "Excel2007");
 
// Save the spreadsheet
$writer->save('products.xlsx');

准备电子表格进行打印

要准备电子表格进行打印,我们将设置纸张方向,尺寸和边距:
 

$sheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$sheet -> getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4); 
$sheet->getPageMargins()->setTop(1);
$sheet ->getPageMargins()->setRight(0.75);
$sheet ->getPageMargins()->setLeft(0.75);
$sheet ->getPageMargins()->setBottom(1);

将PHPExcel与Laravel一起使用

PHPExcel库也可以在Laravel框架中使用。查看以下PHP包(此处)并通过Composer安装它。完成安装步骤后,您可以使用以下代码将数据从数据库导出到Excel电子表格中
 

Excel::create('Products', function($excel) {
 
        // Set the title
        $excel->setTitle('Product list');
   
        // Set the creator
        $excel->setCreator('Voja Janjic');
   
        // Set description
        $excel->setDescription('PHP Excel spreadsheet testing');
   
        $excel->sheet('Products', function($sheet) {
    
                // Get data from the database
                $products = Product::all(); 
   
                // Generate header row
                $sheet->row(1, array(
                        'ID',
                        'Product',
                        'Price',
                        'Quantity',     
                ));
   
                // Generate data rows 
                $i = 2; 
                foreach($products as $product) {    
                        $sheet->row($i, array(
                                   $product->product_id,
                                   $product->product_name,
                                   $product->price,
                                   $variety->quantity,    
                        ));
    
                        $i++;
                }
 
        });
 
})->export('xlsx');

以上就是本篇文章的全部内容了,希望对大家有所帮助。

版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 :
相关文章
  • PHP获取系统毫秒数时间方法
    前言 php中获取时间方法是date(),在php中获取时间戳方法有time()、strtotime(); date():date(format, timestamp),format为格式、timestamp为时间戳(可选
  • PHP中的DI依赖注入的详细介绍
    什么是 DI / 依赖注入 依赖注入DI 其实本质上是指对类的依赖通过构造器完成 自动注入 通俗来说,就是你当前操作一个类,但是这个类的某
  • PHP8.1 Fiber交叉执行多任务(附代码)
    拿平时大家写的 for 循环举例。像 go 你可以写两个go每个里面各写一个循环同时输入,你可以看到输出是交替。在过去的php版本中,如果只开
  • PHP8.0的编译安装与使用的介绍
    安装与配置 本次使用的操作系统Ubuntu 18.04.4 LTS 安装 1.准备必要库 1 2 apt-get install -y autoconf libxml2-dev libsqlite3-dev \ libcurl4-openssl-dev libssl-dev l
  • Mac如何编译PHP 8.0 到MxSrvs工具

    Mac如何编译PHP 8.0 到MxSrvs工具
    开始准备工作 下载 PHP 8.0 PHP 官方下载 https://www.php.net/downloads.php 进入到 MxSrvs 的主程序路径下的/Applications/MxSrvs/bin,根据 Mxsrvs 的命名规则,
  • PHP8 中的 JIT的详细介绍

    PHP8 中的 JIT的详细介绍
    PHP 8 的 JIT(Just In Time)编译器将作为扩展集成到 php 中 Opcache 扩展 用于运行时将某些操作码直接转换为从 cpu 指令。 这意味着使用JIT后,
  • PHP8.2不再支持字符串中用${}插入变量了

    PHP8.2不再支持字符串中用${}插入变量了
    PHP 社区 4 月底通过了一项只有一张反对票的提案,提案内容是在即将发布的 PHP 8.2 中,不再支持使用 ${} 在字符串中插入变量的语法(标记
  • PHP8.2两个新的强类型:null和false的详细介绍

    PHP8.2两个新的强类型:null和false的详细介绍
    PHP 从 7.0 开始不断地在完善强类型,我们可以给方法参数、返回值、类属性等声明类型。 强类型可以让代码更加健壮,易于维护,可读性增
  • PHP从txt文件中读取数据的介绍

    PHP从txt文件中读取数据的介绍
    一、打开/关闭文件 1、对文件操作时首先要打开文件,打开文件用 fopen()函数,语法是: fopen(filename,mode,include_path,context); 2、对文件操作
  • PHP中token的生成
    php token的生成 接口特点汇总: 1、因为是非开放性的,所以所有的接口都是封闭的,只对公司内部的产品有效; 2、因为是非开放性的,所以
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计