The text entered in the textarea has a carriage return, and the problem that another line appears when poi exports the data to excel is solved

I encountered a thorny problem during development today. I used poi to export a data table to excel. There was no problem in use. Today, I accidentally discovered that after adding a textarea text box to the new function, the input content If there is a carriage return, the data is stored in the database, and then extracted to the front end. After the excel table is generated through poi, the table is out of order. As shown in the figure:
insert image description here
The data that should have appeared after 1 has become a new line.
In order to solve this problem, I searched a lot of solutions on the Internet, and the principle is to replace the carriage return, but after following a lot of big guys to do it, I found that it could not be replaced. After trying many methods, I finally found a solution.

exportDatas = exportDatas.replaceAll("\t", "t");
			exportDatas = exportDatas.replaceAll("\r", "r");
			exportDatas = exportDatas.replaceAll("\n", "n");
			exportDatas = exportDatas.replaceAll("trn", "\n");
			exportDatas = exportDatas.replaceAll("rn", "");
			exportDatas = exportDatas.replaceAll("t", "\t");

This is the code after the second attempt, which can be simplified. In the front, I wanted to explore how the tab stops and carriage returns in the cells are represented after the data is transmitted to the background. After verifying the data, I made the following substitutions.
Leave a message again to remind yourself that when you encounter such problems, you can first replace these special symbols with a certain character, and then do the replacement operation after understanding the rules. Expressions in the form of [\n\t\r] are regular expressions format, any characters included will be replaced, so it is not recommended to use it.

Guess you like

Origin blog.csdn.net/fzt12138/article/details/114368451