ds122455 2018-10-24 10:50
浏览 241
已采纳

PHP_XLSXWriter muti-lines头问题

I try to create with PHP_XLSXWriter an excel file having 2 header (text) lines and numeric values in the following lines. I manage to create from php a file having 1 header line with expected types and styles for heder line and following data lines. This can be open in excel correctly. When I insert from php a second header line (text) with writeSheetRow method, excel displays an warning/error message and suggests to repair the file. When I accept the file to be repaired, file content is then ok (header (2 lines) and numeric data). After saving the file, there is no more warning message when opening the file. Does anybody knows a way to proceed in order to avoid the initiel warning message? Thanks.

More details: The issue is due to the fact that comumns types are defined when writing the header (row 1, with writeSheetHeader method). This determine the column's types of all following lines (from row 2 to ..). In my case row 2 is not a data line. It contains only strings that are second header line. This cause the problem because there is a conflict between the type defined in the header (numeric) and the actual values in the line (sring).

  • 写回答

1条回答 默认 最新

  • donglu9825 2018-11-13 21:30
    关注

    I found a solution by creating a modified version of writeSheetRow method that I named : writeSheetRowHeader. In tthis new method I force the the type of the values inserted through this method to 'GENRAL'.

    Insted of :

    public function writeSheetRow($sheet_name, array $row, $row_options=null)
    {
        ....
            $c=0;
            foreach ($row as $v) {
                $number_format = $sheet->columns[$c]['number_format']; //BEFORE
                $number_format_type = $sheet->columns[$c]['number_format_type']; //BEFORE
                $cell_style_idx = empty($style) ? $sheet->columns[$c]['default_cell_style'] : $this->addCellStyle( $number_format, json_encode(isset($style[0]) ? $style[$c] : $style) );
                $this->writeCell($sheet->file_writer, $sheet->row_count, $c, $v, $number_format_type, $cell_style_idx);
                $c++;
            }
    .....
    }
    

    I use

    public function writeSheetRowHeader($sheet_name, array $row, $row_options=null)
    {
        ....
            $c=0;
            foreach ($row as $v) {
                $number_format = 'GENERAL'; // AFTER
                $number_format_type = 'n_auto'; // AFTER
                $cell_style_idx = empty($style) ? $sheet->columns[$c]['default_cell_style'] : $this->addCellStyle( $number_format, json_encode(isset($style[0]) ? $style[$c] : $style) );
                $this->writeCell($sheet->file_writer, $sheet->row_count, $c, $v, $number_format_type, $cell_style_idx);
                $c++;
            }
    .....
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?