版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/supwuq/article/details/50425025
这里实现的是一个js页面,点击导出按钮,导出Excel,此时
谷歌浏览器会自动下载,火狐会弹出来一个文件选择路径让你选,这些事浏览器自己的插件,就不用管了。
初步自己的理解:
这个按钮的点击 发送一个请求路径给后台
后台拦截到进行处理
处理过程中产生了一个Excel文件,
设置好response的一些配置,将那个文件放进去,
响应到前台js,浏览器自动解析发现有要下载的文件,就会自己下载了,无论是弹出框还是马上下载,这些是浏览器的问题了。
好了 看代码
js代码
<input type="button" class="button" value="导出" onclick= "exportExcel()">//生成按钮
<script type="text/javascript">//发送请求
function exportExcel(){
window.location="<%=request.getContextPath()%>/partOfferPast/exportExcel";
}
</script>
java后台controller
//导出excel
@RequestMapping({"exportExcel"})
@ResponseBody
public void exportExcel(JqGridRequest req, HttpServletRequest request,HttpServletResponse response) {
DataResponse<PartOfferPastCustom> list = this.getService().findAll(req);//获取表格内容
try {
HSSFWorkbook workbook = exportExcelServise.getExcel(list);//生成表格
//定义输出流,以便打开保存对话框______________________begin
OutputStream os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename="+ new String("报价履历.xls".getBytes("UTF-8"),"ISO8859-1"));
// 设定输出文件头
response.setContentType("application/x-download");// 定义输出类型
//定义输出流,以便打开保存对话框_______________________end
workbook.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
生成excel的方法getExcel()
public HSSFWorkbook getExcel( DataResponse<PartOfferPastCustom> list) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("部品报价履历一览表");
HSSFRow row = sheet.createRow((int) 0);
sheet.setDefaultColumnWidth(10);
// 创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
// 居中格式、加粗
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
org.apache.poi.hssf.usermodel.HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 11);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setBorderBottom((short) 1);// 下边框
style.setBorderLeft((short) 1);// 左边框
style.setBorderRight((short) 1);// 右边框
style.setBorderTop((short) 1);// 上边框
style.setLocked(true);
style.setFont(font);
//设置表内容格式
HSSFCellStyle style2 = wb.createCellStyle();
style2.setBorderBottom((short) 1);// 下边框
style2.setBorderLeft((short) 1);// 左边框
style2.setBorderRight((short) 1);// 右边框
style2.setBorderTop((short) 1);// 上边框
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell = row.createCell(0);
cell.setCellValue("品番");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("品名");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("补给包装费");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("单价");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("报价日期");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("备注");
cell.setCellStyle(style);
DataResponse<PartOfferPastCustom> list3 = list;
for (int i = 0; i < list3.size(); i++)
{
row = sheet.createRow((int) i + 1);
// 创建单元格,并设置值
row.createCell(0).setCellValue(list3.get(i).getPartno());
row.createCell(1).setCellValue(list3.get(i).getPartname());
row.createCell(2).setCellValue(list3.get(i).getPackcost());
row.createCell(3).setCellValue(list3.get(i).getPrice());
row.createCell(4).setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(list3.get(i).getOfferdate()));
row.createCell(5).setCellValue(list3.get(i).getRemark());
for(int j = 0;j < 5;j++){
row.getCell(j).setCellStyle(style2);}
}
return wb;
}
这样就好了,js就会自己下载了。