1.Excel插入行,下方行动态移动
private void excelInsertRow(int startRow,int rows,XSSFSheet sheet) {
sheet.shiftRows(startRow + 1, sheet.getLastRowNum(), rows,true,false);
startRow = startRow - 1;
for (int i = 0; i < rows; i++) {
XSSFRow sourceRow;
XSSFRow targetRow;
XSSFCell sourceCell;
XSSFCell targetCell;
short m;
startRow = startRow + 1;
sourceRow = sheet.getRow(startRow);
targetRow = sheet.createRow(startRow + 1);
targetRow.setHeight(sourceRow.getHeight());
for (m = sourceRow.getFirstCellNum(); m < sourceRow.getLastCellNum(); m++) {
sourceCell = sourceRow.getCell(m);
targetCell = targetRow.createCell(m);
targetCell.setCellStyle(sourceCell.getCellStyle());
targetCell.setCellType(sourceCell.getCellType());
}
}
}
2.合并单元格
private void mergeCell(XSSFSheet sheet,int titleColumn,int cellIndex){
int rowCount = sheet.getPhysicalNumberOfRows();
String cellText = "";
int startIndex = 0;
int endIndex = 0;
XSSFRow row = null;
Cell cell = null;
for(int i = titleColumn;i < rowCount;i++){
row = sheet.getRow(i);
cell = row.getCell(cellIndex);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(!cell.getStringCellValue().equals(cellText)){
if(startIndex != 0){
endIndex = (i-1);
sheet.addMergedRegion(new CellRangeAddress(startIndex, endIndex, cellIndex, cellIndex));
}
cellText = cell.getStringCellValue();
startIndex = i;
}
}
endIndex = (rowCount - 1);
sheet.addMergedRegion(new CellRangeAddress(startIndex, endIndex, cellIndex, cellIndex));
}