在XSSFRichTextString中忽略新行 [英] New line ignored in XSSFRichTextString

查看:1646
本文介绍了在XSSFRichTextString中忽略新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

:编辑开始:



如果有人偶然发现这个问题,那么这里有一个小问题。



获取newLines后,我想设置行高以匹配内容。为此,我只需将行高设置为cellValue中newLines的数量。
但是当使用poi创建电子表格时,如果在cellValue之前或之后应用cellStyle似乎很重要。在我的情况下,只有在添加cellValue之前应用cellStyle 时才有效。



哦,记住rowHeight定义为1/256一个字符(!?!?!)所以将rowHeight设置为

 (短)(numberOfNewLines * 256)

:编辑结束:



我有这段代码生成Excel电子表格。一些单元格的内容是使用

  cell.setCellValue(new XSSFRichTextString(header))创建的; 
cell.setCellStyle(cs);

但是,如果我设置

  header =Estimated\\\
Duration;

然后新行不会在生成的电子表格中呈现。它只是EstimatedDuration。



我正在使用Apache POI v3.9。



任何建议?

解决方案

让我们首先介绍如何使用Excel在Excel电子表格中添加换行符。根据本文,编辑单元格内容,将光标放在所需的换行位置,然后执行 Alt + Enter 。这样做的结果是在字符串中引入换行符并在中打开单元格中的换行文本。



你有换行符字符串中的字符已经存在,但您需要添加到代码中以修改 CellStyle 以打开包装文本,并使用 setWrapText method

  String header =Estimated\\\
Duration;
cell.setCellValue(new XSSFRichTextString(header));

//添加此行以打开包装文本。
cs.setWrapText(true);

cell.setCellStyle(cs);

这似乎可以在.xls和.xlsx文件上成功测试。



如果单元格样式的换行文本属性设置为 true ,Excel似乎只会换行文本,换行是否存在。 / p>

:EDIT START:

Should anyone stumble across this q then here's a small heads up.

After getting newLines to work I wanted to set the row height to match the content. To do this I simply set the row height to the number of newLines in the cellValue. However when creating a spreadsheet using poi it seems to be matter if you apply the cellStyle before or after the cellValue. In my case it only worked if I apply the cellStyle before adding the cellValue.

Oh and remember that rowHeight is defined as 1/256 of a character(!?!?!) so set rowHeight to

(short)(numberOfNewLines*256)

:EDIT END:

I have this piece of code that generates an Excel spreadsheet. The contents of some of the cells are created using

cell.setCellValue(new XSSFRichTextString(header));
cell.setCellStyle(cs);

However if I set

header = "Estimated\nDuration";

then the new line is not rendered in the resulting spreadsheet. It just comes out as "EstimatedDuration".

I'm using Apache POI v3.9.

Any suggestions?

解决方案

Let's start with how to put a newline in an Excel spreadsheet using Excel. According to this article, edit the cell contents, placing the cursor at the desired newline location, and do Alt + Enter. The effect of this is to introduce a newline character into the string and turn on "Wrap Text" in the cell.

You have the newline character in the string already, but you'll need to add to your code to modify your CellStyle to turn on wrapped text, with the setWrapText method.

String header = "Estimated\nDuration";
cell.setCellValue(new XSSFRichTextString(header));

// Add this line to turn on wrapped text.
cs.setWrapText(true);

cell.setCellStyle(cs);

This appears to test successfully on .xls and .xlsx files.

It appears that Excel will only wrap text, newline present or not, if the cell style has the "wrap text" property set to true.

这篇关于在XSSFRichTextString中忽略新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆