@ApiOperation("/模版下载")
@PostMapping("/excel")
public Result<?> excelExport(HttpServletResponse response) throws IOException{
// try {
// Map<Integer, List<String>> map = new HashMap<>() ;
// ExcelTitleHandler excelTitleHandler = new ExcelTitleHandler(map);
// EasyExcelHelper.writeExcelWithModel(resp, new ArrayList<>(), KoiLocalWaterReadRecordResponse.class, "水量录入", excelTitleHandler);
// } catch (Exception e) {
// e.printStackTrace();
// return new Result<>(904, "系统异常!");
// }
// return Result.success();
// 创建工作簿
Workbook workbook = new HSSFWorkbook();
//Workbook workbook = new XSSFWorkbook();也可
// // 创建Sheet页
Sheet sheet = workbook.createSheet("Sheet1");
// 创建行和单元格,并填充数据
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
Cell cell1 = row.createCell(1);
Cell cell2 = row.createCell(2);
Cell cell3 = row.createCell(3);
Cell cell4 = row.createCell(4);
cell.setCellValue("*用户编号");
cell1.setCellValue("*用户名");
cell2.setCellValue("*用户地址");
// cell3.setCellValue("*起度");
cell3.setCellValue("*止度");
// cell5.setCellValue("*水量");
cell4.setCellValue("备注");
//设置文本格式
CellStyle cellStyle = workbook.createCellStyle();
DataFormat dataFormat = workbook.createDataFormat();
cellStyle.setDataFormat(dataFormat.getFormat("@"));
// cell.setCellStyle(cellStyle);给单元格设置文本样式
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 14);
cellStyle.setFont(font);
//设置默认列的单元格格式为"文本"
for (int i = 0; i < 2; i++) {
//columnIndex表示第几列
sheet.setColumnWidth(0, 10*256);
// 主要起作用的地方
sheet.setDefaultColumnStyle(i, cellStyle);
}
String fileName = URLEncoder.encode("水量录入", "UTF-8");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
workbook.write(byteArrayOutputStream);
byte[] byte = byteArrayOutputStream.toByteArray();
// 设置响应头,指定文件名和文件类型
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename="+fileName+".xls");
response.setContentLength(byte.length);
// 关闭输出流和工作簿对象
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(byte);
outputStream.flush();
outputStream.close();
return null;
}
结果

2914

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



