当前位置:Gxlcms > php框架 > PHP中创建和编辑Excel表格的方法

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

时间:2021-07-01 10:21:17 帮助过:35人阅读

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

创建电子表格

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

  1. // Include PHPExcel library and create its object
  2. require('PHPExcel.php');
  3. $phpExcel = new PHPExcel;
  4. // Set default font to Arial
  5. $phpExcel->getDefaultStyle()->getFont()->setName('Arial');
  6. // Set default font size to 12
  7. $phpExcel->getDefaultStyle()->getFont()->setSize(12);
  8. // Set spreadsheet properties – title, creator and description
  9. $phpExcel ->getProperties()->setTitle("Product list");
  10. $phpExcel ->getProperties()->setCreator("Voja Janjic");
  11. $phpExcel ->getProperties()->setDescription("PHP Excel spreadsheet testing.");
  12. // Create the PHPExcel spreadsheet writer object
  13. // We will create xlsx file (Excel 2007 and above)
  14. $writer = PHPExcel_IOFactory::createWriter($phpExcel, "Excel2007");
  15. // When creating the writer object, the first sheet is also created
  16. // We will get the already created sheet
  17. $sheet = $phpExcel ->getActiveSheet();
  18. // Set sheet title
  19. $sheet->setTitle('My product list');
  20. // Create spreadsheet header
  21. $sheet ->getCell('A1')->setValue('Product');
  22. $sheet ->getCell('B1')->setValue('Quanity');
  23. $sheet ->getCell('C1')->setValue('Price');
  24. // Make the header text bold and larger
  25. $sheet->getStyle('A1:D1')->getFont()->setBold(true)->setSize(14);
  26. // Insert product data
  27. // Autosize the columns
  28. $sheet->getColumnDimension('A')->setAutoSize(true);
  29. $sheet->getColumnDimension('B')->setAutoSize(true);
  30. $sheet->getColumnDimension('C')->setAutoSize(true);
  31. // Save the spreadsheet
  32. $writer->save('products.xlsx');

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

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

编辑现有电子表格

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

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

准备电子表格进行打印

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

  1. $sheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
  2. $sheet -> getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
  3. $sheet->getPageMargins()->setTop(1);
  4. $sheet ->getPageMargins()->setRight(0.75);
  5. $sheet ->getPageMargins()->setLeft(0.75);
  6. $sheet ->getPageMargins()->setBottom(1);

将PHPExcel与Laravel一起使用

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

  1. Excel::create('Products', function($excel) {
  2. // Set the title
  3. $excel->setTitle('Product list');
  4. // Set the creator
  5. $excel->setCreator('Voja Janjic');
  6. // Set description
  7. $excel->setDescription('PHP Excel spreadsheet testing');
  8. $excel->sheet('Products', function($sheet) {
  9. // Get data from the database
  10. $products = Product::all();
  11. // Generate header row
  12. $sheet->row(1, array(
  13. 'ID',
  14. 'Product',
  15. 'Price',
  16. 'Quantity',
  17. ));
  18. // Generate data rows
  19. $i = 2;
  20. foreach($products as $product) {
  21. $sheet->row($i, array(
  22. $product->product_id,
  23. $product->product_name,
  24. $product->price,
  25. $variety->quantity,
  26. ));
  27. $i++;
  28. }
  29. });
  30. })->export('xlsx');

人气教程排行