2017-05-07 67 views
0

我做什么:从数据库 PhpExcel在电池新生产线

  • 爆炸的数据,并插入\ n
  • 准备阵列
  • 开放tamplate
  • 插件组到tamplate
    1. 查询数据
    2. 尝试用新线替换\ n

    如何找到\ n并插入新行?例如,我在A1的下一个文本“One \ nTwo”中。我从A1

    阅读的文本
    $ltr='A1'; 
    $vle=$spreadsheet->getActiveSheet()->getCell($ltr); 
    

    这个我尝试在A1在A1再次插入文本,并在一个单元

    $spreadsheet->getActiveSheet()->getCell($ltr)->setValue("${vle}"); 
    $spreadsheet->getActiveSheet()->getStyle($ltr)->getAlignment()->setWrapText(true); 
    

    \ n,其中新线替换后,但它并没有对我的工作。

    例如: enter image description here

    所以,如果我打开tamplate文本,我不能\ n的细胞取代。我能做什么?

    Upd。我尝试使用char(10)并插入公式(例如)="hello"&char(10)&"world"但是只插入="hello"&char(10)

    +0

    你是如何替换文本'\ N'?示例中提供了示例中显示多行单元格的示例 –

    +0

    @MarkBaker问题已得到解决。我发表了答案。 – autumnrustle

    回答

    1

    如果你问this issue on the PHPspreadsheet github issues log那么这是你的PHP字符串中的理解问题。

    $dataArray = [ 
        ['2010', 'Q1', 'United\nStates', 790], 
        ['2010', 'Q2', 'United States', 730], 
        ['2010', 'Q3', 'United States', 860] 
    ] 
    

    单引号中的'\n'被视为文字\n;双引号中的"\n"被视为新行字符。使用双引号因此,要么建立自己的阵列:

    $dataArray = [ 
        ['2010', 'Q1', "United\nStates", 790], 
        ['2010', 'Q2', "United States", 730], 
        ['2010', 'Q3', "United States", 860] 
    ] 
    

    或使用str_replace函数转换字面\n到新行:

    $modified = str_replace('\n', "\n", $original); 
    
    0

    在excel公式中使用“char(10)”。 & = concantinate,和炭(10)= \ n

    $textWithNewLine = '="hello"&char(10)&"world"'; 
    $spreadsheet->getActiveSheet()->getCell($ltr)->setValue($textWithNewLine); 
    
    +0

    否否。我正在更新quastion - 添加图片 – autumnrustle

    +0

    我明白了。使用char(10)这相当于excel中的新行。 $ textWithNewLine ='hello&char(10)&world' $ spreadsheet-> getActiveSheet() - > getCell($ ltr) - > setValue($ textWithNewLine); – Eyy

    +0

    你确定它的工作?我尝试 - 没有好结果 – autumnrustle

    0

    当我爆炸/内爆STR并插入\ n个I使用 ''。需要使用“”

    implode("\n", $array);