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++;
            }
    .....
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?