EasyPoi - 合並單元格


EasyPoi的簡單使用例子,合並指定單元格

  //dataMap 和 numVOS排序方式相同
public void excelDownload2(Map<String, Object> dataMap, TemplateExportParams params, String fileName, List<NumVOS> numVOS, HttpServletResponse response) throws IOException {
try{
OutputStream out = response.getOutputStream();
Workbook workbook = ExcelExportUtil.exportExcel(params, dataMap);
Sheet sheet =workbook.getSheetAt(0);
      //開始合並的行數
Integer startRow = 2;
      //結束合並的行數
Integer endRow = 2;
      //開始合並的列數
Integer startCol = 2;
for (NumVOS branchNumVO : numVOS) {
        //結束合並的列數 數據所占用的格數
Integer endCol = startCol + numVOS.getNum() - 1;
         //將數據寫入單元格內
sheet.getRow(startRow).getCell(startCol).setCellValue(numVOS.getName());
if (!startRow.equals(endRow) || !startCol.equals(endCol)) {
//合並單元格
CellRangeAddress region = new CellRangeAddress(startRow, endRow, startCol, endCol);
sheet.addMergedRegion(region);
}
//設置下個數據所在列數(同行合並,沒設置行數)
startCol = endCol + 1;
}
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
response.setHeader("Content-Length", String.valueOf(baos.size()));
out.write( baos.toByteArray() );
} catch (Exception e) {
logger.error("導出失敗", JSON.toJSONString(dataMAp),e);
}
}


模板如下:

 

輸出結果如下:

 

 

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM