Try the following ….
// Loop through source columns to add to new row
for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
// Grab a copy of the old/new cell
HSSFCell oldCell = sourceRow.getCell(i);
HSSFCell newCell = newRow.createCell(i);
// Copy style from old cell and apply to new cell
//HSSFCellStyle newCellStyle =
workbook.createCellStyle();
if (newCellStyle != null && oldCell != null){
newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
worksheet.autoSizeColumn(i);
}
newCell.setCellStyle(newCellStyle);
// If there is a cell comment, copy
if (newCell.getCellComment() != null) {
newCell.setCellComment(oldCell.getCellComment());
}
// If there is a cell hyperlink, copy
if (oldCell != null && oldCell.getHyperlink() != null) {
newCell.setHyperlink(oldCell.getHyperlink());
}
// Set the cell data type
if (oldCell != null) {
newCell.setCellType(oldCell.getCellType());
// Set the cell data value
switch (oldCell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
newCell.setCellValue(oldCell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(oldCell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_ERROR:
newCell.setCellErrorValue(oldCell.getErrorCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
newCell.setCellFormula(oldCell.getCellFormula());
break;
case Cell.CELL_TYPE_NUMERIC:
newCell.setCellValue(oldCell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
newCell.setCellValue(oldCell.getRichStringCellValue());
break;
}
}
}
--
Naga
(Srinivasa N Kadiyala)
This message (including any attachments) contains confidential information
intended for a specific individual and purpose, and is protected by law. If you
are not the intended recipient, you should delete this message and any
disclosure, copying, or distribution of this message, or the taking of any
action based on it, by you is strictly prohibited.
v.E.1
-----Original Message-----
From: Srikanth Vattikonda <[email protected]>
Reply-To: POI Users List <[email protected]>
Date: Friday, July 11, 2014 at 7:13 AM
To: POI Users List <[email protected]>
Subject: RE: XSSF data hidden in cell/column when cells merged
>Hi,
>
>I am new to using POI and trying to generate a excel sheet with two cells
>as shown below (only adding required code here)
>
>XSSFCellStyle cellStyle= workbook.createCellStyle();
>cellStyle.setFillForegroundColor(new XSSFColor(new Color(255, 255, 255)));
>cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
>cellStyle.setWrapText(true);
>cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
>cellStyle.setBorderBottom(BorderStyle.THIN);
>cellStyle.setBorderRight(BorderStyle.THIN);
>cellStyle.setBorderTop(BorderStyle.THIN);
>cellStyle.setBorderLeft(BorderStyle.THIN);
>cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
>cellStyle.setFont(fontReference);
>
>for (int i=0; i<10; i++) {
> XSSFRow row = workSheet.createRow(i);
> XSSFCell cell = row.createCell(0);
> cell.setCellStyle(cellStyle);
> cell.setCellValue("some data goes here");
> cell = row.createCell(1);
> cell.setCellStyle(cellStyle);
> cell.setCellValue("");
> workSheet.addMergedRegion(new CellRangeAddress(i, i, 0, 1));
>}
>
>workSheet.setColumnWidth(0, 4000);
>workSheet.setColumnWidth(1, 4000);
>
>But as I am merging the two cells the data in the merged cell is hidden(I
>mean the cell height is not as expected), Whereas if I create only one
>cell
>the cell height automatically fits based on the data and the complete text
>is visible. What should I do to make the complete text visible when cells
>are merged(Merged cells having height so that complete text is visible).
>Please help me out in proceeding further.
>
>Thanks,
>Srikanth