本文主要是介绍java 导出excel文件------第一种方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.第一种方法
首先,定义一个ExlBean.java,主要定义excel的表头标题,我这里定义的是个map,如下代码:
public class ExlBean {public static Map<String, String> map = new HashMap<String, String>();static {map.put("ordNO", "相应合同号");map.put("regionalManager", "区域经理");map.put("salesDirector", "销售总监");map.put("cityManager", "城市经理");map.put("vipDetection", "vip检测");map.put("technician", "技术专员");map.put("staffName", "评估员姓名");map.put("orderSaleNum","销售员(出单)");map.put("orderRegionalManageNum","区域经理(出单)");map.put("technicianNum","技术专员(出单)");map.put("vipDetectionNum","检测师(出单)");map.put("chargeSaleNum","销售员(销售)");map.put("chargeRegionalManageNum","区域经理(销售)");}}
其次,就是根据要求获取你自己要导出的数据列表,我这边返回的数据列表类型是List<Map<String,String>>类型,并且单个数据项要和ExlBean.java中定义的key要一致。代码如下:
@ResponseBody@RequestMapping(value = "/stats4-{type}/{name}.xls")public void getExcelCarPerformance(@PathVariable String type,HttpServletRequest request, HttpServletResponse response, HttpSession session) { String sDoneTimes=request.getParameter("sDoneTime"); String eDoneTimes=request.getParameter("eDoneTime"); BscCarPerformance bscCarPerformance=new BscCarPerformance(); bscCarPerformance.setsDoneTime(sDoneTimes); bscCarPerformance.seteDoneTime(eDoneTimes);List<Map<String, String>> list = this.carPerformanceService.findByCreateTime(bscCarPerformance);//获取到自己需要打印的数据列表HSSFWorkbook workbook = new HSSFWorkbook();//创建HSSFWorlbook对象try {OutputStream os = response.getOutputStream();HSSFSheet sheet = workbook.createSheet("sheet01");Map<String, String> titleMap = ExlBean.map;//获取到自定义的工具类if (list != null && list.size() > 0) {// 生成excel的表头Map<String, String> map1 = list.get(0);HSSFRow row = sheet.createRow(0);//创建行int j = 0;for (String key : map1.keySet()) {HSSFCell cell = row.createCell(j);String title = titleMap.get(key);if (title != null && title.length() > 0) {cell.setCellValue(title);} else {cell.setCellValue(key);}j += 1;}// 循环列表,填充数据for (int i = 1; i <= list.size() && i <= MAX_LINE; i++) {HSSFRow row1 = sheet.createRow(i);//创建行Map<String, String> map = list.get(i - 1);//获取列表中的数据项int h = 0;for (String key : map.keySet()) {//循环keyHSSFCell cell = row1.createCell(h);//创建单元格Object obj = map.get(key);//获取对应key的值if (map.get(key) != null) {
//对于特殊情况如果需要对某个单元格进行特殊操作的话,可以在这里进行代码编写,比如类型显示如下代码if("1".equals(obj.toString())){cell.setCellValue("延保订单");}else if("2".equals(obj.toString())){cell.setCellValue("充值订单");}else{cell.setCellValue(obj.toString());}
} else {cell.setCellValue("");}h += 1;}}}workbook.write(os);//把创建的OutputStream写入到workbook对象中 //最后一定要刷新和关闭流,否则报异常,导出失败。os.flush();os.close();} catch (IOException e) {logger.error(e, e);}}
我这里用的spring mvc注解进行的请求,小伙伴们可以根据自己的实际情况进行相应的操作。
在这里返回List对象类型是map类型的,其实大多数情况下大家返回的一般是自己创建的javabean,那么在这里应该怎么操作,其实这样的话,代码也是大同小异,但是就是有些繁琐,适合打印列数比较少的,如果打印列数比较多的话,我建议用上面的方法。代码如下:
HSSFWorkbook workbook = new HSSFWorkbook();try {OutputStream os = response.getOutputStream();HSSFSheet sheet = workbook.createSheet("sheet01");Map<String, String> titleMap = ExlBean.map;if (list != null && list.size() > 0) {// 生成表头Sys_staffrole_rel map1 = list.get(0);HSSFRow row = sheet.createRow(0);HSSFCell cell = row.createCell(0);String title = titleMap.get("staffName");cell.setCellValue(title);HSSFCell cell1 = row.createCell(1);String title1 = titleMap.get("orderSaleNum");cell1.setCellValue(title1);HSSFCell cell2 = row.createCell(2);String title2 = titleMap.get("orderRegionalManageNum");cell2.setCellValue(title2);HSSFCell cell3 = row.createCell(3);String title3 = titleMap.get("technicianNum");cell3.setCellValue(title3);HSSFCell cell4 = row.createCell(4);String title4 = titleMap.get("vipDetectionNum");cell4.setCellValue(title4);HSSFCell cell5 = row.createCell(5);String title5 = titleMap.get("chargeSaleNum");cell5.setCellValue(title5);HSSFCell cell6 = row.createCell(6);String title6 = titleMap.get("chargeRegionalManageNum");cell6.setCellValue(title6);// 填充数据for (int i = 1; i <= list.size() && i <= MAX_LINE; i++) {HSSFRow row1 = sheet.createRow(i);Sys_staffrole_rel map = list.get(i - 1);HSSFCell cell00 = row1.createCell(0);cell00.setCellValue(map.getStaffName());HSSFCell cell01 = row1.createCell(1);cell01.setCellValue(map.getOrderSaleNum());HSSFCell cell02 = row1.createCell(2);cell02.setCellValue(map.getOrderRegionalManageNum());HSSFCell cell03 = row1.createCell(3);cell03.setCellValue(map.getTechnicianNum());HSSFCell cell04 = row1.createCell(4);cell04.setCellValue(map.getVipDetectionNum());HSSFCell cell05 = row1.createCell(5);cell05.setCellValue(map.getChargeSaleNum());HSSFCell cell06 = row1.createCell(6);cell06.setCellValue(map.getChargeRegionalManageNum());}}
这是核心操作,其他的不做过多介绍了。
到这里导出表格的操作已经基本可以完成了,这种方法用着还行,也方便管理。
这篇关于java 导出excel文件------第一种方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!