这个是我封装的Poi写数据的操作
public static void WriteExcel(HttpServletResponse response, String sheetName, ArrayList<ArrayList>arrayLists, ArrayList<String> TableName){
Workbook workBook = new XSSFWorkbook();
Sheet sheet = workBook.createSheet(sheetName);
Font font = workBook.createFont();
font.setFontHeightInPoints((short)16);
font.setFontName("黑体");
font.setItalic(true);
font.setStrikeout(true);
CellStyle cellStyle = workBook.createCellStyle();
cellStyle.setAlignment(CENTER);
cellStyle.setFont(font);
sheet.addMergedRegion(new CellRangeAddress(
0,
0,
0,
arrayLists.get(1).size()-1
));
sheet.createRow(0).createCell(0).setCellValue("学生违纪表");
Row row = sheet.createRow(1);
row.setRowStyle(cellStyle);
for (int i = 0; i < TableName.size(); i++) {
row.createCell(i).setCellValue(TableName.get(i)); }
for (int i = 2; i < arrayLists.size()+2; i++) {
int U=i-2;
Row row1 = sheet.createRow(i);
for (int j = 0; j <arrayLists.get(U).size(); j++) {
Cell cell = row1.createCell(j);
switch (arrayLists.get(U).get(j).getClass().getSimpleName()){
case "String":cell.setCellValue((String)arrayLists.get(U).get(j));
default:cell.setCellValue((String) arrayLists.get(U).get(j)); } } }
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName="+"test.xls");
try {
response.flushBuffer();
workBook.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
业务代码
public void GetDataDate(HttpServletResponse response,Object MaxDate, Object MinDate){
List<ViolationTable> violationTables = violationTableMapper.selectList(new QueryWrapper<ViolationTable>().ge("_time", MinDate).and(Qw -> (Qw.le("_time", MaxDate))));
ArrayList<ArrayList> arrayLists = new ArrayList<>();
for (ViolationTable violationTable : violationTables) {
ArrayList<String> violationTables1 = new ArrayList<>();
violationTables1.add(violationTable.getStudent_number());
violationTables1.add(violationTable.getStudent_name());
if(violationTable.getStudent_sex()==0){
violationTables1.add("女");
}else {violationTables1.add("男");}
violationTables1.add(violationTable.getStudent_class());
violationTables1.add(String.valueOf(violationTable.getBe_late()));
violationTables1.add(String.valueOf( violationTable.getTruant() ));
violationTables1.add(String.valueOf(violationTable.getLeave_early()));
violationTables1.add(String.valueOf(violationTable.get_time()));
arrayLists.add(violationTables1);
}
poi.WriteExcel(response,new DateTool().GetCurrentTime(), arrayLists, new StringTool().StringMore("学号", "姓名", "性别", "班级", "迟到", "旷课", "早退","日期"));
}
接口
@ResponseBody
@PostMapping("/download")
public void OutFile(@RequestBody Map<Object,String> GetMap, HttpServletResponse response) throws IOException {
System.out.println(GetMap.get("EndTime") + GetMap.get("StartingTime"));
violationTableService.GetDataDate(response,GetMap.get("EndTime"), GetMap.get("StartingTime"));
}
接口
@ResponseBody
@PostMapping("/download")
public void OutFile(@RequestBody Map<Object,String> GetMap, HttpServletResponse response) throws IOException {
System.out.println(GetMap.get("EndTime") + GetMap.get("StartingTime"));
violationTableService.GetDataDate(response,GetMap.get("EndTime"), GetMap.get("StartingTime"));
}
Vue前端代码
download(){
this.axios.post('/download',{StartingTime:this.value[0],EndTime:this.value[1]},{
responseType: 'blob'
}).then(res=>{
let url = window.URL.createObjectURL(new Blob([res.data]));
let link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', this.value[0]+'到'+this.value[1]+'违规表'+'.xlsx');
document.body.appendChild(link);
link.click()
})
}
|