import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.io.IOException;
import java.io.OutputStream;
import java.text.AttributedString;
import java.util.List;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.ShapeTypes;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
...........................중략........................
/**
* ROW 높이 자동 조절
*
* @param rownum
* @param cellValue
*/
public void setAutoRowFit(int cellnum, int rownum) {
XSSFRow row = sheet.getRow(rownum);
XSSFCell cell = row.getCell(cellnum);
XSSFFont cellFont = cell.getCellStyle().getFont();
int fontStyle = java.awt.Font.PLAIN;
if (cellFont.getBold())
fontStyle = java.awt.Font.BOLD;
if (cellFont.getItalic())
fontStyle = java.awt.Font.ITALIC;
java.awt.Font currFont = new java.awt.Font(cellFont.getFontName(), fontStyle,
cellFont.getFontHeightInPoints());
String cellText = cell.getStringCellValue();
AttributedString attrStr = new AttributedString(cellText);
attrStr.addAttribute(TextAttribute.FONT, currFont);
// Use LineBreakMeasurer to count number of lines needed for the text
//
FontRenderContext frc = new FontRenderContext(null, true, true);
LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(), frc);
int nextPos = 0;
int lineCnt = 1;
float columnWidthInPx = sheet.getColumnWidthInPixels(cellnum);
while (measurer.getPosition() < cellText.length()) {
nextPos = measurer.nextOffset(columnWidthInPx);
lineCnt++;
measurer.setPosition(nextPos);
}
System.out.println("lineCnt:" + lineCnt);
if (lineCnt > 1) {
row.setHeightInPoints(
sheet.getDefaultRowHeightInPoints() * lineCnt * /* fudge factor */ 1f);
}
}