Version: Next

POI

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.6</version>
</dependency>

工具类

public class ExcelUtil {
/**
* 导出Excel
*
* @param sheetName
* sheet名称
* @param title
* 标题
* @param values
* 内容
* @param wb
* HSSFWorkbook对象
* @return
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if (wb == null) {
wb = new HSSFWorkbook();
}
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
// 声明列对象
HSSFCell cell = null;
// 创建标题
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
// 创建内容
for (int i = 0; i < values.length; i++) {
row = sheet.createRow(i + 1);
for (int j = 0; j < values[i].length; j++) {
// 将内容按顺序赋给对应的列对象
row.createCell(j).setCellValue(values[i][j]);
}
}
return wb;
}
}

Controller

/**
* 导出报表
*
* @return
*/
@RequestMapping(value = "/getMioExcel")
@ResponseBody
public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
String cardNumber = request.getParameter("searchCardNumber");
// 获取数据
List<WebServiceDto> list = (List<WebServiceDto>) webServiceMio.queryByCardNumber(cardNumber);
// excel标题
String[] title = { "姓名", "证件类型", "证件号码", "个人唯一识别码", "应付标志", "应付金额", "应付日期", "实付日期", "银行账号", "银行代码", "账户所有人名称" };
// excel文件名
String fileName = "实付信息表" + System.currentTimeMillis() + ".xls";
// sheet名
String sheetName = "实付信息表";
// 内容
String[][] content = new String[list.size()][];
for (int i = 0; i < list.size(); i++) {
content[i] = new String[title.length];
WebServiceMioDto mioDto = (WebServiceMioDto) list.get(i);
content[i][0] = mioDto.getName();
content[i][1] = mioDto.getCredentialType();
content[i][2] = mioDto.getCredentialNum();
content[i][3] = mioDto.getPersonGUID();
content[i][4] = mioDto.getMioFlag();
content[i][5] = mioDto.getMioAmnt() + "";
content[i][6] = mioDto.getPlnmioDate();
content[i][7] = mioDto.getMioDate();
content[i][8] = mioDto.getBankAccNo();
content[i][9] = mioDto.getBankCode();
content[i][10] = mioDto.getAccCustName();
}
// 创建HSSFWorkbook
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
// 响应到客户端
try (OutputStream os = response.getOutputStream()) {
this.setResponseHeader(response, fileName);
wb.write(os);
os.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
// 发送响应流方法
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
// fileName = new String(fileName.getBytes(), "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
// response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName.trim());
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}

前端

不能用Ajax请求,因为Ajax的返回类型不支持文件流,响应的文件会被解析为一堆乱码

$("#exportExcel").click(function () { // id选择器选择导出按钮
// id选择器选择input框
var searchCardNumber = $("#aac002").val();
// 请求发送到后端
window.location.href="/webservice/getMioExcel?searchCardNumber="+searchCardNumber;
})