注:本文案例中使用的POI版本是3.15
(每个版本的POI用法都不太一样,特别是4.0后的新版本,还对JDK的版本有要求,至少要JDK1.8才能兼容POI4.0后的版本)
前端JSP页面
//点击下载按钮触发以下事件
function on_downLoad(obj){
window.location.href = URLStr + "downLoad?id="+obj.id
}
工具类
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;
}
}
后台控制层
@RequestMapping(value="/downLoad")
public void downLoad(HttpServletRequest request, HttpServletResponse response, TotalTableVo totalTableVo){
TotalTable totalTable = this.totalTableServiceImpl.getEntityById(TotalTable.class, totalTableVo.getId());
TransferRegistrationVo transferRegistrationVo = new TransferRegistrationVo();
transferRegistrationVo.setTtId(totalTableVo.getId());
//获取数据
List<TransferRegistration> trList = this.transferRegistrationServiceImpl.queryEntityList(transferRegistrationVo);
//excel文件名
String fileName = totalTable.getTitle();
//sheet名
String sheetName = "交接班";
//excel标题
String[] title = {"班次","天气","本班次值班人员","值班时间","上班次值班人员","交接时间","交接事项","接班异常情况"};
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String[][] content = new String[trList.size()][];
for (int i = 0; i < trList.size(); i++) {
content[i] = new String[title.length];
//将对象内容转换成string
TransferRegistration tr = trList.get(i);
content[i][0] = tr.getShift();
content[i][1] = tr.getWeather();
content[i][2] = tr.getThisWatcher();
content[i][3] = sdf.format(tr.getWatchTimeStart()) + "至" + sdf.format(tr.getWatchTimeEnd());
content[i][4] = tr.getLaseWatcher();
content[i][5] = sdf.format(tr.getHandoverTime());
content[i][6] = tr.getHandoverMatters();
content[i][7] = tr.getException();
}
//创建HSSFWorkbook
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
//将文件存到指定位置
try {
this.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//发送响应流方法
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName +".xls"); //要保存的文件名
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
实际效果如下:
注意:要想下载的时候可以自定义下载路径或文件名,要修改浏览器的设置,如下:
(这里以谷歌为例)