php抓取网页表格信息(如何使用纯PHP创建或编辑Excel电子表格,我们将使用PHPExcel库)
优采云 发布时间: 2022-02-05 17:02php抓取网页表格信息(如何使用纯PHP创建或编辑Excel电子表格,我们将使用PHPExcel库)
要使用纯 PHP 创建或编辑 Excel 电子表格,我们将使用 PHPExcel 库,它可以读取和写入多种电子表格格式,包括 xls、xlsx、ods 和 csv。在我们继续之前,请仔细检查您的服务器上是否安装了 PHP 5.2 或更高版本,并且安装了以下 PHP 扩展:php_zip、php_xml 和 php_gd2。
创建电子表格
创建电子表格是将数据导出到 Excel 电子表格的 PHP 应用程序中最常见的用例之一。查看下面的代码,了解如何使用 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);
在 Laravel 中使用 PHPExcel
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');
下一步
PHPExcel 提供了许多本教程未提及的功能,例如函数、计算和图表。