poi excel导入导出
pom
<!--poi导出导入 Excel-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
导出
public String excleOut() throws IOException {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//当前系统时间
String dateNowStr = sdf.format(date);
//在当前项目的根目录下 新建 excelTest文件夹 并 新建 名字为当前日期.xlsx文件
String excelPath = System.getProperty("user.dir") + File.separator + "src\\main\\resources\\static\\excel" + File.separator + dateNowStr + ".xlsx";
log.info(excelPath);
//要导出的数据,也可以前台传
List<AppInfo> list = appInfoService.findAll();
//1.创建一个工作簿
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
//2.创建工作表
XSSFSheet sheet = xssfWorkbook.createSheet("appInfo数据");
//设置样式 star
XSSFCellStyle cellStyle = xssfWorkbook.createCellStyle();
//单元格背景样式
cellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//字体样式
XSSFFont font = xssfWorkbook.createFont();
font.setFontName("黑体");
font.setColor(IndexedColors.BLACK.getIndex());
cellStyle.setFont(font);
//设置样式 end
//3.创建标题行 --第一行 需要手动写
XSSFRow row = sheet.createRow(0);
// row.createCell(0).setCellValue("ID");
// row.createCell(1).setCellValue("app名称");
// row.createCell(2).setCellValue("部门名称");
// row.createCell(3).setCellValue("服务名称");
//如果修改样式后 需要按下面方法创建行
XSSFCell cell = row.createCell(0);
cell.setCellValue("ID");
cell.setCellStyle(cellStyle);
XSSFCell cell1 = row.createCell(1);
cell1.setCellValue("app名称");
cell1.setCellStyle(cellStyle);
XSSFCell cell2 = row.createCell(2);
cell2.setCellValue("部门名称");
cell2.setCellStyle(cellStyle);
XSSFCell cell3 = row.createCell(3);
cell3.setCellValue("服务名称");
cell3.setCellStyle(cellStyle);
//4.将list中的数据遍历,创建行
for(int i = 0;i < list.size();i++){
//设置自动列宽 star
sheet.autoSizeColumn(i);
sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 35 / 10);
//设置自动列宽 end
XSSFRow row1 = sheet.createRow(i + 1);
row1.createCell(0).setCellValue(list.get(i).getId());
row1.createCell(1).setCellValue(list.get(i).getAppName());
row1.createCell(2).setCellValue(list.get(i).getDepName());
row1.createCell(3).setCellValue(list.get(i).getServerName());
}
//5.创建输出流写数据
FileOutputStream fileOutputStream = new FileOutputStream(excelPath);
xssfWorkbook.write(fileOutputStream);
//6.关闭流
fileOutputStream.flush();
fileOutputStream.close();
xssfWorkbook.close();
log.info("*********** out excel end");
//判断excel文件是否已经生成
File file = new File(excelPath);
if(file.exists()){
return excelPath;
}else {
return "false";
}
}
导入
//案例实体AppInfo
//路径 例子:D:mytest\src\main\resources\static\excel\20210927141321.xlsx
public List<AppInfo> excelIn(@RequestParam("filePath") String filePath) throws IOException {
List<AppInfo> result = new ArrayList<>();
//1.获取工作簿
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(filePath);
//2.获取工作表
XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
//因为不需要excel的标题(第1行)
int lastRowNum = sheet.getLastRowNum();
for(int i = 1;i <= lastRowNum;i++){
XSSFRow row = sheet.getRow(i);
if(row != null){
List<String> list = new ArrayList<>();
for (Cell cell : row) {
if(cell != null){
//将excel的数据类型设置成String
cell.setCellType(CellType.STRING);
//读取数据
String value = cell.getStringCellValue();
list.add(value);
}
}
if(list.size() > 0){
AppInfo appInfo = new AppInfo();
appInfo.setId(list.get(0));
appInfo.setAppName(list.get(1));
appInfo.setDepName(list.get(2));
appInfo.setServerName(list.get(3));
result.add(appInfo);
}
}
}
return result;
}
}