PHP中创建和编辑Excel表格的方法
要使用纯PHP创建或编辑Excel电子表格,我们将使用PHPExcel库,它可以读写许多电子表格格式,包括xls,xlsx,ods和csv。在我们继续之前,仔细检查您的服务器上是否有PHP5.2或更高版本以及安装了以下PHP扩展:php_zip,php_xml和php_gd2。
创建电子表格
创建电子表格是PHP应用程序中最常见的用例之一,用于将数据导出到Excel电子表格。查看以下代码,了解如何使用PHPExcel创建示例Excel电子表格:
//IncludePHPExcellibraryandcreateitsobject
require('PHPExcel.php');
$phpExcel=newPHPExcel;
//SetdefaultfonttoArial
$phpExcel->getDefaultStyle()->getFont()->setName('Arial');
//Setdefaultfontsizeto12
$phpExcel->getDefaultStyle()->getFont()->setSize(12);
//Setspreadsheetproperties–title,creatoranddescription
$phpExcel->getProperties()->setTitle("Productlist");
$phpExcel->getProperties()->setCreator("VojaJanjic");
$phpExcel->getProperties()->setDescription("PHPExcelspreadsheettesting.");
//CreatethePHPExcelspreadsheetwriterobject
//Wewillcreatexlsxfile(Excel2007andabove)
$writer=PHPExcel_IOFactory::createWriter($phpExcel,"Excel2007");
//Whencreatingthewriterobject,thefirstsheetisalsocreated
//Wewillgetthealreadycreatedsheet
$sheet=$phpExcel->getActiveSheet();
//Setsheettitle
$sheet->setTitle('Myproductlist');
//Createspreadsheetheader
$sheet->getCell('A1')->setValue('Product');
$sheet->getCell('B1')->setValue('Quanity');
$sheet->getCell('C1')->setValue('Price');
//Maketheheadertextboldandlarger
$sheet->getStyle('A1:D1')->getFont()->setBold(true)->setSize(14);
//Insertproductdata
//Autosizethecolumns
$sheet->getColumnDimension('A')->setAutoSize(true);
$sheet->getColumnDimension('B')->setAutoSize(true);
$sheet->getColumnDimension('C')->setAutoSize(true);
//Savethespreadsheet
$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中编辑电子表格与创建电子表格类似:
//IncludePHPExcellibraryandcreateitsobject
require('PHPExcel.php');
//Loadanexistingspreadsheet
$phpExcel=PHPExcel_IOFactory::load('products.xlsx');
//Getthefirstsheet
$sheet=$phpExcel->getActiveSheet();
//Remove2rowsstartingfromtherow2
$sheet->removeRow(2,2);
//Insertonenewrowbeforerow2
$sheet->insertNewRowBefore(2,1);
//CreatethePHPExcelspreadsheetwriterobject
//Wewillcreatexlsxfile(Excel2007andabove)
$writer=PHPExcel_IOFactory::createWriter($phpExcel,"Excel2007");
//Savethespreadsheet
$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){
//Setthetitle
$excel->setTitle('Productlist');
//Setthecreator
$excel->setCreator('VojaJanjic');
//Setdescription
$excel->setDescription('PHPExcelspreadsheettesting');
$excel->sheet('Products',function($sheet){
//Getdatafromthedatabase
$products=Product::all();
//Generateheaderrow
$sheet->row(1,array(
'ID',
'Product',
'Price',
'Quantity',
));
//Generatedatarows
$i=2;
foreach($productsas$product){
$sheet->row($i,array(
$product->product_id,
$product->product_name,
$product->price,
$variety->quantity,
));
$i++;
}
});
})->export('xlsx');