xlswriter
xlswriter php 版本定义excel 列的格式
当我们使用excel 的时候总是想设置每列的格式,从“常规” 变成 “文本” 这样之类的,
以前我们使用PHPExcel 现在我们使用的是c 写的扩展 xlswriter ,这个读写的运行速度快,具体可以参考文档有多快。xlswriter 在他的文档中没有提到如何设置每列的格式,接下来我们说下如何在xlswriter 设置每个列的格式。
参考phpexcel
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle('Simple');
//设置A3单元格为文本
$objPHPExcel->getActiveSheet()->getStyle('A3')->getNumberFormat()
->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
PHPExcel_Style_NumberFormat::FORMAT_TEXT 我去看了下源码里面定义了很多常量
const FORMAT_GENERAL = 'General';
const FORMAT_TEXT = '@';
const FORMAT_NUMBER = '0';
const FORMAT_NUMBER_00 = '0.00';
const FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00';
const FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-';
const FORMAT_PERCENTAGE = '0%';
const FORMAT_PERCENTAGE_00 = '0.00%';
const FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd';
const FORMAT_DATE_YYYYMMDD = 'yy-mm-dd';
const FORMAT_DATE_DDMMYYYY = 'dd/mm/yy';
const FORMAT_DATE_DMYSLASH = 'd/m/y';
const FORMAT_DATE_DMYMINUS = 'd-m-y';
const FORMAT_DATE_DMMINUS = 'd-m';
const FORMAT_DATE_MYMINUS = 'm-y';
const FORMAT_DATE_XLSX14 = 'mm-dd-yy';
const FORMAT_DATE_XLSX15 = 'd-mmm-yy';
const FORMAT_DATE_XLSX16 = 'd-mmm';
const FORMAT_DATE_XLSX17 = 'mmm-yy';
const FORMAT_DATE_XLSX22 = 'm/d/yy h:mm';
const FORMAT_DATE_DATETIME = 'd/m/y h:mm';
const FORMAT_DATE_TIME1 = 'h:mm AM/PM';
const FORMAT_DATE_TIME2 = 'h:mm:ss AM/PM';
const FORMAT_DATE_TIME3 = 'h:mm';
const FORMAT_DATE_TIME4 = 'h:mm:ss';
const FORMAT_DATE_TIME5 = 'mm:ss';
const FORMAT_DATE_TIME6 = 'h:mm:ss';
const FORMAT_DATE_TIME7 = 'i:s.S';
const FORMAT_DATE_TIME8 = 'h:mm:ss;@';
const FORMAT_DATE_YYYYMMDDSLASH = 'yy/mm/dd;@';
const FORMAT_CURRENCY_USD_SIMPLE = '"$"#,##0.00_-';
const FORMAT_CURRENCY_USD = '$#,##0_-';
const FORMAT_CURRENCY_EUR_SIMPLE = '[$EUR ]#,##0.00_-';
在看下xlswriter excel定义的常量,连文档上描述的都是这么几种
const TYPE_STRING = 0x01;
const TYPE_INT = 0x02;
const TYPE_DOUBLE = 0x04;
const TYPE_TIMESTAMP = 0x08;
const SKIP_NONE = 0x00;
const SKIP_EMPTY_ROW = 0x01;
const SKIP_EMPTY_CELLS = 0x02;
const SKIP_EMPTY_VALUE = 0X100;
const GRIDLINES_HIDE_ALL = 0;
const GRIDLINES_SHOW_SCREEN = 1;
const GRIDLINES_SHOW_PRINT = 2;
const GRIDLINES_SHOW_ALL = 3;
最后尝试两种混合着写
$config = ['path' => '/Users/tal/Desktop'];
$excel = new \Vtiful\Kernel\Excel($config);
$fileObject = $excel->fileName('工作簿1.xlsx');
$fileHandle = $fileObject->getHandle();
$format = new \Vtiful\Kernel\Format($fileHandle);
$boldStyle = $format->number(\PHPExcel_Style_NumberFormat::FORMAT_TEXT)->toResource();
$fileObject->header(['name', 'age'])
->setColumn('A:B', 20, $boldStyle)
->output();