logistics-5-subarea management

来源:互联网 发布:c语言编程图书管理系统 编辑:程序博客网 时间:2024/05/27 00:49
业务:
分区管理-添加分区
分区管理-多条件分页查询
分区管理_查询结果导出

技术点:
1.Mybatis关联多表配置
2.使用springMVC的视图解析器完成导出

01:【Mybatis关联多表配置】

02:【分区管理_关联区域下拉列表显示】


03:【分区管理_区域列表补全效果】


04:【分区管理_添加分区功能实现】


05:【分区管理_多条件分页查询(一)】


06:【分区管理_多条件分页查询(二)】


07:【分区管理_查询结构导出】-传统导出方式




    



页面导出按钮响应函数:
function doExport(){location.href=  "${pageContext.request.contextPath}/subarea_export.do";}
public void export(HttpSession httpSession, HttpServletResponse response) throws IOException {Map<String, Object> condition = (Map<String, Object>) httpSession.getAttribute("condition");// 重新查询一次 (不分页)List<Subarea> subareas = subareaService.findSubareasByCondition(condition);// 构造工作薄HSSFWorkbook hssfWorkbook = new HSSFWorkbook();// 创建sheetHSSFSheet sheet = hssfWorkbook.createSheet("分区数据信息");// 表头HSSFRow headRow = sheet.createRow(0);headRow.createCell(0).setCellValue("分区编号");headRow.createCell(1).setCellValue("省");headRow.createCell(2).setCellValue("市");headRow.createCell(3).setCellValue("区");headRow.createCell(4).setCellValue("关键字");headRow.createCell(5).setCellValue("位置");// 表数据for (Subarea subarea : subareas) {// 每一个Subarea 就是一行数据HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);dataRow.createCell(0).setCellValue(subarea.getId());dataRow.createCell(1).setCellValue(subarea.getRegion().getProvince());dataRow.createCell(2).setCellValue(subarea.getRegion().getCity());dataRow.createCell(3).setCellValue(subarea.getRegion().getDistrict());dataRow.createCell(4).setCellValue(subarea.getAddresskey());dataRow.createCell(5).setCellValue(subarea.getPosition());}// 提供下载response.setContentType("application/vnd.ms-excel");response.setHeader("Content-Disposition", "attachment;filename=subarea.xls");hssfWorkbook.write(response.getOutputStream());}
注意以上最后的3句代码。

08:【分区管理_使用springMVC的视图解析器完成导出】- springMVC导出方式

使用springMVC的视图解析器进行导出。
1.只需要扩展Spring的AbstractExcelView或者AbstractJExcelView即可。AbstractExcelView基于POI API,而AbstractJExcelView基于JExcelAPI。
2.实现buildExcelDocument()方法,在方法中使用模型数据构造出Excel文档就可以了。

这里通过前者来实现Excel视图类。
整个流程如下。
1.
/** * 条件查询 导出 *  * @throws IOException */@RequestMapping("/subarea_export.do")public String export(HttpSession httpSession, HttpServletResponse response, ModelMap map) throws IOException {Map<String, Object> condition = (Map<String, Object>) httpSession.getAttribute("condition");// 重新查询一次 (不分页)List<Subarea> subareas = subareaService.findSubareasByCondition(condition);map.put("subareas", subareas);return "subareaExcel";}
2.配置解析器
<!-- 配置Excel返回解析器 --><!-- 将 Controller 业务方法,返回值与一个bean id 匹配,执行Bean 的 视图显示方法 --><bean class="org.springframework.web.servlet.view.BeanNameViewResolver" ></bean><bean id="subareaExcel" class="cn.itcast.bos.web.view.SubareaExcelView"></bean>
3.实现解析器
public class SubareaExcelView extends AbstractExcelView {@Overrideprotected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {// 指定下载文件名称response.setHeader("Content-Disposition", "attachment;filename=" + FileUtils.encodeDownloadFilename("分区数据.xls", request.getHeader("user-agent")));// 获取数据List<Subarea> subareas = (List<Subarea>) model.get("subareas");// 创建sheetHSSFSheet sheet = workbook.createSheet("分区数据信息");// 表头HSSFRow headRow = sheet.createRow(0);headRow.createCell(0).setCellValue("分区编号");headRow.createCell(1).setCellValue("省");headRow.createCell(2).setCellValue("市");headRow.createCell(3).setCellValue("区");headRow.createCell(4).setCellValue("关键字");headRow.createCell(5).setCellValue("位置");// 表数据for (Subarea subarea : subareas) {// 每一个Subarea 就是一行数据HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);dataRow.createCell(0).setCellValue(subarea.getId());dataRow.createCell(1).setCellValue(subarea.getRegion().getProvince());dataRow.createCell(2).setCellValue(subarea.getRegion().getCity());dataRow.createCell(3).setCellValue(subarea.getRegion().getDistrict());dataRow.createCell(4).setCellValue(subarea.getAddresskey());dataRow.createCell(5).setCellValue(subarea.getPosition());}}}

注意
:由于配置了多个解析器,所以就有了优先级的概念。




页面默认的解析器InternalResourceViewResolver ,而这里配置的解析器为BeanNameViewResolver。
而优先级越小越优先。两者的优先级默认都为Integer.MAX_VALUE,相同时优先级按照顺序的优先级决定。

所以可以设置order来改变优先级顺序。可以将前者order设为10,后者设为8,则后者优先级高于前者。




关于下载文件的文件名为中文的问题?
由于IE URL编码、 火狐 Base64编码,所以可以自定义一个编码的工具类,用来进行附件名的编码。
public class FileUtils {/** * 下载文件时,针对不同浏览器,进行附件名的编码 *  * @param filename *            下载文件名 * @param agent *            客户端浏览器 * @return 编码后的下载附件名 * @throws IOException */public static String encodeDownloadFilename(String filename, String agent)throws IOException {if (agent.contains("Firefox")) { // 火狐浏览器filename = "=?UTF-8?B?"+ new BASE64Encoder().encode(filename.getBytes("utf-8"))+ "?=";filename = filename.replaceAll("\r\n", "");} else { // IE及其他浏览器filename = URLEncoder.encode(filename, "utf-8");filename = filename.replace("+", " ");}return filename;}}
则在使用时
response.setHeader("Content-Disposition", "attachment;filename=" + FileUtils.encodeDownloadFilename("分区数据.xls", request.getHeader("user-agent")));

0 0