public Map<String, Object> getCellInfo(HSSFCell cell) {
HSSFSheet sheet = cell.getSheet();
int rowIndex = cell.getRowIndex();
int columnIndex = cell.getColumnIndex();
boolean isPartOfRegion = false;
int firstColumn = 0;
int lastColumn = 0;
int firstRow = 0;
int lastRow = 0;
int sheetMergeCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetMergeCount; i++) {
CellRangeAddress ca1 = sheet.getMergedRegion(i);
lastColumn = ca1.getLastColumn();
firstColumn = ca1.getFirstColumn();
firstRow = ca1.getFirstRow();
lastRow = ca1.getLastRow();
if (rowIndex >= firstRow && rowIndex <= lastRow) {
if (columnIndex >= firstColumn && columnIndex <= lastColumn) {
isPartOfRegion = true;
break;
}
}
}
Map<String, Object> map = new HashMap<String, Object>();
Integer width = 0;
Integer height = 0;
boolean isPartOfRowsRegion = false;
if(isPartOfRegion){
for (int i = firstColumn; i <= lastColumn; i++) {
width += sheet.getColumnWidth(i);
}
for (int i = firstRow; i <= lastRow; i++) {
height += sheet.getRow(i).getHeight();
}
if(lastRow > firstRow){
isPartOfRowsRegion = true;
}
}else{
width = sheet.getColumnWidth(columnIndex);
height += cell.getRow().getHeight();
}
map.put("isPartOfRowsRegion", isPartOfRowsRegion);
map.put("firstRow", firstRow);
map.put("lastRow", lastRow);
map.put("width", width);
map.put("height", height);
return map;
}
public void modifyHeight(HSSFSheet sheet1, int i) {
List<Integer> list = new ArrayList<>();
int num = sheet1.getRow(i).getLastCellNum();
for (int j = 0; j < num; j++) {
HSSFCell currentCell = sheet1.getRow(i).getCell(j);
if (currentCell != null) {
String str = sheet1.getRow(i).getCell(j).getStringCellValue();
Map<String, Object> cellInfoMap = helper.getCellInfo(sheet1.getRow(i).getCell(j));
Integer cellWidth = (Integer) cellInfoMap.get("width");
Integer strWidth = str.length() * 256;
BigDecimal b = new BigDecimal((float) strWidth / cellWidth);
Integer length = b.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
list.add(length + 1);
}
}
Collections.sort(list, Collections.reverseOrder());
float f = 0.0f;
if (list.get(0) == 0) {
f = 25.5f;
} else {
f = list.get(0) * 25.5f;
}
sheet1.getRow(i).setHeightInPoints(f);
}
public void setStyleHead(XWPFTableRow row, String value) {
XWPFParagraph p1 = row.addNewTableCell().addParagraph();
XWPFRun run1 = p1.createRun();
p1.setVerticalAlignment(TextAlignment.CENTER);
p1.setAlignment(ParagraphAlignment.CENTER);
run1.setFontSize(8);
run1.setFontFamily("微软雅黑");
run1.setText(value);
}