PHP_XLSXWriter 导出excel教程
lynnk
4469 人已围观
相比于PHPExcel,PHP_XLSXWriter是一个小而强悍的Excel读写插件,它并没有PHPExcel功能丰富,但是它导出速度非常快,非常适合于数据量特别大,报表格式不是很复杂的导出需求
<?php include_once("xlsxwriter.class.php"); ini_set('display_errors', 0); ini_set('log_errors', 1); error_reporting(E_ALL & ~E_NOTICE); //设置 header,用于浏览器下载 $filename = "example.xlsx"; header('Content-disposition: attachment; filename="' . XLSXWriter::sanitize_filename($filename) . '"'); header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); header('Pragma: public'); # 表格样式 $styles1 = array( 'font' => 'Arial', 'font-size' => 12, 'font-style' => 'bold', #bold, italic, underline, strikethrough or multiple ie: 'bold,italic' 'color' => '#333', 'fill' => '#fff', # 背景填充 'halign' => 'center', # 水平位置 general, left, right, justify, center 'border' => 'left,right,top,bottom', # 边界 left, right, top, bottom, or multiple ie: 'top,left' 'border-style' => 'thin', # 边框样式 thin, medium, thick, dashDot, dashDotDot, dashed, dotted, double, hair, mediumDashDot, mediumDashDotDot, mediumDashed, slantDashDot 'border-color' => '#333', # 边框颜色 #RRGGBB, ie: #ff99cc or #f9c 'valign' => 'center', # 垂直位置 bottom, center, distributed 'height' => 50, # 行高 // 'collapsed' => true, # 未知 // 'hidden' => true, # 隐藏行 ); # 每列标题头 $header = array( 'created' => 'date', 'product_id' => 'integer', 'quantity' => '#,##0.00', #价格 #,##0.00表示小数位两个,减少或增加改变长度 'amount' => 'price', 'description' => 'string', 'tax' => '[$$-1009]#,##0.00;[RED]-[$$-1009]#,##0.00', ); # 列样式 $col_options = [ 'widths' => [20, 30, 20, 40, 40], # 宽度 'auto_filter' => true, # 筛选 // 'freeze_rows'=>true, # 冻结 // 'freeze_columns'=>true, # 冻结 // 'suppress_row' => true, ]; # 表数据 $rows = array( array('2015-01-01', '1', '-50.5', '2010-01-01 23:00:00', '2012-12-31 23:00:00', '=D2'), array('2003', '=B1', '23.5', '2010-01-01 00:00:00', '2012-12-31 00:00:00', '=D2*0.05'), ); $writer = new XLSXWriter(); $writer->setTitle('标题'); $writer->setSubject('主题'); $writer->setAuthor('作者名字'); $writer->setCompany('公司名字'); $writer->setKeywords('关键字'); $writer->setDescription('描述'); $writer->setTempDir('临时目录'); # 合并单元格,第一行的大标题 $writer->markMergedCell('Sheet1', $start_row = 0, $start_col = 0, $end_row = 0, $end_col = 5); # 每列标题头 $writer->writeSheetHeader('Sheet1', $header, $col_options); # 表数据行插入 foreach ($rows as $row) { $writer->writeSheetRow('Sheet1', $row, $styles1); } #统计行数 返回行数 $writer->countSheetRows('Sheet1'); # 输出文档 $writer->writeToStdOut(); // $writer->writeToFile('example.xlsx'); // echo $writer->writeToString(); #没什么卵用 // $writer->log('错误信息'); # 控制台输出错误信息 数据支持数组、字符串 exit(0);
发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。