服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

PHP中创建和编辑Excel表格的方法

2019-09-06 14:05二哈少爷 PHP教程

本文通过实例代码给大家介绍了如何在php中使用PHPExcel库实现创建和编辑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');

 

延伸 · 阅读

精彩推荐
  • PHP教程PHP封装的数据库模型Model类完整示例【基于PDO】

    PHP封装的数据库模型Model类完整示例【基于PDO】

    这篇文章主要介绍了PHP封装的数据库模型Model类,结合实例形式分析了php基于PDO针对mysql数据库常见增删改查、统计、判断等相关操作封装与使用技巧,需要的朋友可以参考下...

    小菜鸟有大梦想2802019-06-01
  • PHP教程PHP实现一个轻量级容器的方法

    PHP实现一个轻量级容器的方法

    这篇文章主要介绍了PHP实现一个轻量级容器的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...

    乌啦啦2262019-06-27
  • PHP教程PHP封装的mysqli数据库操作类示例

    PHP封装的mysqli数据库操作类示例

    这篇文章主要介绍了PHP封装的mysqli数据库操作类,涉及php针对mysqli数据库的连接、增删改查、分页、排序等相关操作技巧,需要的朋友可以参考下...

    法法之轮战神2512019-06-13
  • PHP教程php如何计算两坐标点之间的距离

    php如何计算两坐标点之间的距离

    这篇文章主要为大家详细介绍了php如何计算两坐标点之间的距离,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    王大刀3762019-07-08
  • PHP教程PHP自定义错误处理的方法分析

    PHP自定义错误处理的方法分析

    这篇文章主要介绍了PHP自定义错误处理的方法,结合实例形式分析了php错误处理机制及php自定义错误处理相关操作技巧,需要的朋友可以参考下...

    天涯海角路4772019-07-15
  • PHP教程Thinkphp5框架ajax接口实现方法分析

    Thinkphp5框架ajax接口实现方法分析

    这篇文章主要介绍了Thinkphp5框架ajax接口实现方法,结合实例形式分析了thinkPHP5 ajax交互相关操作技巧,需要的朋友可以参考下...

    zhensg3432019-08-28
  • PHP教程Linux基于php-fpm模式的lamp搭建phpmyadmin的方法

    Linux基于php-fpm模式的lamp搭建phpmyadmin的方法

    这篇文章主要介绍了Linux基于php-fpm模式的lamp搭建phpmyadmin的方法,以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。...

    我姓陈_范1382019-08-30
  • PHP教程thinkphp5实现无限级分类

    thinkphp5实现无限级分类

    这篇文章主要为大家详细介绍了thinkphp5实现无限级分类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    知末随未4392019-06-12