这篇文章适合刚刚入门的小白或者是想要实现一个简单的导出数据功能的来参考学习
导出数据前先来简单了解一下几个类
- XSSFWorkbook和HSSFWorkbook都是Excel对象
- HSSFWorkbook:操作Excel2003以前(包括2003)的版本,扩展名是.xls
- XSSFWorkbook:操作Excel2007的版本,扩展名是.xlsx
- Sheet:工作表对象,Excel文件包涵的sheet,一个对象代表一个表单
- Row:表示表格中的行对象。
- Cell:表示表格中的单元格(列)对象。
- CellStyle:用来设置单元格样式
- XSSFFont:用来设置字体样式
- BorderStyle:用来设置边框样式
- HSSFColor:用来设置颜色样式
认识到这几个类之后我们来导入maven依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
这些都是导出Excel表格所需的依赖,下载好依赖之后开始编写工具类
package com.example.demo.utils.poi;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExportExcel {
/**
* 生成Execl
*/
public XSSFWorkbook getWorkBook(){
XSSFWorkbook wb = new XSSFWorkbook();
// 生成一个Sheet表单,参数为Sheet表单名称
Sheet sheet = wb.createSheet("0");
// 自定义列每一列的宽度,自定义8列
for (int i = 0; i < 8; i++) {
sheet.setColumnWidth(i,4300);
}
/**
* 设置单元格 样式3
*/
CellStyle cellStyle = wb.createCellStyle(); // 创建一个cellStyle对象
cellStyle.setBorderTop(BorderStyle.THIN); // 上边框样式
cellStyle.setBorderBottom(BorderStyle.THIN); // 下边框样式
cellStyle.setBorderLeft(BorderStyle.THIN); // 左边框样式
cellStyle.setBorderRight(BorderStyle.THIN); // 右边框样式
cellStyle.setTopBorderColor(HSSFColor.BLACK.index); // 上边框颜色
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 下边框颜色
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index); // 左边框颜色
cellStyle.setRightBorderColor(HSSFColor.BLACK.index); // 右边框颜色
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 上下居中
cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); // 设置背景颜色
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); // solid 填充 foreground 前景色
/**
* 列标题 在这里插入标题
*/
Row rowLabel; // 创建行对象
Cell cellLabel; // 创建列对象
rowLabel = sheet.createRow(0); // 创建第一行
cellLabel = rowLabel.createCell(0); // 创建第一行第一列
cellLabel.setCellType(CellType.STRING); // 设置第一行第一列单元格类型
cellLabel.setCellStyle(cellStyle); // 设置第一行第一列单元格样式
cellLabel.setCellValue("二级单位id"); // 设置第一行第一列单元格的值
cellLabel = rowLabel.createCell(1); // 创建第一行第二列 ......
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("二级单位名称");
cellLabel = rowLabel.createCell(2);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("项目名称");
cellLabel = rowLabel.createCell(3);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("investment_xxjd");
cellLabel = rowLabel.createCell(4);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("registered_capital_cccc");
cellLabel = rowLabel.createCell(5);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("capital_reserve_cccc");
cellLabel = rowLabel.createCell(6);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("financing_plan");
cellLabel = rowLabel.createCell(7);
cellLabel.setCellType(CellType.STRING);
cellLabel.setCellStyle(cellStyle);
cellLabel.setCellValue("bank_loan");
/**
* 页脚
*/
setExcelFooterName("测试-页脚",0,wb);
return wb;
}
/**
* 设置Excel页脚
*/
public void setExcelFooterName(String customExcelFooterName,int setExcelFooterNumber,XSSFWorkbook wb){
// 用来给Sheet设置名称(或替换名称)
// customExcelFooterName:新名称
// setExcelFooterNumber:Sheet表单索引,指的是第几个表单,0就代表第一个表单
// wb:当前Excel文件表格的对象
wb.setSheetName(setExcelFooterNumber,customExcelFooterName);
}
/**
* 输出流 导出Excel到桌面
*/
public static void exportOutPutExcel(String exprotPositionPath, XSSFWorkbook wb){
try {
File file = new File(exprotPositionPath);
// 文件输出流
FileOutputStream fileOutputStream = new FileOutputStream(file);
// 把Excel表格写入路径
wb.write(fileOutputStream);
// 关闭文件输出流连接
fileOutputStream.close();
}catch (IOException e){
System.err.println(e.getMessage());
}
}
}
在Service层操作导出Excel表格
package com.example.demo.service.impl;
import com.example.demo.dto.projectExecutiveDto;
import com.example.demo.mapper.mcProjectMapper;
import com.example.demo.mapper.projectExecutiveMapper;
import com.example.demo.service.projectExecutiveService;
import com.example.demo.utils.poi.ExportExcel;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class projectExecutiveServiceImpl implements projectExecutiveService {
@Autowired
private projectExecutiveMapper projectExecutiveMapper;
public boolean Test() {
boolean flat = false;
ExportExcel zxExportExcel = new ExportExcel();
XSSFWorkbook wb = zxExportExcel.getWorkBook();
Sheet sheet = wb.getSheetAt(0);
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 上下居中
Cell cellCheck;
List<projectExecutiveDto> data = projectExecutiveMapper.selectProjectExecutiveByYearIsMonthAndDateAndIsId();
if (data.size() != 0) {
for (int i = 0; i < data.size(); i++) {
Row rowCheck = sheet.createRow(1 + i);
cellCheck = rowCheck.createCell(0);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(data.get(i).getProj_lead_impl_unit_id());
cellCheck = rowCheck.createCell(1);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(data.get(i).getProj_lead_impl_unit_name());
cellCheck = rowCheck.createCell(3);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(data.get(i).getInvestment_xxjd());
cellCheck = rowCheck.createCell(4);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(data.get(i).getRegistered_capital_cccc());
cellCheck = rowCheck.createCell(5);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(data.get(i).getCapital_reserve_cccc());
cellCheck = rowCheck.createCell(6);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(data.get(i).getFinancing_plan());
cellCheck = rowCheck.createCell(7);
cellCheck.setCellType(CellType.STRING);
cellCheck.setCellStyle(cellStyle);
cellCheck.setCellValue(data.get(i).getBank_loan());
}
// 操作目录必须存在
ExportExcel.exportOutPutExcel("C:\\\\test\\\\Test.xlsx", wb);
flat = true;
}
return flat;
}
}
Controller层调用
package com.example.demo.controller;
import com.example.demo.service.projectExecutiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/projectExecutive")
public class projectExecutiveController {
@Autowired
private projectExecutiveService projectExecutiveService;
@RequestMapping("/getProjectExecutive")
public String getProjectExecutive(){
String result;
boolean flot = projectExecutiveService.Test();
if(flot){
result = "成功";
}else{
result = "失败";
}
System.out.println(result);
return result;
}
}
使用postman来发起测试请求
请求成功之后打开Excel表格查看数据
到这里我们的导出数据到Excel表格里的功能就已经完成了。
这篇文章适合刚刚入门的小白或者是想要实现一个简单的导出数据功能的来参考学习,其中有一些看不懂的代码或者一些方法可以自行查阅资料