合并相同数据单元格
int departRow = 0;
String departValue = "";
System.out.println("sheet.getLastRowNum()=" + sheet.getLastRowNum());
for (int i = 2; i <= sheet.getLastRowNum() + 1; i++) {
HSSFRow rows = sheet.getRow(i);
HSSFCell cells0 = null;
if (rows != null) {
cells0 = rows.getCell(2);
}
if (cells0 == null || !cells0.getStringCellValue().equals(departValue)) {
if (StringUtils.isNotBlank(departValue)) {
sheet.addMergedRegion(new CellRangeAddress(departRow, i - 1, 2, 2));
if (rows!=null){
HSSFCell cellsV = rows.getCell(2);
cellsV.setCellStyle(style);
}
}
if (cells0 != null) {
departRow = i;
departValue = cells0.getStringCellValue();
}
}
}
添加备注
addComment(patriarch, cell, "备注");
/**
* 设置单元格注释
*
* @param patriarch
* @param cell
* @param cellnum
*/
public static void addComment(HSSFPatriarch patriarch, HSSFCell cell, String cellnum) {
HSSFComment comment = patriarch.createComment(
new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 2, (short) 4, 4));
comment.setString(new HSSFRichTextString(cellnum));
cell.setCellComment(comment);
}