pom.xml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
Controller
@Autowired
private HttpServletResponse response;
@ResponseBody
@RequestMapping("download_execl")
public void download_execl(LandingPageDto landingPageDto) throws IOException{
List<LoanDataDto> list = projectService.download_execl_select(landingPageDto.download_execl_select());
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
HSSFRow row = sheet.createRow(0);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell = null;
cell = row.createCell(0);
cell.setCellValue("电话");
int line = 1;
for(int i=0;i<list.size();i++){
row = sheet.createRow(line);
line++;
LoanDataDto loanDataDto = list.get(i);
if(BaseUtils.checkStringempty(loanDataDto.getPhone())){
cell = row.createCell(0);
cell.setCellValue(loanDataDto.getPhone());
}
}
OutputStream output=response.getOutputStream();
response.reset();
String filename = "domo".xls";
response.setHeader("Content-disposition", "attachment; filename="+ new String(filename.getBytes("UTF8"), "ISO8859-1"));
response.setContentType("application/vnd.ms-excel");
wb.write(output);
output.close();
}
html
<button type="button" data-th-disabled = "${total_forward_num eq 0}" class="btn btn-primary" onclick="download_execl()">下载号码</button>
function download_execl(){
var param = $("#fileForm").serialize();
window.location.href = "download_execl?"+param;
}