以前只知道用poi导出Excel,最近用了SpringMvc的Excel导出功能,结合jxl和poi实现,的确比只用Poi好,两种实现方式如下:
一、结合jxl实现:
1、引入jxl的所需jar包:
<dependency org="net.sourceforge.jexcelapi" name="jxl" rev="2.6.3" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/>
2、接口和实现类:
接口源码:
public interface ExportRS { /** * 息导出到Excel * <P> * <ul> * </ul> * </P> * * @return 导出文件流 */ @RequestMapping(value="/export/excel" , method={RequestMethod.POST,RequestMethod.GET}) ModelAndView exportExcel(@RequestParam(value = "condition", required = true) String condition, HttpServletRequest request, HttpServletResponse response); }
实现源码:
@RestController public class ExportRSImpl implements ExportRS { @Override public ModelAndView exportExcel(String condition, HttpServletRequest request, HttpServletResponse response) { try{ NSearch nSearch = JsonObjUtil.JsonToObj(condition, NSearch.class); String userId = SecurityHelper.getCurrentUserId(); QueryResult result = _mupport.search(nSearch, userId); long total = result.getTotalCount(); //System.out.println("资产总数是:"+total); Map<String, Object> model = new HashMap<String, Object>(); model.put("total", total+""); model.put("filename", "资产信息-"+new SimpleDateFormat("yyyyMMddhhmmss").format(new Date())+".xls"); model.put("items", result.getItems()); //资产类型 List<AssetCategoryDefine> categoryList = _assetCatRp.findAllRestrict(); //所有用户 String stationId = SecurityHelper.getCurrentStationId(); List<User> userList = _rpUser.findByStationId(stationId); model.put("allUser", userList); model.put("allCategory", categoryList); return new ModelAndView(new JExcelView(),model); } catch (Throwable e) { throw new AssetRuntimeException(e); } } }