明确将单元格的数据类型设置为数字值的文本 [英] Explicitly set a cell’s datatype as text for number values

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

问题描述

我想将单元格的数据类型显式设置为数字值的文本.

I want to explicitly set a cell’s datatype as a text for number values.

我正在从MySQL数据库中获取数据,并使用PHPExcel库导出到excel文件.我有大约20位长的大数字,我想导出到excel单元格(整个列的每个单元格都有不同的数字).

I am fetching data from my MySQL database and exporting to excel file using PHPExcel Library. I have large about 20 digit long numbers which I want to export to excel cell(each cell of the entire column will have different numbers).

我发现您可以通过提及PHPExcel文档4.6.7的代码来做到这一点.

I have found that you can do this by code mentioned PHPExcel Documentation 4.6.7.

$i=0;
foreach($dd as $d) {
    $objPHPExcel
        ->setActiveSheetIndex(0)
       ->getcell('A'.$i)
       ->setValueExplicit($d['serial_no'], PHPExcel_Cell_DataType::TYPE_STRING);
    $i++;
}

$d['serial_no']不同的序列来自数据库,例如"9876543211234567890".

$d['serial_no']-different serials coming from database e.g."9876543211234567890".

我已经实现了此代码的正常工作.但是,当我打开该excel工作表并尝试修改该单元格,然后在其他任何地方单击时,它将再次转换为数字格式(客户要求:它应该保留为文本).

I have implemented this code its working fine. But when I open that excel sheet and try to modify that cell then click on anywhere else it again gets converted to number format(client's requirement: it should remain as text).

即使我在该单元格中进行编辑,我也希望将该单元格保留为文本.

I want to stay that cell as text even if I edit in it.

推荐答案

我尚未对其进行测试,但是将单元格设置为"quotePrefix"可能会阻止MS Excel在使用MS Excel编辑数据类型时尝试转换数据类型

I've not tested it, but possibly setting the cell as "quotePrefix" may prevent MS Excel from trying to convert the datatype when editing it with MS Excel

$objPHPExcel->getActiveSheet()
    ->getStyle('A2:O128')
    ->setQuotePrefix(true);

仅适用于Excel2007

Excel2007 only

这篇关于明确将单元格的数据类型设置为数字值的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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