php phpexcel - 如何更改excel整列的数据类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22090978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
phpexcel -How to change data type for whole column of an excel
提问by Shibu
I'm trying to change the datatype for a whole column (for eg: i need to change for "M" Column to general format). Its displaying as 2.00in quantity field,I need to change this whole column to general format ie. display as "2". But its not changing the datatype.
我正在尝试更改整个列的数据类型(例如:我需要将“M”列更改为通用格式)。它2.00在数量字段中显示,我需要将整个列更改为通用格式,即。显示为"2". 但它没有改变数据类型。
Here is the code:
这是代码:
$objPHPExcel->getActiveSheet()
->setCellValue(
$aCells[$eExcelColumn] . $eExcelRow,
$sCellData ,
PHPExcel_Cell_DataType::TYPE_NUMERIC
);
$objPHPExcel->getActiveSheet()->getStyle('M1:M97')
->getNumberFormat()
->setFormatCode('0');
How to do for the whole column "M" as general format
如何将整列“M”作为通用格式
回答by Mark Baker
PHPExcel doesn't support column or row styling: you need to set the style for a rangeof cells, exactly as you are doing with
PHPExcel 不支持列或行样式:您需要为range单元格设置样式,与您所做的完全一样
$objPHPExcel->getActiveSheet()->getStyle('M1:M97')
->getNumberFormat()
->setFormatCode('0');
if you want Generalformat rather than '0', then set it to GeneralInstead:
如果您想要Generalformat 而不是'0',则将其设置为General相反:
$objPHPExcel->getActiveSheet()->getStyle('M1:M97')
->getNumberFormat()
->setFormatCode('General');
or
或者
$objPHPExcel->getActiveSheet()->getStyle('M1:M97')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_GENERAL);
回答by Klompenrunner
You can do a column like this:
你可以做一个这样的列:
$objPHPExcel->getActiveSheet()->getStyle('M:M')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_GENERAL);
A complete working example. The result in column A has two 2, column B two 2.00
一个完整的工作示例。A列的结果有两个2,B列的两个2.00
$sheet = $objPHPExcel->getActiveSheet();
$sheet->getStyle('A:A')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_GENERAL);
$sheet->getStyle('B:B')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
$sheet->setCellValue('A1', 2);
$sheet->setCellValue('A2', 2.0);
$sheet->setCellValue('B1', 2);
$sheet->setCellValue('B2', 2.0);
A few more examples for column and row selections that I pulled from my actual base:
我从实际基础中提取的列和行选择的更多示例:
$objPHPExcel->getActiveSheet()->getStyle('1:1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A:A')->getNumberFormat()->setFormatCode('yyyy/mm');
$objPHPExcel->getActiveSheet()->getStyle('D:D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E:E')->getNumberFormat()->setFormatCode('$#,###');

