SpringMVC Excel导出到指定文件夹
来源:互联网 发布:qq邮箱smtp默认端口 编辑:程序博客网 时间:2024/06/06 02:09
1、导入poi-3.8.jar
2、jsp代码:
<a id="daochu" onmousedown="daochuExcel(this);" href="">导出</a>
<script type="text/javascript">function daochuExcel(obj){ if(!confirm("您确定要导出吗?")) { return; } var answer= document.getElementsByName("checkbox"); var ids=''; for(var i=0;i<answer.length;i++){ if( answer[i].checked){ var ans=answer[i].value; ids+=ans+","; } $("body").append("<a id=exportExcel href='${basePath}orderController/Excelorder.do?orids="+ids+"'></a>"); document.getElementById("exportExcel").click(); $("#exportExcel").remove();}}</script>
3、Java代码片段:
(a)
@RequestMapping("/orderController/Excelorder") public void TestExcel(HttpServletRequest request, HttpServletResponse response,String orids) throws UnsupportedEncodingException{ response.setContentType("octets/stream"); // response.addHeader("Content-Disposition", "attachment;filename=test.xls"); String excelName = "订单信息表"; // orids="201301051121"; //转码防止乱码 response.addHeader("Content-Disposition", "attachment;filename="+new String( excelName.getBytes("gb2312"), "ISO8859-1" )+".xls"); String[] headers = new String[]{"订单号","订单状态","订单金额","最终支付金额"}; try { OutputStream out = response.getOutputStream(); exportExcel(excelName,headers, getList(orids), out,"yyyy-MM-dd"); out.close(); System.out.println("excel导出成功!"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
(b)
public List<Order> getList(String orids){ List<Order> orderlist= new ArrayList<Order>(); String[] orderids=orids.split(","); for(int i=0;i<orderids.length;i++){ Map<String,Object> params = new HashMap<String, Object>(); params.put("orderid", orderids[i]); List<Order> olist=orderbo.QueryOrderlistOrId(params); orderlist.add(olist.get(0)); } return orderlist; }
(c)
protected void exportExcel(String title,String[] headers,List mapList,OutputStream out,String pattern){ //声明一个工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); //生成一个表格 HSSFSheet sheet = workbook.createSheet(title); //设置表格默认列宽度为15个字符 sheet.setDefaultColumnWidth(20); //生成一个样式,用来设置标题样式 HSSFCellStyle style = workbook.createCellStyle(); //设置这些样式 style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //生成一个字体 HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.VIOLET.index); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //把字体应用到当前的样式 style.setFont(font); // 生成并设置另一个样式,用于设置内容样式 HSSFCellStyle style2 = workbook.createCellStyle(); style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); style2.setBorderRight(HSSFCellStyle.BORDER_THIN); style2.setBorderTop(HSSFCellStyle.BORDER_THIN); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一个字体 HSSFFont font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 style2.setFont(font2); //产生表格标题行 HSSFRow row = sheet.createRow(0); for(int i = 0; i<headers.length;i++){ HSSFCell cell = row.createCell(i); cell.setCellStyle(style); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } for (int i=0;i<mapList.size();i++) { Order o =(Order) mapList.get(i); row = sheet.createRow(i+1); int j = 0; row.createCell(j++).setCellValue(o.getOrderid()); String orderstate=o.getOrderstate(); String str1="1";String str2="2";String str3="3";String str4="4";String str5="5";String str6="6";String str7="7"; String orderstatestr=""; if(orderstate.equals(str1)){ orderstatestr="待确认"; } if(orderstate.equals(str2)){ orderstatestr="待付款"; } if(orderstate.equals(str3)){ orderstatestr="待发货"; } if(orderstate.equals(str4)){ orderstatestr="待收货"; } if(orderstate.equals(str5)){ orderstatestr="待评价"; } if(orderstate.equals(str6)){ orderstatestr="已完成"; } if(orderstate.equals(str7)){ orderstatestr="关闭交易"; } row.createCell(j++).setCellValue(orderstatestr); row.createCell(j++).setCellValue(o.getOrderamount()); row.createCell(j++).setCellValue(o.getIs_price_final_pay()); } try { workbook.write(out); } catch (IOException e) { e.printStackTrace(); } }
0 0
- SpringMVC Excel导出到指定文件夹
- winform 导出数据到指定文件夹 excel格式 但是有重复的文件就报错
- winform中 以excel的格式导出 listview的数据 到指定目标文件夹
- Datatable 导出Excel 到指定路径
- 把指定的DataSet中的内容导出到Excel中
- 把指定的DataSet中的内容导出到Excel中
- 把指定的DataSet中的内容导出到Excel中
- 浏览器客户端导出指定区域到Word、Excel
- 导出页面指定区域内容到Word/Excel
- c# 将指定的信息导出到eXcel文件中,
- java 导出数据到指定的excel模板中。
- ACCESS里导出子窗体内容到指定excel里
- 数据库数据按指定格式导出到Excel
- VB 将excel内容导出到指定文件中
- 从springMVC项目中导出数据到Excel表格
- 从springMVC项目中导出数据到Excel表格
- springmvc导出excel
- springmvc导出excel
- 堆与栈有什么区别?
- Linux基本命令(二)
- 机器学习实战——第三章:决策树ID3/C4.5
- FTP协议原理以及TCP/IP协议
- Java笔记三
- SpringMVC Excel导出到指定文件夹
- @ResponseBody,@RequestBody,@PathVariable
- 黑马学习日记11
- Linux - 访问系统信息
- html px em pt长度单位(像素 相对长度 点)知识篇
- java中String和StringBuffer哪个效率高
- 浅析typedef和define的作用域
- Java Concurrency In Practice 阅读笔记(第一、二部分)
- Python标准库:内置函数super([type[, object-or-type]])