用PHPExcel以文本格式读取数字

用PHPExcel以文本格式读取数字

本文介绍了用PHPExcel以文本格式读取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组可以存储数字,如01,01A,01B,02,2,当我用PHPExcel得到这个值的时候,例如01,02就是0。为了解决这个问题,我试图格式化这些值将存储在Excel中的行,并将其设置为文本类型,就像在下面的代码中一样:

  $ objPHPExcel-> setActiveSheetIndexByName('BlocksList'); 
$ objPHPExcel-> getActiveSheet() - > fromArray($ blockNames,null,'A2');
$ latestBLColumn = $ objPHPExcel-> getActiveSheet() - > getHighestDataColumn();
$ column ='A';
$ row = 1;
for($ column ='A'; $ column!= $ latestBLColumn; $ column ++){
$ objPHPExcel-> getActiveSheet() - > getStyle($ column。$ row) - > getNumberFormat() - > setFormatCode(PHPExcel_Style_NumberFormat :: FORMAT_TEXT);

$ objPHPExcel-> getActiveSheet() - > fromArray($ blockNames,null,'A1');

因此,通过这样做,我得到数组像01,01A,02,02B。 ..我把它存储在A2行。我得到最高的列在条件为使用此值。在这种情况下,我将范围A中的第1行设置为最高列,将其作为文本进行格式化。

我的模板已经生成,所有的数字都是文本格式的,但是问题在于我使用 fromArray() 方法,它会转换数组的数字,然后才能在excel中正确使用它。
您是否有任何想法解决这个问题?解决方案

使用数字格式的格式会影响显示的方式,而不是存储的方式。

您必须将数字明确地存储为字符串,所以不能使用fromArray()。
使用setCellValueExplicit()或setCellValueExplicitByColumnAndRow()来传递PHPExcel_Cell_DataType :: TYPE_STRING的$ pDataType参数。


$ b

编辑 p>

请注意,您也可以为一系列单元格设置样式,因此不需要添加for循环的开销:

  $ range ='A'。$ row。':'。$ latestBLColumn。$ row; 
$ objPHPExcel-> getActiveSheet()
- > getStyle($ range)
- > getNumberFormat()
- > setFormatCode(PHPExcel_Style_NumberFormat :: FORMAT_TEXT);

编辑#2 使用单元格绑定器

b

创建一个定制的单元格绑定:

  class PHPExcel_Cell_MyValueBinder扩展PHPExcel_Cell_DefaultValueBinder 
实现PHPExcel_Cell_IValueBinder


$
$
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

$ if $($ s $($ value)){
public function bindValue(PHPExcel_Cell $ cell,$ value = null){
$ value = PHPExcel_Shared_String :: SanitizeUTF8($ value);


//实现你自己的覆盖逻辑
if(is_string($ value)& $ value [0] =='0'){
$ cell-> setValueExplicit($ value,PHPExcel_Cell_DataType :: TYPE_STRING);
返回true;
}

//尚未绑定?使用默认值父...
返回parent :: bindValue($ cell,$ value);




$ b $ p
$ b为避免自动加载器出现任何问题, / Classes / PHPExcel / Cell目录。然后,在使用fromArray()调用之前,告诉PHPExcel使用你的值而不是默认的绑定:

  PHPExcel_Cell :: setValueBinder(new PHPExcel_Cell_MyValueBinder()); 


I have an array that can store numbers in such as 01, 01A, 01B, 02, 2, and When I get this value using PHPExcel, it's being removed the '0's in case of 01, 02, for example. To solve this problem, I tried to format the row where these values will be stored in excel and set it as text type, just like in the following code:

    $objPHPExcel->setActiveSheetIndexByName('BlocksList');
    $objPHPExcel->getActiveSheet()->fromArray($blockNames, null, 'A2');
    $latestBLColumn = $objPHPExcel->getActiveSheet()->getHighestDataColumn();
    $column = 'A';
    $row = 1;
    for ($column = 'A'; $column != $latestBLColumn; $column++) {
        $objPHPExcel->getActiveSheet()->getStyle($column.$row)->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT );
    }
    $objPHPExcel->getActiveSheet()->fromArray($blockNames, null, 'A1');

So, by doing this, I get the array with numbers like 01, 01A, 02, 02B... and I store it in the Row A2. I get the highest Column to use this value in the condition For. In this condition, I set for the Row 1 in the range A until the highest column, to be formated as text.

My template is generated, and all the numbers are in text format, but the problem is that I think when I use the "fromArray()" method, it transforms the numbers of the array before I can get it right in excel.Do you have any idea of how can I solve this problem??

解决方案

Formatting using a number format affects the way a number is displayed, not the way it is stored.

You'll have to store the numbers explicitly as strings, so you can't use fromArray().Use setCellValueExplicit() or setCellValueExplicitByColumnAndRow() instead, passing a $pDataType argument of PHPExcel_Cell_DataType::TYPE_STRING.

EDIT

Note that you can also set styles for a range of cells, so there's no need to add the overhead of the for loop:

$range = 'A'.$row.':'.$latestBLColumn.$row;
$objPHPExcel->getActiveSheet()
    ->getStyle($range)
    ->getNumberFormat()
    ->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT );

EDIT #2 Using a cell binder

Create a customised cell value binder:

class PHPExcel_Cell_MyValueBinder extends PHPExcel_Cell_DefaultValueBinder
    implements PHPExcel_Cell_IValueBinder
{
    public function bindValue(PHPExcel_Cell $cell, $value = null)
    {
        // sanitize UTF-8 strings
        if (is_string($value)) {
            $value = PHPExcel_Shared_String::SanitizeUTF8($value);
        }

        // Implement your own override logic
        if (is_string($value) && $value[0] == '0') {
            $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
            return true;
        }

        // Not bound yet? Use default value parent...
        return parent::bindValue($cell, $value);
    }
}

To avoid any problems with the autoloader, create this in the /Classes/PHPExcel/Cell directory. Otherwise, give the class your own non-PHPExcel name, and ensure that it's loaded independently.

Then, before using your fromArray() call, tell PHPExcel to use your value binder instead of the default binder:

PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_MyValueBinder() );

这篇关于用PHPExcel以文本格式读取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 18:57