Explicitly set a cell’s datatype as text for number values

自闭症网瘾萝莉.ら 提交于 2019-12-11 02:49:00

问题


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

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

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']-different serials coming from database e.g."9876543211234567890".

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.


回答1:


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 only




回答2:


$objPHPExcel->getActiveSheet()
->getStyle('A'.$i)
->getNumberFormat()
->setFormatCode(
    PHPExcel_Style_NumberFormat::FORMAT_TEXT
);


来源:https://stackoverflow.com/questions/31086293/explicitly-set-a-cell-s-datatype-as-text-for-number-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!