国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP中創建和編輯Excel表格的方法

2024-05-04 21:50:27
字體:
來源:轉載
供稿:網友

要使用純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.   
  4. $phpExcel = new PHPExcel; 
  5.   
  6. // Set default font to Arial 
  7. $phpExcel->getDefaultStyle()->getFont()->setName('Arial'); 
  8.   
  9. // Set default font size to 12 
  10. $phpExcel->getDefaultStyle()->getFont()->setSize(12); 
  11.   
  12. // Set spreadsheet properties – title, creator and description 
  13. $phpExcel ->getProperties()->setTitle("Product list"); 
  14. $phpExcel ->getProperties()->setCreator("Voja Janjic"); 
  15. $phpExcel ->getProperties()->setDescription("PHP Excel spreadsheet testing."); 
  16.   
  17. // Create the PHPExcel spreadsheet writer object 
  18. // We will create xlsx file (Excel 2007 and above) 
  19. $writer = PHPExcel_IOFactory::createWriter($phpExcel"Excel2007"); 
  20.   
  21. // When creating the writer object, the first sheet is also created 
  22. // We will get the already created sheet 
  23. $sheet = $phpExcel ->getActiveSheet(); 
  24.   
  25. // Set sheet title 
  26. $sheet->setTitle('My product list'); 
  27.   
  28. // Create spreadsheet header 
  29. $sheet ->getCell('A1')->setValue('Product'); 
  30. $sheet ->getCell('B1')->setValue('Quanity'); 
  31. $sheet ->getCell('C1')->setValue('Price'); 
  32.   
  33. // Make the header text bold and larger 
  34. $sheet->getStyle('A1:D1')->getFont()->setBold(true)->setSize(14); 
  35.   
  36. // Insert product data 
  37.   
  38. //Vevb.com 
  39. // Autosize the columns 
  40. $sheet->getColumnDimension('A')->setAutoSize(true); 
  41. $sheet->getColumnDimension('B')->setAutoSize(true); 
  42. $sheet->getColumnDimension('C')->setAutoSize(true); 
  43.   
  44. // Save the spreadsheet 
  45. $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.   
  4. // Load an existing spreadsheet 
  5. $phpExcel = PHPExcel_IOFactory::load('products.xlsx'); 
  6.   
  7. // Get the first sheet 
  8. $sheet = $phpExcel ->getActiveSheet(); 
  9.   
  10. // Remove 2 rows starting from the row 2 
  11. $sheet ->removeRow(2,2); 
  12.   
  13. // Insert one new row before row 2 
  14. $sheet->insertNewRowBefore(2, 1); 
  15.   
  16. // Create the PHPExcel spreadsheet writer object 
  17. // We will create xlsx file (Excel 2007 and above) 
  18. $writer = PHPExcel_IOFactory::createWriter($phpExcel"Excel2007"); 
  19.   
  20. // Save the spreadsheet 
  21. $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.   
  3.         // Set the title 
  4.         $excel->setTitle('Product list'); 
  5.     
  6.         // Set the creator 
  7.         $excel->setCreator('Voja Janjic'); 
  8.     
  9.         // Set description 
  10.         $excel->setDescription('PHP Excel spreadsheet testing'); 
  11.     
  12.         $excel->sheet('Products'function($sheet) { 
  13.      
  14.                 // Get data from the database 
  15.                 $products = Product::all();  
  16.     
  17.                 // Generate header row 
  18.                 $sheet->row(1, array
  19.                         'ID'
  20.                         'Product'
  21.                         'Price'
  22.                         'Quantity',      
  23.                 )); 
  24.     
  25.                 // Generate data rows  
  26.                 $i = 2;  
  27.                 foreach($products as $product) {     
  28.                         $sheet->row($iarray
  29.                                    $product->product_id, 
  30.                                    $product->product_name, 
  31.                                    $product->price, 
  32.                                    $variety->quantity,     
  33.                         )); 
  34.      
  35.                         $i++; 
  36.                 } 
  37.   
  38.         }); 
  39.   
  40. })->export('xlsx'); 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 正阳县| 顺义区| 龙里县| 蓬溪县| 新龙县| 海兴县| 宜兰市| 独山县| 牙克石市| 滨海县| 龙州县| 磴口县| 桐梓县| 化德县| 榆树市| 雅江县| 美姑县| 措勤县| 车致| 科技| 同仁县| 马关县| 永丰县| 抚顺县| 泊头市| 甘德县| 鄂州市| 南陵县| 台南县| 和平区| 亳州市| 商南县| 徐汇区| 瓮安县| 军事| 林芝县| 徐闻县| 阿坝县| 南充市| 揭东县| 镇赉县|