doubi2228 2019-02-22 07:31
浏览 299

PHPExcel:编写单元格数据类型问题时出现问题

I have PHP code to export the data to excel. Used PHPExcel library for the same.

PHPExcel library Version 1.7.6

We encountered a problem while writing the following value ==PD==[HW]RECEIVING CRC ERRORS

When I open the Excel manually and set the cell data type as TEXT it is accepting this value.

But while trying to generate the excel using PHPExcel library, getting an error as below exception 'Exception' with message 'L14 -> Formula Error: Unexpected operator '=''

I tried to solve this issue by setting the data type of the cell as STRING, but no luck... Tried below ways to set the cell data type...

#first try
$activeSheet->setCellValueExplicit($symptomColumn.$rowCount, $val, PHPExcel_Cell_DataType::TYPE_STRING);

#second try
$activeSheet->getCell($symptomColumn.$rowCount)->setValueExplicit($val, PHPExcel_Cell_DataType::TYPE_STRING);

#third try
$activeSheet->getCell($symptomColumn.$rowCount)->setDataType(PHPExcel_Cell_DataType::TYPE_STRING);

#fourth try
$activeSheet->getStyle($symptomColumn.$rowCount)
                                    ->getNumberFormat()
                                    ->setFormatCode(
                                        PHPExcel_Style_NumberFormat::FORMAT_GENERAL
                                    );

#fifth try
$activeSheet->getStyle($symptomColumn.$rowCount)
                                    ->getNumberFormat()
                                    ->setFormatCode(
                                        PHPExcel_Style_NumberFormat::FORMAT_TEXT
                                    );

Can anyone please help me to resolve the issue while writing the text "==PD==[HW]RECEIVING CRC ERRORS" to the cell while creating an excel using PHPExcel library?

Thanks in Advance...

  • 写回答

1条回答 默认 最新

  • doujing5150 2019-02-23 09:37
    关注

    If PHPExcel encounters a cell where the first character of content is an =, then it considers that cell to contain an Excel formula, and will try to evaluate it as such. If it isn't actually a formula, then it will throw an exception like this. There is no simple solution in PHPExcel, other than to suggest that you add a leading space (or other character) before the =. PHPExcel is no longer a supported library, and this bug will not be fixed (especially not in the older version that you are running).

    The latest master branch of the successor library PHPSpreadsheet does contain a fix for this, allowing you to set the style of the cell to quoted text. This will identify that this is not a formula to the calculation engine, and is similar to what you are recommended to do in MS Excel itself.

    $workSheet->setCellValue('A2', "==PD==[HW]RECEIVING CRC ERRORS");
    $workSheet->getCell('A2')->getStyle()->setQuotePrefix(true);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题