POI介绍
Apache软件基金会的开放源码函式库, 提供API给Java程序对 Microsoft Office 格式档案读和写的功能。
核心结构
1 HSSF - 提供读写Microsoft Excel格式档案的功能。
2 XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
3 HWPF - 提供读写Microsoft Word格式档案的功能。
4 HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
5 HDGF - 提供读写Microsoft Visio格式档案的功能。
POI导出
引入依赖
1 <!‐‐ POI导入导出依赖 ‐‐>
2 <dependency>
3 <groupId>org.apache.poi</groupId>
4 <artifactId>poi‐ooxml</artifactId>
5 <version>3.8</version>
6 </dependency>
核心代码
1 // 从数据库查询数据
2 List<CarVo> carList = getCarList(query).getDataList();
3 if(carList == null || carList.isEmpty()) {
4 return;
5 }
6
7 // 导出Excel 需要的技术 POI (poi)导入导出
8
9 // 创建一个excel工作簿
10 XSSFWorkbook workbook = new XSSFWorkbook();
11 // 创建一个sheet页
12 XSSFSheet sheet = workbook.createSheet();
13 // 创建表头行
14 XSSFRow titleRow = sheet.createRow(0);
15
16 // 创建一个内容居中、有背景色、字体样式的单元格
17 XSSFCellStyle cellStyle = workbook.createCellStyle();
18 cellStyle.setAlignment(HorizontalAlignment.CENTER);
19 // 设置背景颜色
20 cellStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
21 cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
22 setCellWidth(cellStyle);
23 // 字体样式
24 XSSFFont font = workbook.createFont();
25 font.setBold(true);
26 cellStyle.setFont(font);
27
28 // 创建表头单元格
29 String[] titleArr = {"编号","名称","品牌","颜色","排量","价格(万元)","上市
时间"};
30 Integer[] titleWidth =
{20*100,50*100,50*100,50*100,50*100,50*100,50*100};
31 for (int i = 0; i < titleArr.length; i++) {
32 XSSFCell createCell = titleRow.createCell(i);
33 createCell.setCellValue(titleArr[i]);
34 createCell.setCellStyle(cellStyle);
35 sheet.setColumnWidth(i, titleWidth[i]);
36 }
37
38 // 数据区域内容样式
39 XSSFCellStyle cellStyleCenter = workbook.createCellStyle();
40 cellStyle.setAlignment(HorizontalAlignment.CENTER);
41
42 int j = 1;
43 for (int i = 0; i < carList.size(); i++) {
44 XSSFRow dataRow = sheet.createRow(j++);
45 CarVo carVo = carList.get(i);
46
47 // 编号居中
48 XSSFCell cidCell = dataRow.createCell(0);
49 cidCell.setCellValue(carVo.getCid());
50 cidCell.setCellStyle(cellStyleCenter);
51
52 dataRow.createCell(1).setCellValue(carVo.getCname());
53 dataRow.createCell(2).setCellValue(carVo.getCbrandVo());
54 dataRow.createCell(3).setCellValue(carVo.getCcolor());
55 dataRow.createCell(4).setCellValue(carVo.getCdiscVo());
56 dataRow.createCell(5).setCellValue(carVo.getCprice());
57 dataRow.createCell(6).setCellValue(carVo.getCisup());
58 }
59
60 // 将文件返回前台
61 FileUtil.excelDownload(response, workbook,
LocalDateTime.now().toString() + "‐汽车数据.xlsx");
1 /**
2 * 设置单元格边框
3 * @param cellStyle
4 * void
5 * @throws
6 */
7 private void setCellWidth(XSSFCellStyle cellStyle) {
8 cellStyle.setBorderRight(BorderStyle.THIN);
9 cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
10
11 cellStyle.setBorderLeft(BorderStyle.THIN);
12 cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
13
14 cellStyle.setBorderTop(BorderStyle.THIN);
15 cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
16
17 cellStyle.setBorderBottom(BorderStyle.THIN);
18 cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
19 }
1 public static void excelDownload(HttpServletResponse response, XSSFWorkbo
ok workbook, String fileName) {
2 // 通过response获取的输出流,作为服务端向客户端浏览器输出内容的通道
3 OutputStream os = null;
4 try {
5 os = response.getOutputStream();
6 // 处理下载文件名的乱码问题(根据浏览器的不同进行处理)
7 response.reset();
8 response.setCharacterEncoding("UTF‐8");
9 response.setContentType("application/x‐msdownload"); // 不同类型的文件对应
不同的MIME类型
10 response.setHeader("Content‐Disposition", "attachment;filename=" + URLE
ncoder.encode(fileName,"utf‐8"));
11 workbook.write(os);
12 } catch (Exception ex) {
13 throw new RuntimeException(ex.getMessage());
14 } finally {
15 try {
16 // 关闭输出流
17 if (null != os) {
18 os.close();
19 os = null;
20 }
21 } catch (Exception ex) {
22 throw new RuntimeException(ex.getMessage());
23 }
24 }
25 }
256

被折叠的 条评论
为什么被折叠?



