PHP generates Excel to control line breaks in Excel cells

Source: Internet
Author: User
The company uses Mantis to manage bugs, Mantis has a feature that exports bugs to Excel,

But the current version of this mantis, in the export of Excel, "Summary, description, problem repro Step description, additional information, comments" and so on,

When I submit a bug, the carriage return line break is gone, because there is no format, it is inconvenient to read in Excel.

This is the problem that I think we are improving.

When using PHP for Excel, how to write a newline character within an Excel cell to control the format of the cell.

I experimented, \ r \ n When generating Excel, is invalid, will be filtered.

But if you write to <br>, the effect is not to wrap the cells, but to change a new cell.

Google, in order to achieve the purpose of the cell line wrap, you need to insert the following string

' <br style= ' Mso-data-placement:same-cell; '/> '

In addition to the process of my investigation, I found another interesting question.

As mentioned earlier, when using PHP to generate Excel, \ r \ n can not afford any line-wrapping effect, to control the line, you need to use HTML tags.

However, if you put \ r \ n in <pre> </pre> Center,

The shape is as follows, then, at this time, \ r \ n is equivalent to a <br> in Excel, a new cell is made.

Wrote <pre>
... \ r \ n
... \ r \ n
... \ r \ n
</pre>

This behavior is obviously not what we want,

So let's just replace them with the following.

$p _new_lines = Array ("\ r \ n", "\ n", "   \ r", "\ r \ n", "<pre>", "</pre>", "<br>", "</br>", "< Br/> ");  $p _change_line_in_excel_cell = ' <br style= ' Mso-data-placement:same-cell; '/> ';  $p _result = Str_replace ($p _new_lines, $p _change_line_in_excel_cell, $p _input);
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.