HelpControl.java
3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.sincere.haikangface.control;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController("/help/")
@Api(tags = "辅助工具")
@RequestMapping("/help/*")
public class HelpControl {
@RequestMapping(value = "createFile", method = RequestMethod.GET)
@ApiOperation(value = "生成文件")
@ApiImplicitParams({@ApiImplicitParam(name = "file", value = "根目录")})
public String createFile(@RequestParam("file") String file) {
// System.out.println("count:" + (count++));
//第一步,创建一个workbook对应一个excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
File file1 = new File(file);
File[] files = file1.listFiles();
List<String> list = new ArrayList<>();
for (int i = 0; i < files.length; i++) {
File file2 = files[i];
if (file2.isDirectory()) {
File[] file3 = file2.listFiles();
for (int j = 0; j < file3.length; j++) {
File file4 = file3[j];
String fileName = file4.getName();
String fileDirName = file2.getName();
list.add(fileDirName+","+fileName);
}
}else if (file2.isFile()){
String fileName = file2.getName() ;
String fileDirName = file1.getName();
list.add(fileDirName+","+fileName);
}
}
return writeToExcel(workbook,"文件",list).getAbsolutePath();
}
private File writeToExcel(HSSFWorkbook workbook, String sheetName, List<String> values) {
//第二部,在workbook中创建一个sheet对应excel中的sheet
HSSFSheet sheet = workbook.createSheet(sheetName);
//第三部,在sheet表中添加表头第0行,老版本的poi对sheet的行列有限制
HSSFRow row = sheet.createRow(0);
//第四步,创建单元格,设置表头
HSSFCell cell = null;
for (int i = 0; i < 2; i++) {
cell = row.createCell(i);
if (i == 0)
cell.setCellValue("目录名称");
else if (i==1)
cell.setCellValue("文件名称");
}
//第五步,写入数据
for (int i = 0; i < values.size(); i++) {
HSSFRow row1 = sheet.createRow(i + 1);
String value = values.get(i);
row1.createCell(0).setCellValue(value.split(",")[0]);
row1.createCell(1).setCellValue(value.split(",")[1]);
}
//将文件保存到指定的位置
try {
File file = new File("./file/");
if (!file.exists()) file.mkdirs();
File file1 = new File(file.getAbsolutePath(), sheetName + ".xls");
if (!file1.exists()) file1.createNewFile();
FileOutputStream fos = new FileOutputStream(file1);
workbook.write(fos);
System.out.println("写入成功");
fos.close();
return file1;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}