黑松山资源网 Design By www.paidiu.com
要使用纯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');
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2025年03月06日
2025年03月06日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]