日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POI实现Excel导出时常用方法说明

發(fā)布時間:2025/3/19 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POI实现Excel导出时常用方法说明 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

場景

SpringBoot中使用POI實現(xiàn)自定義Excel布局式導(dǎo)出

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89497792

實現(xiàn)

新建XSSFWorkbook對象

XSSFWorkbook book=new XSSFWorkbook();

新建sheet頁

Sheet tempSheet = null;tempSheet = book.createSheet();

sheet賦值

book.setSheetName(0, "霸道流氓");

創(chuàng)建單元格樣式對象

XSSFCellStyle alignStyle = book.createCellStyle();

設(shè)置單元格水平居中

?alignStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);

設(shè)置單元格水平左對齊

?alignStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);

設(shè)置單元格水平右對齊

?alignStyle.setAlignment(XSSFCellStyle.ALIGN_RIGHT);

設(shè)置單元格豎直居中

alignStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);

設(shè)置單元格豎直上對齊

alignStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_TOP);

設(shè)置單元格豎直下對齊

alignStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_BOTTOM);

設(shè)置單元格底部邊框樣式

?alignStyle.setBorderBottom(BorderStyle.THIN);

設(shè)置單元格左邊邊框樣式

alignStyle.setBorderLeft(BorderStyle.THIN);

設(shè)置單元格右邊邊框樣式

alignStyle.setBorderRight(BorderStyle.THIN);

設(shè)置單元格頂部邊框樣式

alignStyle.setBorderTop(BorderStyle.THIN);

設(shè)置單元格底部邊框顏色

alignStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());

設(shè)置單元格左邊邊框顏色

?alignStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());

設(shè)置單元格右邊邊框顏色

alignStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());

設(shè)置單元格頂部邊框顏色

alignStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());

單元格設(shè)置字體

org.apache.poi.ss.usermodel.Font ztFont3 = book.createFont(); ztFont3.setBoldweight(org.apache.poi.ss.usermodel.Font.BOLDWEIGHT_BOLD); alignStyle3.setFont(ztFont3);

創(chuàng)建行并給指定列賦值

//第一行第三列 tempSheet.createRow(0).createCell(y+2).setCellValue("第一行第三列");

設(shè)置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);

設(shè)置請求響應(yīng)類型以及標(biāo)題

String title = "霸道流氓氣質(zhì)"; 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();

?

總結(jié)

以上是生活随笔為你收集整理的POI实现Excel导出时常用方法说明的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。