POI实现Excel导出时常用方法说明
生活随笔
收集整理的這篇文章主要介紹了
POI实现Excel导出时常用方法说明
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
SpringBoot中使用POI實現自定義Excel布局式導出
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89497792
實現
新建XSSFWorkbook對象
XSSFWorkbook book=new XSSFWorkbook();新建sheet頁
Sheet tempSheet = null;tempSheet = book.createSheet();sheet賦值
book.setSheetName(0, "霸道流氓");創建單元格樣式對象
XSSFCellStyle alignStyle = book.createCellStyle();設置單元格水平居中
?alignStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);設置單元格水平左對齊
?alignStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);設置單元格水平右對齊
?alignStyle.setAlignment(XSSFCellStyle.ALIGN_RIGHT);設置單元格豎直居中
alignStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);設置單元格豎直上對齊
alignStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_TOP);設置單元格豎直下對齊
alignStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_BOTTOM);設置單元格底部邊框樣式
?alignStyle.setBorderBottom(BorderStyle.THIN);設置單元格左邊邊框樣式
alignStyle.setBorderLeft(BorderStyle.THIN);設置單元格右邊邊框樣式
alignStyle.setBorderRight(BorderStyle.THIN);設置單元格頂部邊框樣式
alignStyle.setBorderTop(BorderStyle.THIN);設置單元格底部邊框顏色
alignStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());設置單元格左邊邊框顏色
?alignStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());設置單元格右邊邊框顏色
alignStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());設置單元格頂部邊框顏色
alignStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());單元格設置字體
org.apache.poi.ss.usermodel.Font ztFont3 = book.createFont(); ztFont3.setBoldweight(org.apache.poi.ss.usermodel.Font.BOLDWEIGHT_BOLD); alignStyle3.setFont(ztFont3);創建行并給指定列賦值
//第一行第三列 tempSheet.createRow(0).createCell(y+2).setCellValue("第一行第三列");設置Cell樣式
?tempSheet.getRow(0).getCell(y+2).setCellStyle(alignStyle5);合并單元格
//合并單元格Cell? 第1行到第1行 第3列到第10列 CellRangeAddress region = new CellRangeAddress(0,0,y+2,y+9); tempSheet.addMergedRegion(region);設置請求響應類型以及標題
String title = "霸道流氓氣質"; response.reset(); response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(title + ".xlsx", "UTF-8")))); response.setContentType("application/vnd.ms-excel;charset=UTF-8");生成excel
OutputStream out = response.getOutputStream(); book.write(out); out.flush(); out.close();?
總結
以上是生活随笔為你收集整理的POI实现Excel导出时常用方法说明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot中使用POI实现自定
- 下一篇: MyBatisPlus条件构造器带条件更