phpexcel在单元格范围内设置数据类型 [英] phpexcel set data type on range of cells

查看:984
本文介绍了phpexcel在单元格范围内设置数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 PHPExcel ,我正在使用:

$workSheet->fromArray($array);

设置我的数据.但是,需要将一列设置为字符串(带有前导零的数字,如果未设置为字符串,则前导零会被截断).如果我没有使用fromArray方法,则可以使用PHPExcel_Worksheet::setCellValueExplicit()设置数据类型.

To set my data. However, one column needs to be set as string (a number with a leading zero, the leading zero gets cut off if not set to string). If I wasn't using the fromArray method, I could set the data type using PHPExcel_Worksheet::setCellValueExplicit().

但是,我想使用一系列列来做到这一点.理想情况是这样的:

However, I want to do this using a range of columns. Ideally something like this:

$worksheet->getCell('A1:A50')->setDataType(PHPExcel_Cell_DataType::TYPE_STRING);

但是看代码,getCell不允许一个范围,只能一个单元格.我该怎么办?

But looking at the code, getCell won't allow a range, only a single cell. How can I do this?

推荐答案

PHPExcel中目前没有方法可以让您为一系列单元格(仅单个单元格)设置数据类型.

There is currently no method in PHPExcel that will allow you to set the data type for a range of cells, only for an individual cell.

有两个选项...难点在于编写一个自定义单元格值绑定器,当由fromArray()设置单元格值时,该值会自动将这些单元格的数据类型设置为字符串.

There are a couple of options... the harder would be to write a custom Cell Value Binder that automatically sets the data type for those cells to string when the cell value is set by fromArray().

相反,您可以做的(并且要容易得多)是将数据类型保留为数字,而将值保留为数字,但是设置一个数字格式掩码,该掩码告诉Excel用前导零显示数字. /p>

What you could do instead (and is far easier), is to leave the datatype as a number, and the value as a numeric, but to set a number format mask which tells Excel to display the numbers with leading zeroes.

$objPHPExcel->getActiveSheet()->getStyle('L3:N2048')
                              ->getNumberFormat()->setFormatCode('0000');

这篇关于phpexcel在单元格范围内设置数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆