Discussion:
Create a Cell with multi line text using Apachi POI
Forum
2011-12-06 18:22:12 UTC
Permalink
I am using Apachi POI to create excel sheet using java. I am able to create
a workbook, create sheets and add rows and column cells. I have a
requirement where in I want to create a single cell which can hold multi
lines texts and each words in the sentence with different fonts.
For Example: I want to do the following as shown in the attached image.

<nabble_img src="multiline_excel_sample_data.jpg" border="0" alt="Multi
line in a single cell using poi"/>

Please help me with a sample code. I have written code to create a row with
2 columns. But I am unable to create a cell which has multi line texts with
varying font types as shown in the attached Book1.xls file (See row1 column
2 in the excel).

<nabble_a href="Book1.xls">Book1.xls</nabble_a>

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class TestExcel {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
try {

HSSFWorkbook workbook = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("d:/Book2.xls");
HSSFSheet sheet1 = workbook.createSheet();
HSSFRow row1 = sheet1.createRow((short) 0);
HSSFCell cell1 = row1.createCell((short) 0);

cell1.setCellValue("This is Row 1 column 1.");

row1.createCell((short) 1).setCellValue("This is Row 1 column 2.");

workbook.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
;
}

}


--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-a-Cell-with-multi-line-text-using-Apachi-POI-tp5053020p5053020.html
Sent from the POI - User mailing list archive at Nabble.com.
Sergey Maslov
2011-12-07 03:27:06 UTC
Permalink
I can`t see the attachment, but adding multiline cell string is rather simple.
You should create cell style, set the wrap property and apply the
style to the cell you want.
Then you can use the standard java excape sequence ('\n') for the new line .

Example:

cell1.setCellValue("This is Row 1 \n column 1.");
CellStyle style = workbook.createCellStyle();
style.setWrapText(true);
cell1.setCellStyle(style);

You can use several fonts on a single cell storing the
HSSFRichTextString value in the cell.

Font font1 = workbook.createFont();
font1.setFontHeightInPoints((short)12);
font1.setFontName("Courier New");
font1.setItalic(true);
font1.setStrikeout(true);
font1.setColor((short)2);

Font font2 = workbook.createFont();
font2.setFontHeightInPoints((short)14);
font2.setFontName("Arial");
font2.setItalic(true);
font2.setColor((short)7);

HSSFRichTextString richString = new HSSFRichTextString("Coloured \n text");
richString.applyFont( 0, 6, font1);
richString.applyFont( 6, 13, font2);
cell1.setCellValue( richString );

I also advice you to read Busy Developers' Guide to HSSF and XSSF Features:
http://poi.apache.org/spreadsheet/quick-guide.html
It contains many useful examples.
Post by Forum
I am using Apachi POI to create excel sheet using java. I am able to create
a workbook, create sheets and add rows and column cells. I have a
requirement where in I want to create a single cell which can hold multi
lines texts  and each words in the sentence with different fonts.
For Example: I want to do the following as shown in the attached image.
<nabble_img src="multiline_excel_sample_data.jpg" border="0" alt="Multi
line in a single cell using poi"/>
Please help me with a sample code. I have written code to create a row with
2 columns. But I am unable to create a cell which has multi line texts with
varying font types as shown in the attached Book1.xls file (See row1 column
2 in the excel).
<nabble_a href="Book1.xls">Book1.xls</nabble_a>
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class TestExcel {
/**
 */
public static void main(String[] args) throws Exception {
try {
HSSFWorkbook workbook = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("d:/Book2.xls");
HSSFSheet sheet1 = workbook.createSheet();
HSSFRow row1 = sheet1.createRow((short) 0);
HSSFCell cell1 = row1.createCell((short) 0);
cell1.setCellValue("This is Row 1 column 1.");
row1.createCell((short) 1).setCellValue("This is Row 1 column 2.");
workbook.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
;
}
}
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-a-Cell-with-multi-line-text-using-Apachi-POI-tp5053020p5053020.html
Sent from the POI - User mailing list archive at Nabble.com.
Loading...