教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php语言怎么做表格

php语言怎么做表格

发布时间:2020-08-09   编辑:jiaochengji.com
教程集为您提供php语言怎么做表格等资源,欢迎您收藏本站,我们将为您提供最新的php语言怎么做表格资源
要使用纯PHP创建或编辑Excel电子表格,我们将使用PHPExcel库,它可以读写许多电子表格格式,包括xls,xlsx,ods和csv。在我们继续之前,仔细检查您的服务器上是否有PHP 5.2或更高版本以及安装了以下PHP扩展:php_zip,php_xml和php_gd2。

创建电子表格

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

// 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语言怎么做表格的详细内容,更多请关注教程集其它相关文章!

  • 本文原创发布教程集,转载请注明出处,感谢您的尊重!
  • 您可能感兴趣的文章:
    php语言怎么做表格
    php源码怎么变成html
    PHP与C语言的语法有什么不同
    怎么打开php文件
    Python是一门怎样的编程语言
    php文件是什么 php文件怎么打开
    php和css一样吗
    php程序员是干什么
    最牛php程序员是谁
    php是不是高级语言?

    [关闭]
    ~ ~