首先pom配置文件
<!-- POI 包 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
前端代码:
js传递到后台, 之前用ajax传递没有反应,改成window可以直接下载。
2 后台接收
//导出已认证用户EXCEL
@ResponseBody
@RequestMapping("/exportFile")
public String exportFile(HttpServletRequest request,HttpServletResponse response) throws IOException {
response.reset(); // 清除buffer缓存
// 指定下载的文件名
String authstatus =request.getParameter("authStatus");
int authStatus =Integer.parseInt(authstatus);
Date date =new Date();
SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMddHHmmss");
String time =sdf.format(date);
response.setHeader("Content-Disposition", "attachment;filename=File" + time +".xlsx");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
XSSFWorkbook workbook = null;
try {
if(authStatus==0) {
//查询待认证用户信息
List<UserRoleDTO> list=userService.findAuthUser(0); //根据不同项目 获取list集合即可
workbook = ExcelUtils.DexportContacts(list);
}if(authStatus==1) {
//查询已认证用户信息
List<UserRoleDTO> list=userService.findAuthUser(1);
workbook = ExcelUtils.exportContacts(list);
}
OutputStream output;
try {
output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
bufferedOutPut.flush();
workbook.write(bufferedOutPut);
bufferedOutPut.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e1) {
e1.printStackTrace();
}
return null;
}
最后是excel工具类
public class ExcelUtils {
//查询已认证用户列表
public static XSSFWorkbook exportContacts(List<UserRoleDTO> list)
throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException {
XSSFWorkbook xssfWorkbook = null;
String sheetName = "已认证用户列表";
xssfWorkbook = createExcelFile(list, sheetName);
return xssfWorkbook;
}
//已认证数据生成EXCEL
public static XSSFWorkbook createExcelFile(List<UserRoleDTO> list, String sheetName)
throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException {
// 创建新的Excel工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
XSSFSheet sheet = workbook.createSheet(sheetName);
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("商户号");
cell = row.createCell(1);
cell.setCellValue("用户名");
cell = row.createCell(2);
cell.setCellValue("用户类型");
cell = row.createCell(3);
cell.setCellValue("上级用户");
cell = row.createCell(4);
cell.setCellValue("状态");
cell = row.createCell(5);
cell.setCellValue("认证");
cell = row.createCell(6);
cell.setCellValue("可用余额");
cell = row.createCell(7);
cell.setCellValue("冻结金额");
cell = row.createCell(8);
cell.setCellValue("注册时间");
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i+1);
row.createCell(0).setCellValue(list.get(i).getPayMerchantNum());
row.createCell(1).setCellValue(list.get(i).getUserName());
row.createCell(2).setCellValue(list.get(i).getRoleName());
row.createCell(3).setCellValue(list.get(i).getParentName());
row.createCell(4).setCellValue((list.get(i).getIsBlocked()==true)?"正常":"禁用");
row.createCell(5).setCellValue("已认证");
row.createCell(6).setCellValue(list.get(i).getBalance().toString());
row.createCell(7).setCellValue(list.get(i).getBlockedbalance().toString());
row.createCell(8).setCellValue(list.get(i).getCreateTime());
}
return workbook;
}
最后效果