java 如何使用java在excel中插入换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4546804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:53:58  来源:igfitidea点击:

How to insert line break into excel using java?

javaexcel

提问by sdsd

I am read from a database column which stores data like

我从存储数据的数据库列中读取,例如

testing\n testing\n

测试\n 测试\n

I am then using the jxl.write.WritableWorkbook methods to generate the excel file that will read from this column.

然后我使用 jxl.write.WritableWorkbook 方法生成将从该列读取的 excel 文件。

However when being displyed as excel There are no line break in the column data.

但是当显示为excel时,列数据中没有换行符。

What is the reason and how should i resolve this?

这是什么原因,我应该如何解决这个问题?

回答by morja

You might try: WritableCellFormat.setWrap(true)

你可以试试: WritableCellFormat.setWrap(true)

Look here: http://jexcelapi.sourceforge.net/resources/faq/

看这里:http: //jexcelapi.sourceforge.net/resources/faq/

回答by shareef

I searched the internet since i needed an answer and here it is...

我搜索了互联网,因为我需要一个答案,这里是...

From JExcel API FAQ:

来自 JExcel API 常见问题解答:

How do I add a newline or carriage-return to a Label cell? Use "\012"or "\n". Also make sure to turn on cell wrapping with WritableCellFormat.setWrap(true)or the newline will show up as a square (unknown character).

如何向标签单元格添加换行符或回车符?使用"\012""\n"。还要确保打开单元格换行,WritableCellFormat.setWrap(true)否则换行符将显示为正方形(未知字符)。

I tested it and it worked for "\012"or "\n"as i put also .setWrap(true)and i tested it with

我测试了它,它对"\012""\n"也有效.setWrap(true),我也用它进行了测试

WritableCellFormat times16format =
            new WritableCellFormat(times16font);
            times16format.setWrap(true);
            times16format.setAlignment(Alignment.RIGHT);
            times16format.setBorder(Border.ALL, BorderLineStyle.HAIR);


//Merge col[0-2] and row[0]
        sheet.mergeCells(0, 0, 2, 0);
        Label leftPartLabel =
                new Label(
                0,
                0,
                reportKingdomData + "2" + dateData + " " + MyDateFormat.format(new Date()) + timeData + "\n" + MyTimeFormat.format(new Date()),
                times16format);

        sheet.addCell(leftPartLabel);

Insert Line In jxl JExcel API

在 jxl JExcel API 中插入行

回答by Puspendu Banerjee

Try this:

试试这个:

 for (int row = 0; row<100; row++)
 {
  sheet.addCell(new Label(0,row,"Testing "+row);
 }

Feel comfortable to let me know, if you are facing problem and don't forget to mark/upvote correct answer.

如果您遇到问题并且不要忘记标记/投票正确答案,请随时告诉我。