php 如何让phpexcel在电话号码中保持前导0?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9635178/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:19:27  来源:igfitidea点击:

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

phpphpexcel

提问by Bogdan

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);

回答by Mark Baker

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. In your code, passing PHPExcel_Cell_DataType::TYPE_STRING to setCellValue() has no meaning, and the argument is simply ignored.

请注意,在第一种情况下,我调用的是 setCellValueExplicit() 方法,而不是 setCellValue() 方法。在您的代码中,将 PHPExcel_Cell_DataType::TYPE_STRING 传递给 setCellValue() 没有意义,并且参数被简单地忽略。

回答by Allan Sun

The easiest solution is to use setCellValueExplicitByColumnAndRow($pColumn = 0, $pRow = 1, $pValue = null, $pDataType = PHPExcel_Cell_DataType::TYPE_STRING),

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

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

回答by Alex

For me this did the trick

对我来说,这成功了

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

回答by sk001

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.

受到这个答案的启发。我希望这对某人有帮助。

回答by Juan M

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

回答by Tomasz

I came across this thread when looking for a solution and there is my other answerthat may be helpfull for someone in case or column/row deletion that causes cell formatting to get lost...

我在寻找解决方案时遇到了这个线程,还有我的其他答案可能对某人有所帮助,以防万一或列/行删除导致单元格格式丢失...