简体   繁体   English

如何让phpexcel在电话号码中保持领先0?

[英]How to get phpexcel to keep leading 0s in phone numbers?

Here's the code i'm using right now to set the cell values. 这是我正在使用的代码来设置单元格值。 It works alright if the number has separators like . 如果数字有分隔符,它可以正常工作。 or / but when there's no separator it gets saved as int and the leading 0 is stripped 或者/但是当没有分隔符时,它会被保存为int并且前导0被剥离

$sheet->setCellValue($letter[$j].$row_nr,$entity['Phone'], PHPExcel_Cell_DataType::TYPE_STRING);

Either: 或者:

// Set the value explicitly as a string
$objPHPExcel->getActiveSheet()->setCellValueExplicit('A1', '0029', PHPExcel_Cell_DataType::TYPE_STRING);

or 要么

// Set the value as a number formatted with leading zeroes
$objPHPExcel->getActiveSheet()->setCellValue('A3', 29);
$objPHPExcel->getActiveSheet()->getStyle('A3')->getNumberFormat()->setFormatCode('0000');

Note that in the first case I'm calling the setCellValueExplicit() method, not the setCellValue() method. 请注意,在第一种情况下,我正在调用setCellValueExplicit()方法,而不是setCellValue()方法。 In your code, passing PHPExcel_Cell_DataType::TYPE_STRING to setCellValue() has no meaning, and the argument is simply ignored. 在您的代码中,将PHPExcel_Cell_DataType :: TYPE_STRING传递给setCellValue()没有任何意义,并且简单地忽略该参数。

最简单的解决方案是使用setCellValueExplicitByColumnAndRow($ pColumn = 0,$ pRow = 1,$ pValue = null,$ pDataType = PHPExcel_Cell_DataType :: TYPE_STRING),

$PHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($columnPointer, $rowPointer, $value);

For me this did the trick 对我来说这就是诀窍

// Set the value explicitly as a string
$objPHPExcel->getActiveSheet()->setCellValueExplicit('A1', '0029', PHPExcel_Cell_DataType::TYPE_STRING);

Just came across an alternative solution and I thought I'd post it here. 刚刚遇到了另一种解决方案,我想我会在这里发布。 This does not require the use of libraries, just formatting the required .xls cells when creating the html table to be exported. 这不需要使用库,只需在创建要导出的html表时格式化所需的.xls单元格。

<td style="mso-number-format:'@';">your number w/leading zeroes here</td>

I hope someone finds it useful. 我希望有人发现它有用。 Below is a complete reference with formatting codes: 以下是格式代码的完整参考:

http://www.ozgrid.com/Excel/CustomFormats.htm http://www.ozgrid.com/Excel/CustomFormats.htm

Whenever someone proceeds like I did, this can help : 每当有人像我一样前进时,这可以帮助:

$inputFileName = 'file.xls';
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$objWorkSheet = $objPHPExcel->getActiveSheet();
$objWorkSheet->getCell('A1')->setValueExplicit('0029', PHPExcel_Cell_DataType::TYPE_STRING);

As inspired by this answer . 受到这个答案的启发。 I hope this help somebody. 我希望这有助于某人。

我在寻找解决方案时遇到了这个问题 ,并且我的另一个答案可能对案例或列/行删除中导致单元格格式丢失的人有所帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM