importjava.text.SimpleDateFormat;
importjava.util.Date;
importorg.apache.commons.lang3.StringUtils;
importorg.apache.poi.hssf.usermodel.HSSFCell;
importorg.apache.poi.hssf.usermodel.HSSFDataFormat;
importorg.apache.poi.hssf.usermodel.HSSFDateUtil;
importorg.apache.poi.hssf.usermodel.HSSFRow;
importorg.apache.poi.hssf.usermodel.HSSFSheet;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
importorg.apache.poi.ss.usermodel.Cell;
importorg.apache.poi.ss.util.CellRangeAddress;
/**
* excel行自适应高度
*2019年8月27日 下午1:59:27
*/
public classAutoRowHeightUtil
{
/**
*自适应excel行高
*2019年8月27日 上午10:27:33
*cell列
*@parammargin字体边距
*/
public static voidautoRowHeight(HSSFCellcell,short margin)
{
if(cell==null)
{
return;
}
StringcellVal= getStringCellValue(cell) ;
if(StringUtils.isBlank(cellVal))
{
return;
}
HSSFRowrow=cell.getRow();
HSSFWorkbookworkbook=row.getSheet().getWorkbook();
short charHeight=cell.getCellStyle().getFont(workbook).getFontHeightInPoints();
float charPx=charHeight/ (float)72 * (float)96;//字体像素
float cellWidthPx= getCellWidth(cell);//宽度像素
int charLength=cellVal.length();//字符长度
int charInCell= (int)(cellWidthPx/charPx);//每个列(包括合并的列)的字数
//在指定宽度的列中的字符展示行数
int rowNum=charLength/charInCell+ (charLength%charInCell> 0 ? 1 : 0);
//自适应之后的行高点数
short cellHeightPx= (short) (((short)rowNum) * (charHeight+margin));
row.setHeightInPoints(cellHeightPx);
}
/**
*自适应excel行高
*@authorlw
*@date2019年8月27日 上午10:27:33
*@paramcell列
*/
private staticString getStringCellValue(Cellcell)
{
switch(cell.getCellType())
{
caseHSSFCell.CELL_TYPE_FORMULA:
if(cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("yyyy-MM-dd"))
{
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");
return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
}
return cell.getCellFormula();
caseHSSFCell.CELL_TYPE_STRING:
return cell.getStringCellValue();
caseHSSFCell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell))
{
StringdateStr="";
int style=cell.getCellStyle().getDataFormat();
Datedate=cell.getDateCellValue();
//对不同格式的日期类型做不同的输出,与单元格格式保持一致
switch(style)
{
case178:
dateStr=newSimpleDateFormat("yyyy'年'M'月'd'日'").format(date);
break;
case14:
dateStr=newSimpleDateFormat("yyyy-MM-dd").format(date);
break;
case179:
dateStr=newSimpleDateFormat("yyyy/MM/dd HH:mm").format(date);
break;
case181:
dateStr=newSimpleDateFormat("yyyy/MM/dd HH:mm a ").format(date);
break;
case22:
dateStr=newSimpleDateFormat(" yyyy/MM/dd HH:mm:ss ").format(date);
break;
default:
break;
}
return dateStr;
}
else
{
cell.setCellType(Cell.CELL_TYPE_STRING);
}
return cell.getStringCellValue();
caseHSSFCell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue() ?"TRUE":"FALSE";
default:
return "";
}
}
/**
*获取单元格及合并单元格的宽度
*@authorlw
*@date2019年6月28日 下午1:58:42
*@paramcell单元格
*@return 单元格及合并单元格的宽度(像素)
*/
private static floatgetCellWidth(HSSFCellcell)
{
if(cell==null)
{
return0;
}
HSSFSheetsheet=cell.getSheet();
int rowIndex=cell.getRowIndex();
int columnIndex=cell.getColumnIndex();
float width=sheet.getColumnWidthInPixels(columnIndex);
boolean isPartOfRegion=false;
int firstColumn= 0;
int lastColumn= 0;
int firstRow= 0;
int lastRow= 0;
int sheetMergeCount=sheet.getNumMergedRegions();
for(int i= 0;i
{
CellRangeAddressca=sheet.getMergedRegion(i);
firstColumn=ca.getFirstColumn();
lastColumn=ca.getLastColumn();
firstRow=ca.getFirstRow();
lastRow=ca.getLastRow();
if(rowIndex==firstRow&&rowIndex<=lastRow)
{
if(columnIndex==firstColumn&&columnIndex<=lastColumn)
{
isPartOfRegion=true;
break;
}
}
}
if(isPartOfRegion)
{
width= 0;
for(int i=firstColumn;i<=lastColumn;i++)
{
width+=sheet.getColumnWidthInPixels(i);
}
}
return width;
}
}